c++のdllを動的に呼び出す

6427 ワード

            ,      ,        。
[
DllImport(
"Kernel32.dll")
]
public static extern int LoadLibrary(String funcname);

[
DllImport(
"Kernel32.dll")
]
public static extern int GetProcAddress(int handle, String funcname);

[
DllImport(
"Kernel32.dll")
]
public static extern int FreeLibrary(int handle);

//
// : (test) ..
//
public delegate int Invoker(string ProjName, string SchemeName, string Mile, ref double ContnMile, ref bool Reliability, ref short LineKind);

static void Main(string[] args)
{
int handle = LoadLibrary(@"test.dll"); // ..

if (handle != 0)
{
int address = GetProcAddress(handle, "test"); // ..

if (address != 0)
{
Invoker invoker
= (Invoker)Marshal.GetDelegateForFunctionPointer(new IntPtr(address), typeof(Invoker));

//use invoker -> demo: invoker(...);

FreeLibrary(handle);
}
}
}