Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Namespace webservice1
{
/// <Summary>
/// Summary of dllinvoke
/// </Summary>
Public class dllinvoke
{
[Dllimport ("kernel32.dll")]
Private extern static intptr loadlibrary (string path );
[Dllimport ("kernel32.dll")]
Private extern static intptr getprocaddress (intptr Lib, string funcname );
[Dllimport ("kernel32.dll")]
Private extern static bool freelibrary (intptr Lib );
Private intptr hlib;
Public dllinvoke (string dllpath)
{
Hlib = loadlibrary (dllpath );
}
~ Dllinvoke ()
{
Freelibrary (hlib );
}
// Convert the function to be executed to the Delegate
Public Delegate invoke (string apiname, type T)
{
Intptr API = getprocaddress (hlib, apiname );
Return (delegate) Marshal. getdelegateforfunctionpointer (API, t );
}
}
}
The main function of the above class is to dynamically find the hosted DLL function to be loaded through the API, and return the proxy (pointer) of the function. It seems that C ++ can also load it like this, next time, experiment in C ++
BelowCodeYes:
Public class test
{
Public Delegate int setdbconfig (string IPaddress, string dbname, string username, string psw); // declare the method through delegation
Dllinvoke DLL;
Setdbconfig;
Public Test
{
DLL = new dllinvoke (server. mappath (@"~ /Bin/dllagain. dll "); // instantiate the DLL
Setdbconfig = (setdbconfig) DLL. Invoke ("setdbconfig", typeof (setdbconfig ));
}
Public static void main ()
{
Setdbconfig (string IPaddress, string dbname, string username, string psw); // call this operation
}
}
From: http://zhouweigang01.blog.163.com/blog/static/934090720095492458949/