Loading dlls dynamically

C

Claire

How do I load and run dll functions dynamically please? The dll may be
anywhere and its location is set in a command line parameter.

I've loaded the dll OK, done the equivalent of GetProcAddress

// kernel32, not used dynamically as it's fixed location.
[DllImport("kernel32.dll")]
internal static extern IntPtr LoadLibrary(String dllname);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetProcAddress(IntPtr hModule, String
procname);

// DynamicLibName is path/name of a dll on file system.
IntPtr Handle = LoadLibrary(DynamicLibName);
IntPtr GetError = GetProcAddress(Handle, "GetError");

If the function declaration for GetError is "int GetError(int ErrorNum" how
do I move on from the above to "return GetError(3);"

From old Delphi days I realize I must declare some kind of type, then link
it to the "GetError" procedure address. Im stuck on this bit.

I was following an example on a microsoft blog, they gave examples for a new
feature in the next version of dotnet - no good for my version. Their link
to a faq doing the same with version 1.1 required a userid and password, so
I was stuck.
Code or a link to a web site would be appreciated as I probably won't
understand a general overview of the process
 
R

RCS

I'm not sure exactly what you are saying.

In order to compile, you'll need to have an available version of a library
(except maybe when you use extern?)

But assuming that is the case, but you want to swap out the version at
runtime, I believe System.Reflection is going to be the only way. Something
like System.Reflection.Assembly.Load()
 
J

John Puopolo

Hi...

In essence, you are loading a module and getting a pointer to the function
you want to call. Hmmm, in .NET, a pointer to a function is delegate. Try
creating a delegate for the function you want to call and then call the
function through the delegate.

John Puopolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top