66681

Statically linking a C++ library to a C# process using CLI or any other way

Is it possible to take a static library written in C++ and somehow integrate it into a .NET process in a way that the .NET process will be integrated with the lib into one exe file? What I mean is that I know I can invoke a c++ DLL from within a C# process but the user will still have to have the C++ DLL, but is it possible to use a lib instead of a DLL?(this way the user won't even know that the exe uses my library). and if it is possible, how?

Answer1:

Unique among other managed languages, C++ allows for mixed mode - a combination of managed (C++/CLI) and native C++ within the same compilation unit (dll/exe/lib), with calls back and forth. Maybe you can leverage that, create a kind of a glue layer. I've never tried though.

The key is the /clr compiler switch - you apply it to some files in the project but not others. Then you create some classes/functions as managed. The unmanaged bits can see them and call them, and vice versa. Passing primitive types around is done transparently, for strings there's some marshaling trickery. I'll be able to post more on Monday.

EDIT: seems like some deep magic would be required. Not on the mixed C++ side - on the linking the result to C# side. The Visual Studio IDE does not readily support the scenario, you see. Chances are, the regular build process won't be of any use.

EDIT2: you can compile your C++ bits to a .netmodule by specifying a /LN command line option to the compiler and /NOASSEMBLY to a linker. Now, to link that to the C# exe...

Answer2:

I did something like that with visual 2008 (I don't know if it's still possible with newer versions of VC). So it IS possible. In my experience you can only mix native C++ with managed C++ though, not C#. To do so, I simply created a solution containing (in my case) several libraries written in native C++, create an exe project in managed C++ and added dependencies on the projects. You will need to write some wrapping managed C++ classes though as you cannot mix directly a native type in a managed class. (you will have some compilation error like "error C4368: cannot define 'nativeVar' as a member of managed 'ManagedClass': mixed types are not supported".

I don't know why you want to use .NET with your library, personally I wanted to use the .NET interface, and even if it worked for me, the mix has been a big pain in my back and if I had to do it again I would go with all native C++ with MFC, or GUI library.

Answer3:

No, this is wholly impossible. Mixed mode is banned in C++/CLI now- every bit of code is either wholly .NET or wholly native, and the only way to cross the boundary is through P/Invoke and a DLL.

Recommend

  • How do I get a managed bean inside an unmanaged object?
  • How does Dispose work with Entity Framework
  • Converting inout values to UnsafeMutablePointer
  • Redirect website to mobile site and from mobile to desktop?
  • Getting contents of square brackets, avoiding nested brackets
  • SwipeListView in Xamarin Form
  • Why do native C++ projects have a TargetFrameworkVersion?
  • When is locking on types a good idea?
  • How to share a database between ASP.NET MVC 5 application and .NET console application?
  • Does SmartGit support git-svn?
  • Can a variable be stored within an image or div tag?
  • Hibernate: Inheritance and relationship mapping + generics
  • JavaScriptCore External Arrays
  • How to check if a string contains any string of a column in MySQL and vice versa?
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Get the number 18437736874454810627
  • C#: Import/Export Settings into/from a File
  • How to use function wrapper in mustache.php?
  • WPF Template Binding in ToggleButton UserControl
  • DIV instruction jumping to random location?
  • Cast between interfaces whose interface signatures are same
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Z3: Convert between FP and BitVector?
  • How to get Eclipse Oxygen to run on Java 9
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Why winpcap requires both .lib and .dll to run?
  • python regex in pyparsing
  • C# - Getting references of reference
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?