Home All Groups Group Topic Archive Search About

Dynamic Native Loadlib

Author
13 Oct 2006 6:53 PM
Mystagogue
I understand that the means of explicitly linking against native code, is
with an attribute set like this:

[System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint =
"PlaySound", SetLastError = true)]

But what if you want the name of the DLL to be dynamic - that is - specified
with a variable rather than a string literal?  The above code doesn't compile
if I insert a variable in lieu of the string literal for the DLL name.  Is
there a way to do that (perhaps with a different FCL service)?

If it can't be done, I suppose the reason would be that the compiler wants
to collect in advance some type info from the named native DLL, so that it
can construct proper marshalling code.  But who knows...

Author
13 Oct 2006 7:14 PM
Eugene Mayevski
Hello!
You wrote  on Fri, 13 Oct 2006 11:53:01 -0700:

M> But what if you want the name of the DLL to be dynamic - that is -
M> specified with a variable rather than a string literal?  The above code

This (dynamic loading of the dll with the name defined in runtime) is
supported only in .NET 2.0. You can make some kind of proxy native DLL if
you need. Such DLL would load the desired DLL itself.

With best regards,
Eugene Mayevski
http://www.SecureBlackbox.com - the comprehensive component suite for
network security
Author
13 Oct 2006 8:08 PM
Mystagogue
You said:

> This (dynamic loading of the dll with the name defined in runtime) is
> supported only in .NET 2.0.

I am using .NET 2.0. (I have Visual Studio 2005 Team System).  Could you
tell me what syntax or semantics adjustment I must make to use this feature? 
As I mentioned before, it does not compile if I use a variable.

> You can make some kind of proxy native DLL if
> you need. Such DLL would load the desired DLL itself.

Yes, this is true.  But if, as you suggest, .NET 2.0 is **inherently**
capable of doing this - then please do share how its done.
Author
13 Oct 2006 8:36 PM
Mattias Sjögren
>I am using .NET 2.0. (I have Visual Studio 2005 Team System).  Could you
>tell me what syntax or semantics adjustment I must make to use this feature? 
>As I mentioned before, it does not compile if I use a variable.

You can call the native LoadLibrary and GetProcAddress APIs, and then
wrap the function pointer in a delegate with
Marshal.GetDelegateForFunctionPointer.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
14 Oct 2006 7:04 AM
Eugene Mayevski
Hello!
You wrote  on Fri, 13 Oct 2006 13:08:02 -0700:

M> I am using .NET 2.0. (I have Visual Studio 2005 Team System).  Could you
M> tell me what syntax or semantics adjustment I must make to use this
M> feature?  As I mentioned before, it does not compile if I use a
M> variable.

Right, there's another syntax there. Unfortunately I don't remember, which
one exactly, cause we went a proxy route.

With best regards,
Eugene Mayevski
http://www.SecureBlackbox.com - the comprehensive component suite for
network security

AddThis Social Bookmark Button