Mixing C++ and C# within program

This is a discussion on Mixing C++ and C# within program within the C# Programming forums, part of the General Programming Boards category; Hi all, I'm assuming you can mix C# and C++ without too much fuss using VS? Since I'm more experienced ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Mixing C++ and C# within program

    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
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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...)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    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].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21