48

I've a c++ project. I admit that I'm a complete ZERO in c++. But still I need to write a c++.net wrapper so I could work with an unmanaged c++ library using it. So what I have: 1) unmanaged project's header files. 2) unmanaged project's libraries (.dll's and .lib's) 3) an empty C++.NET project which I plan to use as a wrapper for my c# application

How can I start? I don't even know how to set a reference to an unmanaged library.

S.O.S.

Evgeny007
  • 642
  • 2
  • 6
  • 8

2 Answers2

39

http://www.codeproject.com/KB/mcpp/quickcppcli.aspx#A8

This is general direction. You need to create C++/CLI Class Library project, add .NET class to it (StudentWrapper in this sample), create unmanaged class instance as managed class member, and wrap every unmanaged class function. Unmanaged library is added to C++/CLI project using linker dependencies list, and not as reference. In the Project - Properties - Linker open Additional Dependencies and add .lib name there.

Note: since we are talking about C++/CLI wrapper, no PInvoke! PInvoke is used to call exported functions (API), and not classes.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • 6
    C++/CLI is actually implicit P/Invoke, so it is still P/Invoke. http://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx, You can use P/Invoke to call C++ classes as well, just need to do some extra work. – xInterop Apr 23 '13 at 03:47
  • 2
    Here another great article on [codeproject (link)](http://www.codeproject.com/Articles/651516/Exposing-native-to-managed-Cplusplus-CLI-vs-P-Invo). Describes both P/Invoke and C++/CLI. Credit to Shmuel Zang. – Kay Zed Jul 07 '16 at 17:28
8

You need to use p/invoke from .NET to talk to your unmanaged DLL.

Essentially you create a function header for each function you want to call in your unmanaged DLL, and tell .NET which DLL the function lives in, then just call that function like any other in your .NET wrapper.

You shouldn't even need any C++ knowledge - as long as you know the function definition of the functions in your unmanaged DLL, and the correct datatypes.

ANeves
  • 6,219
  • 3
  • 39
  • 63
Andy Shellam
  • 15,403
  • 1
  • 27
  • 41