{"id":526,"date":"2015-05-05T11:14:44","date_gmt":"2015-05-05T03:14:44","guid":{"rendered":"http:\/\/www.withonly.com\/?p=526"},"modified":"2015-05-05T11:14:44","modified_gmt":"2015-05-05T03:14:44","slug":"c%e5%92%8cc%e7%9b%b8%e4%ba%92%e8%b0%83%e7%94%a8dll","status":"publish","type":"post","link":"https:\/\/blog.5danyuan.com\/?p=526","title":{"rendered":"c++\u548cc#\u76f8\u4e92\u8c03\u7528dll"},"content":{"rendered":"<p>\u524d\u4e0d\u4e45\u660a\u95ee\u8d77\u6765\u5982\u4f55\u8ba9c++\u548cc#\u76f8\u4e92\u8c03\u7528dll\uff0c\u540e\u6765\u4e13\u95e8\u53bb\u641c\u7d22\u4e86\u4e0b\uff0c\u73b0\u5728\u8863\u670d\u81ea\u5df1\u6d17\u5206\u4eab\u51fa\u6765\uff0c\u53ef\u80fd\u5bf9\u5927\u5bb6\u6709\u5e2e\u52a9\u3002<br \/>\n\u9996\u5148\u6211\u4eec\u6765\u8bf4c#\u8c03\u7528c++\u7f16\u8bd1\u7684dll\u3002\u8fd9\u4e2a\u7f51\u4e0a\u5df2\u7ecf\u5f88\u591a\u4e86\uff0cc++\u91cc\u548c\u666e\u901a\u5199\u6cd5\u4e00\u6837\uff0c\u8bbe\u7f6e\u5bfc\u51fa\u51fd\u6570\u7b49\u7b49\uff0c\u4f8b\u5982c++\u91cc\u6709\u4e00\u4e2a\u51fd\u6570 int cplusplus(int a,int b)\uff0c\u90a3\u4e48\u5728C#\u91cc\uff0c\u53ef\u4ee5\u5148\u7533\u660e\uff1a<\/p>\n<blockquote><p>[DllImport (abc.dll)]<br \/>\npublic extern static int cplusplus(int a,int b);<\/p><\/blockquote>\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u50cf\u666e\u901a\u7684C#\u51fd\u6570\u4e00\u6837\u8c03\u7528\u4e86\u3002\u6709\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff0c<br \/>\n1\u3001DllImport \u540e\u62ec\u53f7\u91cc\u8f93\u5165c++ \u7684dll \u7684\u6587\u4ef6\u540d\uff0c\u5982\u679c\u8be5dll\u4e0d\u662f\u5728\u7cfb\u7edf\u76ee\u5f55\u6216\u8005\u73af\u5883\u53d8\u91cf\u7684\u76ee\u5f55\uff0c\u6216\u8005\u662fc#\u7a0b\u5e8f\u7684bin\u76ee\u5f55\u91cc\uff0c\u8fd9\u91cc\u5c31\u5e94\u8be5\u8f93\u5165\u5b8c\u6574\u7684\u8def\u5f84\u3002\u5efa\u8bae\u653e\u5230c#\u7684bin\u76ee\u5f55\u91cc\u3002<br \/>\n2\u3001\u51fd\u6570\u5fc5\u987b\u58f0\u660e\u4e3apublic\u7684\uff0c\u4e0d\u8981\u5fd8\u8bb0\u4e86extern\u5173\u952e\u5b57\uff0c\u5f53\u7136static\u4e5f\u6240\u5fc5\u9700\u7684\u3002<br \/>\n\u53cd\u8fc7\u6765\uff0cc++\u662f\u5982\u4f55\u8c03\u7528c#\u7684dll\u5462\uff0c\u4f8b\u5982c#\u91cc\u6709\u4e00\u4e2a\u51fd\u6570 public int csharp(int a,int b)\u5728\u7c7bPipe\u4e0b\uff0c\u800cPipe\u7c7b\u5c5e\u4e8eABC\u547d\u540d\u7a7a\u95f4\uff0c\u90a3\u4e48\u5bf9\u4e8ec++\u9879\u76ee\uff0c\u9996\u5148\u5e94\u8be5\u8bbe\u7f6e\u9879\u76ee\u5c5e\u6027\uff0c\u6253\u5f00\u516c\u5171\u8bed\u8a00\u8fd0\u884c\u65f6\u652f\u6301\uff0c\u5373\/clr\u6807\u8bb0\uff0c\u7136\u540ec++\u9879\u76ee\u4e2d\u6dfb\u52a0c#\u7684dll\u6587\u4ef6\u5f15\u7528\uff0c\u63a5\u4e0b\u6765\uff0c\u5728cpp\u6587\u4ef6\u91cc\u6dfb\u52a0c#\u5bf9\u5e94\u51fd\u6570\u7684\u547d\u540d\u7a7a\u95f4 using namespace ABC;\u6700\u540e\u5c31\u662f\u5728\u9700\u8981\u8c03\u7528\u7684\u5730\u65b9\uff0c\u4f7f\u7528\u6258\u7ba1\u4ee3\u7801\u7684\u65b9\u5f0f\u8c03\u7528\uff1a<\/p>\n<blockquote><p>Pipe ^p = gcnew Pipe();<br \/>\nint a = p.csharp(3,4);<\/p><\/blockquote>\n<p>\u7f51\u4e0a\u6709\u540c\u5b66\u63d0\u53ca\u7f16\u8bd1\u5931\u8d25\uff0c\u6709\u4e00\u79cd\u53ef\u80fd\u662f\u6ca1\u6709\u6dfb\u52a0dll\u7684\u5f15\u7528\uff0c\u5982\u679c\u4e0d\u559c\u6b22\u6dfb\u52a0\u5f15\u7528\uff0c\u90a3\u4e48\u5728cpp \u6587\u4ef6\u91cc\uff0c\u4f7f\u7528 #using &#8220;..\\Debug\\csharp.dll&#8221; \u4e5f\u662f\u4e00\u6837\u53ef\u4ee5\u7684\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u5c31\u662f\u7528using\uff0c\u800c\u975e include\u5173\u952e\u5b57\uff0c\u5f53\u7136\u7edd\u5bf9\u8def\u5f84\u4e5f\u662f\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<p>\u73b0\u5728c++\u548cc#\u76f8\u4e92\u8c03\u7528dll\u7684\u65b9\u5f0f\u5c31\u7b97\u5b8c\u6210\u4e86\uff0c\u6709\u7684\u540c\u5b66\u5bf9c++\u8c03\u7528c#\u7684dll\u7684\u65b9\u5f0f\u4e0d\u592a\u8ba4\u53ef\u3002\u4e3a\u4ec0\u4e48\u5462\uff0c\u56e0\u4e3a\u8981\u7528\u6258\u7ba1\u4ee3\u7801\u7684\u65b9\u5f0f\u3002\u5176\u5b9e\u4e0a\u9762\u63cf\u8ff0\u7684\u65b9\u6cd5\uff0c\u672c\u8d28\u4e0a\u662f\u4f7f\u7528\u7684COM\u8c03\u7528\u6765\u5b8c\u6210\u7684\uff0c\u5fae\u8f6f\u4e5f\u7ed9\u51fa\u4e86\u76f4\u63a5COM\u8c03\u7528\u7684\u793a\u4f8b\uff0c\u559c\u6b22\u8fd9\u79cd\u65b9\u5f0f\u7684\u540c\u5b66\u4e0d\u59a8\u7422\u78e8\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u4e0d\u4e45\u660a\u95ee\u8d77\u6765\u5982\u4f55\u8ba9c++\u548cc#\u76f8\u4e92\u8c03\u7528dll\uff0c\u540e\u6765\u4e13\u95e8\u53bb\u641c\u7d22\u4e86\u4e0b\uff0c\u73b0\u5728\u8863\u670d\u81ea\u5df1\u6d17\u5206\u4eab\u51fa\u6765\uff0c\u53ef\u80fd\u5bf9\u5927\u5bb6\u6709\u5e2e\u52a9\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[234,5],"tags":[120,122,235],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-c","category-daily","tag-c-sharp","tag-dll","tag-235"],"_links":{"self":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=526"}],"version-history":[{"count":0,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/526\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}