Mixing C++ and C# within program [Archive] - C Board

PDA

View Full Version : Mixing C++ and C# within program


dudeomanodude
04-30-2008, 11:15 AM
Hi all,

I'm assuming you can mix C# and C++ without too much fuss using VS? Since I'm more experienced w/C++ I'd like to use C# to interface with SQL Server, take the info that I need from that and then use my more familiar C++ techniques to actually process this information, then shove the info back in our server again with C#.

Is this reasonable?

Thanks in advance,

Eric

Magos
04-30-2008, 12:05 PM
Not in the same project (as far as I know), however you can create class libraries to link to your main executable which are written in any .NET language (C#, VB.NET, C++.NET etc...)

dudeomanodude
04-30-2008, 06:56 PM
I found this in another forum:

You can't mix C++ and C# in the same Visual Studio project, but you can make a class library in one language, then reference that project / class library in a different project using the other language.

I realize this is pretty much what you said Magos, I'm just not experienced with writing DLL's so can someone offer me a little more info on this process?

Elysia
05-01-2008, 03:46 AM
In Visual Studio, it's a cinch.
All you need to do is write your dll as usual. To call functions in the dll, you need to export those functions. To do that, you simply add __declspec(dllexport) to the function to be exported (and __declspec(dllimport) in the other project to import them, but this may not apply to C#). This is usually best accomplished with a macro in the header.
Another way is to use a .def file. Put the names of the function to export under the EXPORTS section. Then you can call the functions as normal if the compiler can see the headers.

The export process is native, which is what you need I think since it's going to be C++. As for importing under C#, that's beyond my knowledge, however.

Magos
05-01-2008, 05:42 AM
Assuming you have Visual Studio it's incredibly easy to create and link DLL's.

To create DLL:
1) Create a new project, just select "Class Library" instead of "Window Forms Application" or "Console Application"
2) Create classes just as you normally would, nothing special needs to be done

To link to DLL:
1) In your project, go to "Project" -> "Add Reference"
2) Select "Browse" and find your previously compiled DLL
3) Click "Ok" and you're done. All DLL classes will be available as if they were created in your main project

If you modify your DLL Visual Studio will detect this and update your references automatically.
Note that these are class library DLL's, only usable in .NET languages.

dudeomanodude
05-01-2008, 08:46 AM
Thank you both,

It sounds simpler than I thought.

Now if the library is going to be C++, does it make a difference if it's managed C++ or not?

Magos
05-01-2008, 09:41 AM
Of course you need to use C++.NET (managed) for a .NET class library, in the interface that is. Internally you can use whatever C++ you like (which is very useful to port old C/C++ libs as a .NET wrapper).

Elysia
05-01-2008, 09:45 AM
It is possible to use native dlls within dotNet too, I believe (and they do not require registering in the registry!), but whichever way you do, you still need some kind of interop. I'd still go for native since it doesn't require any registry crap (dll hell?) [do you know how easy it is to screw up such applications by removing just one registry entry?] [It also makes it more difficult to remove with an uninstall monitor program].