In C ++, we can use loadlibrary and getprocaddress to dynamically call the DLL export function.
Can this method be used in C?
This is acceptable in DOTNET 2.0, thanks to a new function in 2.0.
Marshal. getdelegateforfunctionpointer Method
This method is added in. NET Framework 2.0.
Converts an unmanaged function pointer to a delegate.
The instance code is as follows:
Public Delegate int msgbox (INT hwnd, string MSG, string CPP, int OK );
[Dllimport ("Kernel32")]
Public static extern int getprocaddress (INT handle, string funcname );
[Dllimport ("Kernel32")]
Public static extern int loadlibrary (string funcname );
[Dllimport ("Kernel32")]
Public static extern int freelibrary (INT handle );
Private Static delegate getaddress (INT dllmodule, string functionname, type T)
{
Int ADDR = getprocaddress (dllmodule, functionname );
If (ADDR = 0)
Return NULL;
Else
Return marshal. getdelegateforfunctionpointer (New intptr (ADDR), t );
}
Private void button#click (Object sender, eventargs E)
{
Int huser32 = 0;
Huser32 = loadlibrary ("user32.dll ");
Msgbox mymsg = (msgbox) getaddress (huser32, "messageboxa", typeof (msgbox ));
Mymsg (this. Handle. toint32 (), txtmsg. Text, txttitle. Text, 64 );
Freelibrary (huser32 );
}