I don’t know how to explain this but I will try. Here is what I’m using :
C++ :
extern "C" __declspec(dllexport) void c(char path[])
{
//some code with the path.
}
C#:
[DllImport("DLL")]
static extern void c(char[] path);
As you can see I’m using an exported function from c++.
My question is, is there an easier way to get the path of the application from the DLL without passing it as a parameter to the exported function ?
This is really easy, but takes some forethought:
The first thing to do is implement a
DllMainin the native DLL, which will cache the module’s handle when the DLL loads. That looks like:Later, when you want to get the name, just call
GetModuleFileName, like so:The only complicated part, if you want to call it that, is storing the handle from DllMain. Calling
GetModuleHandle(NULL)will give you the executing module’s handle, not the DLL (same asGetExecutingAssemblyin C#).