1
votes

I have native C++ dlls that I need to use in our group's new C# programs. I wanna create C++/CLI wrappers for native C++ dlls, so as to be able to import and compile them in C#.

What I need to know is how to load the dll file in the wrapper source file at the first place. Exactly the same thing that #using <...> does for MSIL, but for loading native C++ dlls.

And also, how will the objects and namespaces inside the dll become accessible then (synatically)?

EDIT:

I use

class __declspec(dllexport) radar
{
    // declarations and definitions
    ...
};

to export my radar class from my radar.dll, and need to import it in a VC++(CLI) program like this: __declspec(dllimport) public class radar; so as to conduct it's definition into C#, but definitely I can't see radar in C#, because its not defined public in VC++(CLI). Even when I wanna use native dll's objects in VC++ dll's code, like radar pos1(); I get:

error C2512: 'radar' : no appropriate default constructor available

How can do this, having just the radar declaration at hand?

END EDIT.

Thank you!

1
Why do you need to create a C++/CLI wrapper? is there a reason not to just call the native DLL's using PInvoke from C#? msdn.microsoft.com/en-us/library/aa446536.aspx - PeskyGnat

1 Answers

3
votes

It is automatic, you don't do this yourself. You specify the native DLL import libraries (.lib) in the linker settings for the C++/CLI project, Additional Dependencies setting. The linker links in a reference to the DLL name into the C++/CLI assembly. As soon as the C# code uses any of the managed types in the C++/CLI assembly, the CLR loads the assembly. Windows notices the native DLL references in the DLL and automatically loads them.

This can only come to a good end if Windows can actually find the native DLLs at runtime. Copy them into the build folder of the EXE project. That's awkward, you can do it with a post-build event or by adding them to the C# project with their Copy to Output Directory property set to true.