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.
InstanceCodeAs 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 );
}