Reputation: 21927
For versioning reasons it would be beneficial for me to be able to use p/invoke on a native C++ dll whose filename will be determined at runtime, as such:
[DllImport(myDllFilename)]
private static extern void MyInvokedMethod();
Unfortunately [DllImport]
is an attribute and demands a constant expression. Are there any other options for getting around this error?
I am aware I can use identically named files in different folders and load the right one at runtime, but being able to use differently named files would be ideal for my case.
Upvotes: 2
Views: 540
Reputation: 613053
There is no workaround. You will need to use LoadLibrary and GetProcAddress. That's really no fun at all so I'd try to avoid going that way if feasible.
Upvotes: 3
Reputation: 70369
To achieve what you want you need to PInvoke several things (LoadLibrary
and GetProcAddress
- see links below)... it is called "late binding native code"... using this is not easy/recommended but it is possible...
Some relevant links with explanations, samples and source code:
Upvotes: 1