View Full Version : using existing c++ functions in c#


E-Male
27th September 2005, 20:12
sorry if this is an other stupid question

i'm doing some c# at the moment and have only 1 real problem

most open-source material i can use is c++ (or some pure c)

in a c++ project u could just use #include and call the functions (simplified)

i know there is a command to use c in c++, but i'm not sure how to do it with c++ in c#

stax76
28th September 2005, 04:45
It's not a stupid question since advanced interop requires deep knowledge in both worlds, managed and unmanaged. Everything can be found in the MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp

I've got some experience so feel free to show some code.

Doom9
28th September 2005, 07:34
good link. Using COM components is probably the easiest, using DLLs a tad bit harder (the difficulty stems from creating the proper structs to exchange data mostly, and all the pointer hooplah). You may find a couple interesting uses of dlls in the MeGUI source code (most notably I'm wrapping around AVIFile, and DGDecode).

Doom9
28th September 2005, 18:43
an article on Interop was just posted on MSDN: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/

E-Male
28th September 2005, 20:42
thx guys

i didn't think that just using a function would get so difficult

i think i'll just compile the c++ parts seperately (to exe) and call them from the c# app with their CLI (that way the batch guys can also get some CLI stuff to play with)

just in case i get back to the idea of combining both:
can i use a static lib instead of a dll? so everything is in the one exe

stax76
28th September 2005, 21:13
Something like this can be done:

http://blogs.msdn.com/junfeng/archive/2005/05/19/420186.aspx

That's what I meant with advanced. You can downoad VC++ 2005 Express for free btw. It supports pure, mixed or native output, I wrote some mixed C++/CLR code for my DVB project a while ago and was quite impressed by the possibilities of C++/CLR. The other MS languages, the IDE and .NET made huge progress as well.