I am using Visual Studio 2008.

I created a windows Console project and wrote my code. it compiled well and the program created a default .exe file.

Now i created another project. this time its a C# project, and i am trying to access the functionality from the C++ project i used before.

So what i did was i tried to add the .exe file of the C++ project as a reference in the C# project but i get the following error message.

"a reference to c:......./example.exe could not be added. please make sure the the file is accessible, and that is a valid assembly or COM component"

what should i do ? how should i resolve this ?

I wonder if i should post this in the C# forum, but yet i think i have to make changes to the C++ project (or may be i am wrong)

Help

Recommended Answers

All 3 Replies

You'll want a C++ DLL to access the functions from C#.

Unfortunately for you, it isn't really as easy as adding a reference to a C++ executable.

For a guide to creating a DLL please consult google.

You'll want a C++ DLL to access the functions from C#.

Unfortunately for you, it isn't really as easy as adding a reference to a C++ executable.

For a guide to creating a DLL please consult google.

Thankyou for the reply, but how do i do that. i am a beginner so could you give me instructions as in how to do it.

And is it possible to create the dll without creating a new project because i tried that and it didn't work. It would be great if i could modify the present C++ code to create this dll file.

help please ?

DLLs require Windows-specific declarations on each function you want to have accessible. As far as I know, these extra declarations do not impact your ability to access the functions if built into an .exe:

int someFunction(int someArgument)
{
   ...
}

becomes

extern "C" __declspec(dllexport) int someFunction(int someArgument)
...

Then to create the DLL in VisualStudio (if that's what you're using), I recommend starting a new project with the type-stuff pre-set-up to build a DLL, then import your same source file into the new project. There are probably ways to set up switches so that a single project builds everything you need, try poking around on msdn.com. Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.