locked
Creating a shared library with both C# and native C++ code in it RRS feed

  • Question

  • I'm trying to build a shared library that can be called by both C# and C++, and contains code from both a C# project and a C++ project. The library will be linked at runtime by a C++ program and a C# program, and the C# and C++ code in it does not need to interoperate (although that would be a plus, it is not necessary). I just want a single library that works like the C# library when the C# program tries to use it, and the C++ library when the C++ program tries to use it.

    The C++ code calls code that has C linkage. I've tried using the MSVC linker to combine a netmodule and a C++ object file, but the resulting library is not callable from C# (the functions with C linkage still work with the C++ program).

    • Moved by Amanda Zhu Monday, May 6, 2013 7:13 AM not VS general forum issues and not sure which appropriate forum this thread can be moved to
    Thursday, May 2, 2013 8:12 PM

Answers

All replies

  • There isn't a way (using VS) to build two projects into one assembly.  You would need to make this a single C++/CLI project, and port the C# code to C++/CLI.  That would provide the potential for having a single library usable from both languages.

    Otherwise, you can build the C++ lib using C++ and the C# lib using C#, and distribute 2 assemblies - and one can use types from the other (if you're using C++/CLI or P/Invoke), but not have both languages in one library.



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 2, 2013 8:18 PM
  • Otherwise, you can build the C++ lib using C++ and the C# lib using C#, and distribute 2 assemblies - and one can use types from the other (if you're using C++/CLI or P/Invoke), but not have both languages in one library.

    Well, I don't need the code from C++ to use anything from C#, or the other way around.

    And I thought C++/CLI couldn't have C linkage?

    Thursday, May 2, 2013 8:24 PM
  • Well, I don't need the code from C++ to use anything from C#, or the other way around.


    Then just make two libraries.. far simpler.

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 2, 2013 10:01 PM
  • I'm aware it's much simpler that way. This is mostly an aesthetic goal. The functions of the two libraries are analagous in the two programs, and the two programs are related (and editor in C# and an engine in native C++).
    Thursday, May 2, 2013 10:30 PM
  • I'm aware it's much simpler that way. This is mostly an aesthetic goal. The functions of the two libraries are analagous in the two programs, and the two programs are related (and editor in C# and an engine in native C++).

    If your engine is a native C++ engine, you're better off keeping them separate.  Otherwise, you'll end up "bringing in" the .NET framework for no reason... (since an assembly using .NET will force it to get initialized).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 2, 2013 10:59 PM
  • Hi,

    You can try to write your DLL code in this definition way:

    Build your DLL with extern “C” to let the compiler enable C style linkage and __stdcall calling convention. In that way, the C# code can use the native C++ DLL with extern static P/Invoke: [DllImport(“ … ”)].

    Have a nice day.

    Regards,


    Elegentin Xie
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 6, 2013 7:24 AM
  • But will the C# project be able to call the C# code in the DLL? I'm not trying to call any C++ code from C#, or vice versa.

    FJ

    Sunday, May 12, 2013 10:30 PM
  • For C# issues you can ask the experts over here.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads

     

     

     


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows]

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    Sunday, May 12, 2013 10:50 PM