void sayHello(void)
{
printf("SayHello word!\n");
{
printf("SayHello word!\n");
}
; sayHello.def : Declares the module parameters for the DLL.
LIBRARY "sayHello"
DESCRIPTION 'sayHello Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
sayHello
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CallUnManDll
{
class Program
{
delegate bool doDllFunction();//如果需要执行的函数有参数,可对之进行声明。
static void Main(string[] args)
{//E:\learnProg\vc\sayHello\Release\sayHello.dll
//void sayHello(void)
DllInvoke dllInvoke = new DllInvoke(args[0]);//非托管dll文件路径
doDllFunction show = (doDllFunction) dllInvoke.Invoke(args[1],typeof(doDllFunction));// InitFunction为需要执行的函数名
bool ret=show();//执行方法,可根据定义决定是否需要传参数
}
}
public class DllInvoke
{
[DllImport("Kernel32.dll")]
private static extern IntPtr LoadLibrary(string path);
[DllImport("Kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr lib,string FunName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr lib);
private IntPtr libr;
public DllInvoke(string path)
{
libr = LoadLibrary(path);
}
public Delegate Invoke(string funName, Type type)
{
IntPtr api = GetProcAddress(libr, funName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, type);
}
~DllInvoke()
{
FreeLibrary(libr);//释放。必须的
}
}
}