在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
这两天忙着把框架改为支持加载C++和Delphi的插件,来不及更新blog了。
1 public class LoadDll
2 { 3 #region Win32 API : Load dll 4 [DllImport("kernel32.dll")] 5 public static extern IntPtr LoadLibrary(string path); 6 7 [DllImport("kernel32.dll")] 8 public static extern IntPtr GetProcAddress(IntPtr lib, string funcName); 9 10 [DllImport("kernel32.dll")] 11 public static extern bool FreeLibrary(IntPtr lib); 12 13 [DllImport("kernel32.dll")] 14 public static extern IntPtr GetStdHandle(int nStdHandle); 15 16 [DllImport("user32", EntryPoint = "CallWindowProc")] 17 public static extern int CallWindowProc(IntPtr lpPreWndFunc, int hwnd, int msg, int wParam, int lParam); 18 #endregion 19 20 private IntPtr _dllLib; 21 22 /// <summary> 23 /// Initializes a new instance of the <see cref="LoadDll"/> class. 24 /// </summary> 25 public LoadDll() 26 { 27 28 } 29 30 /// <summary> 31 /// Initializes a new instance of the <see cref="LoadDll"/> class. 32 /// </summary> 33 /// <param name="path">The path.</param> 34 public LoadDll(string path) 35 { 36 _dllLib = LoadLibrary(path); 37 } 38 39 /// <summary> 40 /// 注销对象时释放资源 41 /// <see cref="LoadDll"/> is reclaimed by garbage collection. 42 /// </summary> 43 ~LoadDll() 44 { 45 FreeLibrary(_dllLib); 46 } 47 48 /// <summary> 49 /// 初始化dll的路径 50 /// </summary> 51 /// <param name="path">The path.</param> 52 public void InitPath(string path) 53 { 54 if (_dllLib == IntPtr.Zero) 55 _dllLib = LoadLibrary(path); 56 } 57 58 /// <summary> 59 /// 根据非托管的dll中的方法名称映射成托管的委托类型 60 /// </summary> 61 /// <param name="methodName">非托管的dll中的方法名称</param> 62 /// <param name="methodType">托管的委托类型</param> 63 /// <returns></returns> 64 public Delegate InvokeMethod(string methodName, Type methodType) 65 { 66 //获取非托管的dll中方法的地址 67 var methodPtr = GetProcAddress(_dllLib, methodName); 68 //将非托管的方法转换为委托 69 return Marshal.GetDelegateForFunctionPointer(methodPtr, methodType); 70 } 71 }
调用:
1 loadDll = new LoadDll(path);
2 stop = (StopHandler)loadDll.InvokeMethod("stop", typeof(StopHandler)); 3 start = (StartHandler)loadDll.InvokeMethod("start", typeof(StartHandler)); 4 init = (InitHandler)loadDll.InvokeMethod("init", typeof(InitHandler)); 5 query = (QueryHandler)loadDll.InvokeMethod("query", typeof(QueryHandler)); 6 setDatabaseInfo = (SetDatabaseInfoHandler)loadDll.InvokeMethod("setDatabaseInfo", typeof(SetDatabaseInfoHandler)); 7 setMonitorInfo = (SetMonitorInfoHandler)loadDll.InvokeMethod("setMonitorInfo", typeof(SetMonitorInfoHandler));
1 /// 插件导出方法
2 extern "C" __declspec(dllexport) void setDatabaseInfo(LPCTSTR dbServer, LPCTSTR dbUser, LPCTSTR dbPass, LPCTSTR dbName); 3 extern "C" __declspec(dllexport) void setMonitorInfo(LPCTSTR _agentBm, LPCTSTR _com); 4 extern "C" __declspec(dllexport) void init(); 5 extern "C" __declspec(dllexport) void start(); 6 extern "C" __declspec(dllexport) void stop(); 7 extern "C" __declspec(dllexport) LPCTSTR query(LPCTSTR devname, LPCTSTR id);
C#中的委托
1 ///<summary>
2 /// 处理Stop事件 3 ///</summary> 4 publicdelegatevoid StopHandler(); 5 ///<summary> 6 /// 处理Start事件 7 ///</summary> 8 publicdelegatevoid StartHandler(); 9 ///<summary> 10 /// 处理Init事件 11 ///</summary> 12 publicdelegatevoid InitHandler(); 13 ///<summary> 14 /// 处理Query事件 15 ///</summary> 16 publicdelegatestring QueryHandler(string devName, string id); 17 ///<summary> 18 /// 处理SetDataBaseInfo事件 19 ///</summary> 20 publicdelegatevoid SetDatabaseInfoHandler(string server,string user,string password,string dbName); 21 ///<summary> 22 /// 处理SetMonitorInfo事件 23 ///</summary> 24 publicdelegatevoid SetMonitorInfoHandler(string agentBm, string com); 接下来怎么搞,你们都懂的 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论