{"id":1724,"date":"2019-07-29T14:02:48","date_gmt":"2019-07-29T06:02:48","guid":{"rendered":"https:\/\/www.xiaobo.li\/?p=1724"},"modified":"2019-07-29T14:05:39","modified_gmt":"2019-07-29T06:05:39","slug":"c-%e8%b0%83%e7%94%a8cdll%e4%bc%a0%e9%80%92%e6%8c%87%e5%90%91%e6%8c%87%e9%92%88%e7%9a%84%e6%8c%87%e9%92%88%e5%8f%82%e6%95%b0%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/1724","title":{"rendered":"C# \u8c03\u7528C++DLL\u4f20\u9012\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u53c2\u6570\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++\u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct DeviceInfo\n{    \n    char szDeviceName[DEVICE_NAME_LEN];\n    char szMACAddress[MAC_ADDRESS_LEN];        \n    char szDeviceIP[DEVICE_IP_LEN];\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[StructLayout(LayoutKind.Sequential)]\npublic struct DeviceInfo\n{\n    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]\n    public string szDeviceName;\n\n    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]\n    public string szMACAddress;\n\n    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]\n    public string szDeviceIP; \n\n\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u60c5\u51b51\uff1aC++\u7684dll\u8d1f\u8d23\u5206\u914d\u5185\u5b58<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u5bfc\u51fa\u51fd\u6570\u7684\u58f0\u660e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#define DLL_API  extern \"C\"  __declspec(dllexport)\nDLL_API int findAllDevices(DeviceInfo** ppDeviceInfoList,int * pCount);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#\u5bfc\u5165\u51fd\u6570\u7684\u58f0\u660e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[DllImport(\"IPAlter_d.dll\")]\npublic extern static int findAllDevices(out IntPtr pDeviceInfo, ref int pCount);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#\u7684\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">IntPtr pBuff = new IntPtr();\/\/\u76f4\u63a5new\u4e00\u4e2a\u53c2\u6570\u5373\u53ef\n if (IPAlter.findAllDevices(out pBuff, ref cout) != 1)\n {\n     System.Console.WriteLine(\"\u641c\u7d22\u5931\u8d25\uff01\");\n     System.Console.ReadLine();\n     return;\n }\n for (int i = 0; i &lt; cout; i++)\n {\nIntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(DeviceInfo)) * i);\n\nSystem.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szDeviceName); \n System.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szDeviceIP); \n System.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szMACAddress);\n \n }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u60c5\u51b52\uff1aC#\u8d1f\u8d23\u5206\u914d\u5185\u5b58<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u5bfc\u51fa\u51fd\u6570\u7684\u58f0\u660e:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DLL_API int findAllDevice(DeviceInfo* ppDeviceInfoList,int * pCount);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;C#\u5bfc\u5165\u51fd\u6570\u7684\u58f0\u660e:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[DllImport(\"IPAlter_d.dll\")]\npublic extern static int findAllDevice(IntPtr pDeviceInfo, ref int pCount);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;C#\u7684\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DeviceInfo[] DeviceInfoList = new DeviceInfo[50];\n int size = Marshal.SizeOf(typeof(DeviceInfo)) * 50;\n byte[] bytes = new byte[size];\n IntPtr pBuff = Marshal.AllocHGlobal(size);\n if (IPAlter.findAllDevice(pBuff, ref cout) != 1)\n {\n     System.Console.WriteLine(\"\u641c\u7d22\u5931\u8d25\uff01\");\n     System.Console.ReadLine();\n     return;\n }\n for (int i = 0; i &lt; cout; i++)\n {\n\nIntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(DeviceInfo)) * i);\n DeviceInfoList[i] = (DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo));\n System.Console.WriteLine(DeviceInfoList[i].szDeviceName);\n System.Console.WriteLine(DeviceInfoList[i].szDeviceIP);\n System.Console.WriteLine(DeviceInfoList[i].szMACAddress);\n\n<code class=\"hljs javascript\"><\/code>\n }\n Marshal.FreeHGlobal(pBuff);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/www.cnblogs.com\/cxwx\/archive\/2010\/12\/29\/1921140.html\">http:\/\/www.cnblogs.com\/cxwx\/archive\/2010\/12\/29\/1921140.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/hi.baidu.com\/fanr520\/item\/e761f9ca0766d462f6c95d55\">http:\/\/hi.baidu.com\/fanr520\/item\/e761f9ca0766d462f6c95d55<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/blog.csdn.net\/wangweitingaabbcc\/article\/details\/7663949\">http:\/\/blog.csdn.net\/wangweitingaabbcc\/article\/details\/7663949<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++\u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a struct DeviceInfo { char szDev &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/1724\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299],"tags":[],"class_list":["post-1724","post","type-post","status-publish","format-standard","hentry","category-csharpcode"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=1724"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1724\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}