Best practices when using C++ DLL Project interop with C#?
I've implemented some of my project in C++ and I call that code from C#. My current way of loading the DLL is to call the LoadLibrary (and then use GetProcAddress + the Delegate helper method to invoke methods safely from C#). But I have to hard code the DLL's path, allowing it to either exist in the bin folder, or backing out of that folder to the solution folder and into x64, etc., to the built DLL file.
How else can I load the DLL?
5 Replies
Is the c++ project in the same solution as the c# project you're loading it from?
In that case, MSBuild is very robust and there are many ways to configure your projects to automatically copy the dll from the c++ output to the c# output on build
It is yeah
you don't need to hardcode the path, you can load the dll from current/working directory
That's becomes the bin folder of the app
This is what I do now:
you could get the executing path for example from
System.Reflection.Assembly.GetExecutingAssembly().Location
or if you don't have any specific necessity you could just try omitting the path