{"id":137564,"date":"2020-07-14T09:23:18","date_gmt":"2020-07-14T01:23:18","guid":{"rendered":"http:\/\/4563.org\/?p=137564"},"modified":"2020-07-14T09:23:18","modified_gmt":"2020-07-14T01:23:18","slug":"nodejs-%e4%bd%bf%e7%94%a8-node-ffi-%e8%b0%83%e7%94%a8-dll-%e7%9a%84%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=137564","title":{"rendered":"Nodejs \u4f7f\u7528 node-ffi \u8c03\u7528 DLL \u7684\u64cd\u4f5c"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Nodejs \u4f7f\u7528 node-ffi \u8c03\u7528 DLL \u7684\u64cd\u4f5c               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yangheng4922 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>node \u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u539f\u751f\u6a21\u5757 \u6298\u817e\u4e86\u597d\u4e45<\/p>\n<p>\u73af\u5883\u4ec0\u4e48\u7684\u641e\u4e86\u5927\u534a\u5929 \u5f04\u597d\u4e86<\/p>\n<p>\u53ef\u4ee5\u8c03\u7528 DLL \u7684\u65b9\u6cd5 \u8fd0\u884c<\/p>\n<p>\u4f46\u662f\u53d1\u73b0 \u6709\u4e00\u4e2a DLL \u66b4\u9732\u7684\u662f\u4e00\u4e2a Class \u4e0d\u77e5\u9053\u8981\u600e\u4e48\u8c03\u7528<\/p>\n<p>\u4eca\u5929\u67e5\u4e86\u597d\u4e45\u8d44\u6599<\/p>\n<p>\u6b63\u5e38\u5bfc\u51fa\u65b9\u6cd5\u7684 DLL \u5934\u6587\u4ef6\u662f\u8fd9\u6837\u7684<\/p>\n<pre><code>extern \"C\" int __declspec(dllexport)My_Test(char *a, int b, int c); extern \"C\" void __declspec(dllexport)My_Hello(char *a, int b, int c); <\/code><\/pre>\n<p>nodejs \u7684\u8c03\u7528\u65b9\u6cd5<\/p>\n<pre><code>const dll = ffi.Library( '.\/test.dll', {     \/\/ My_Test \u662f dll \u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u4e24\u8005\u540d\u79f0\u9700\u8981\u4e00\u81f4     \/\/ [a, [b\uff0cc....]] a \u662f\u51fd\u6570\u51fa\u53c2\u7c7b\u578b\uff0c[b\uff0cc]\u662f dll \u51fd\u6570\u7684\u5165\u53c2\u7c7b\u578b     My_Test: ['int', ['string', 'int', 'int']], \/\/ \u53ef\u4ee5\u7528\u6587\u672c\u8868\u793a\u7c7b\u578b     My_Hello: [ref.types.void, ['string', ref.types.int, ref.types.int]] \/\/ \u66f4\u63a8\u8350\u7528`ref.types.xx`\u8868\u793a\u7c7b\u578b\uff0c\u65b9\u4fbf\u7c7b\u578b\u68c0\u67e5\uff0c`char*`\u7684\u7279\u6b8a\u7f29\u5199\u4e0b\u6587\u4f1a\u8bf4\u660e }) <\/code><\/pre>\n<p>\u4f46\u662f\u6211\u54ea\u4e00\u4e2a\u6a21\u5757\u5bfc\u51fa\u7684\u662f\u4e00\u4e2a class \u4e0d\u77e5\u9053\u8981\u600e\u4e48\u53bb\u8c03\u7528\u5b83<\/p>\n<p><strong>class __declspec(dllexport) Person<\/strong><\/p>\n<pre><code>#pragma once   class  __declspec(dllexport) Person { private:  int age; public:  int getAge();  void setAge(int a); }; <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2586489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u529e\u6cd5\u8c03\u7528\uff0c\u56e0\u4e3a C++ \u6ca1\u6709\u4e00\u4e2a\u7edf\u4e00\u7684 ABI\uff0c\u4e0d\u540c\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fa\u6765\u7684\u90fd\u4e0d\u80fd\u4e92\u76f8\u8c03\u7528\uff0c\u751a\u81f3\u540c\u4e00\u4e2a\u7f16\u8bd1\u5668\u4e0d\u540c\u7248\u672c\u4e5f\u4e0d\u4e00\u5b9a\u517c\u5bb9\u3002<br \/>\u89e3\u51b3\u65b9\u6cd5\u662f\u7528 C \u8bed\u8a00\u7684\u63a5\u53e3\u5c01\u88c5\u4e00\u904d\u3002\u6bd4\u5982 cef https:\/\/bitbucket.org\/chromiumembedded\/cef \u5c31\u662f\u8fd9\u4e48\u505a\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : VDimos <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u662f\u60f3\u5bfc\u51fa\u8fd9\u4e2a\u7c7b\u6765\u7ed9 node \u4f7f\u7528\uff1f<br \/>\u7f16\u8bd1\u6210 DLL \u4e4b\u540e\uff0c\u7c7b\u8fd9\u4e2a\u6982\u5ff5\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002<br \/>FFI \u662f\u7528\u6765\u8c03\u7528\u51fd\u6570\u7684\uff0c\u5728 linux \u5c31\u662f\u7528\u4e86 dlopen \u548c dlsym \u8fd9\u4e9b\u5c01\u88c5\u7684\uff0cwin \u4e0b\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u672c\u8d28\u4e0a\u90fd\u662f\u5728 so \u6216\u8005 dll \u6587\u4ef6\u91cc\u67e5\u627e\u52a8\u6001\u7b26\u53f7\u8868\uff0c\u6240\u4ee5\u4f60\u60f3\u8981\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u53ea\u6709\u518d\u5bfc\u51fa\u4e00\u4e2a c \u6807\u51c6\u7684\u51fd\u6570\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u5b9e\u73b0\u4f60\u60f3\u505a\u7684\u8981\u7528\u5230\u8fd9\u4e2a\u7c7b\u7684\u64cd\u4f5c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yangheng4922 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ysc3839 #1 <br \/>@VDimos #2 <br \/>\u610f\u601d\u662f\u6ca1\u529e\u6cd5\u8c03\u7528 C++\u7684\u7c7b \u9700\u8981\u8fd0\u884c\u7684\u8bdd\u53ea\u80fd\u8ba9 C++\u7a0b\u5e8f\u5458\u5c01\u88c5\u5c42\u51fd\u6570\u6765\u8c03\u7528\u4e48                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586492\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yangheng4922 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @VDimos #2 \u4e0d\u662f\u6211\u60f3\u5bfc\u51fa\u7c7b \u8fd9\u4e2a DLL \u662f\u516c\u53f8 c++\u53d1\u7ed9\u6211\u8c03\u8bd5\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586493\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : VDimos <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yangheng4922 \u662f\u7684\uff0c\u7c7b\u662f cpp \u7684\u6982\u5ff5\uff0cdll \u91cc\u9762\u53ea\u6709\u51fd\u6570\u7684\u6982\u5ff5\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586494\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : VDimos <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yangheng4922 \u4f60\u53ef\u4ee5\u81ea\u5df1\u5199\uff0cmsdn \u4e0a\u8bf4\u7528_declspec(dllimport)\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u5bfc\u51fa\u7684\u5bf9\u8c61\uff0c\u524d\u63d0\u662f\u4f60\u4eec\u7f16\u8bd1\u5668\u8fd9\u4e9b\u5f97\u4e00\u6837\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 pe \u5de5\u5177\u67e5\u770b\u5b83\u7528\u7684\u7f16\u8bd1\u5668\u8fd9\u4e9b\uff0c\u6211\u8bb0\u5f97 dll \u662f\u6709\u8fd9\u4e2a\u4fe1\u606f\u7684\u3002\u7136\u540e\u81ea\u5df1\u7528 extern \u201cc\u201d\u6765\u5bfc\u51fa c \u98ce\u683c\u7684\u51fd\u6570\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586495\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yangheng4922 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @VDimos #6 \u4e0d\u4f1a C++                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586496\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yangheng4922 \u662f\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2586497\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bestie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e4b\u524d\u4e5f\u6709\u8fd9\u4e48\u4e2a\u9700\u6c42\uff0c\u540e\u6765\u76f4\u63a5\u8ba9 C++\u63d0\u4f9b node \u6a21\u5757\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Nodejs \u4f7f\u7528 node-ff&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137564"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137564"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137564\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137564"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}