{"id":63,"date":"2023-02-07T16:48:23","date_gmt":"2023-02-07T08:48:23","guid":{"rendered":"http:\/\/zkxx.xyz\/?p=63"},"modified":"2023-02-07T17:44:02","modified_gmt":"2023-02-07T09:44:02","slug":"mfc%e6%89%a9%e5%b1%95dll","status":"publish","type":"post","link":"https:\/\/zkxx.xyz\/index.php\/2023\/02\/07\/mfc%e6%89%a9%e5%b1%95dll\/","title":{"rendered":"MFC\u6269\u5c55DLL"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>MFC\u6269\u5c55DLL\u4e0e\u5e38\u89c4DLL<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">MFC\u63d0\u4f9b\u4e86\u4e09\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u652f\u6301DLL\u7684\u5f00\u53d1\uff1a<\/h4>\n\n\n\n<p>1.\u5efa\u7acb\u9759\u6001\u94fe\u63a5MFC\u7684\u5e38\u89c4DLL\uff08Regular DLL\uff09<br>2.\u5efa\u7acb\u52a8\u6001\u94fe\u63a5MFC\u7684\u5e38\u89c4DLL<br>3.\u5efa\u7acb\u52a8\u6001\u94fe\u63a5MFC\u7684\u6269\u5c55DLL\uff08Extension DLL\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><br>\u6269\u5c55DLL\u4e0e\u5e38\u89c4DLL\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/h4>\n\n\n\n<p><br>\uff081\uff09MFC\u7684\u6269\u5c55DLL\u652f\u6301C++\u63a5\u53e3\uff0c\u5373\u6269\u5c55DLL\u80fd\u591f\u5bfc\u51fa\u6574\u4e2aC++\u7c7b\u3002\u8fd9\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5df2\u6709\u7684MFC\u7c7b\u6d3e\u751f\u65b0\u7684\u53ef\u518d\u7528\u7c7b\u3002\u6269\u5c55DLL\u5728\u5efa\u7acb\u65f6\u4f7f\u7528\u7684\u662fMFC\u7684\u52a8\u6001\u94fe\u63a5\uff0c\u56e0\u800c\u6269\u5c55DLL\u8981\u6c42\u5ba2\u6237\u7a0b\u5e8f\u52a8\u6001\u5730\u94fe\u63a5\u5230MFC\u52a8\u6001\u5e93\u3002\u6269\u5c55DLL\u7684\u4e00\u4e2a\u7279\u70b9\u662f\u5c3a\u5bf8\u5f88\u5c0f\uff0c\u80fd\u591f\u5f88\u5feb\u52a0\u8f7d\u3002<br>\uff082\uff09\u5e38\u89c4DLL\u53ef\u88ab\u4efb\u610fwin32\u7f16\u7a0b\u73af\u5883\u52a0\u8f7d\u3002\u5b83\u7684\u5c40\u9650\u6027\u5728\u4e8e\u5e38\u89c4DLL\u53ea\u80fd\u5bfc\u51fa\u6807\u51c6C\u63a5\u53e3\uff0c\u4e0d\u80fd\u5bfc\u51faC++\u7c7b\u3002\u4f46\u5728\u5e38\u89c4DLL\u5185\u90e8\uff0c\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528C++\u7c7b\u53caMFC\u7c7b\u3002<br>\uff083\uff09\u5e38\u89c4DLL\u80fd\u591f\u91c7\u7528<strong>\u663e\u5f0f\u94fe\u63a5\u6216\u9690\u5f0f\u94fe\u63a5<\/strong>\uff0c\u800c\u6269\u5c55DLL\u53ea\u80fd\u91c7\u7528\u663e\u793a\u94fe\u63a5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><br>\u9759\u6001\u94fe\u63a5MFC\u7684DLL\u4e0e\u52a8\u6001\u94fe\u63a5MFC\u7684DLL\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/h4>\n\n\n\n<p><br>\uff081\uff09\u9759\u6001\u94fe\u63a5MFC\u7684DLL\u5c06\u62f7\u8d1d\u6240\u6709\u9700\u8981\u7684MFC\u7684\u4ee3\u7801\uff0c\u6210\u4e3a\u81ea\u6211\u5305\u542b\u7684\u6a21\u5757\uff0c\u53ef\u72ec\u7acb\u4e8eMFC\u7684DLL\u5e93\u800c\u8fd0\u884c\uff0c\u4f46DLL\u7684\u4ee3\u7801\u5c3a\u5bf8\u4f1a\u8f83\u5927\u3002<br>\uff082\uff09\u52a8\u6001\u94fe\u63a5MFC\u7684DLL\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u94fe\u63a5MFC\u7c7b\u5e93\uff0c\u56e0\u800cDLL\u7684\u4ee3\u7801\u5c3a\u5bf8\u4f1a\u5927\u5927\u51cf\u5c0f\uff0c\u4f46\u5fc5\u987b\u786e\u4fdd\u5728\u8fd0\u884c\u7684\u7cfb\u7edf\u4e0a\u6709\u5408\u9002\u7248\u672c\u7684MFC DLL\u5e93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u663e\u5f0f\u94fe\u63a5\u6216\u9690\u5f0f\u94fe\u63a5<\/strong><\/h3>\n\n\n\n<p>\u7406\u8bba\uff1a\u8c03\u7528\u52a8\u6001DLL\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4e00\u79cd\u662f\u9690\u5f0f\u94fe\u63a5\uff0c\u4e00\u79cd\u662f\u663e\u5f0f\u94fe\u63a5\uff0c\u5982\u679c\u7528loadlibrary\u65b9\u5f0f\u5c31\u662f\u663e\u793a\u94fe\u63a5\uff0c\u7528.h\u3001.lib\u3001.dll\u4e09\u4ef6\u5957\u5c31\u5c5e\u4e8e\u9690\u5f0f\u94fe\u63a5\u3002<\/p>\n\n\n\n<p>\u4e00\uff1a\u9690\u5f0f\u94fe\u63a5\uff1a<\/p>\n\n\n\n<p>\u9690\u5f0f\u94fe\u63a5\u91c7\u7528\u9759\u6001\u52a0\u8f7d\u7684\u65b9\u5f0f\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u9700\u8981.h\u3001.lib\u3001.dll\u4e09\u4ef6\u5957\u3002\u65b0\u5efa\u201c\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\u201d\u6216\u201c\u7a7a\u9879\u76ee\u201d\u3002\u914d\u7f6e\u5982\u4e0b:<\/p>\n\n\n\n<p>\u9879\u76ee-&gt;\u5c5e\u6027-&gt;\u914d\u7f6e\u5c5e\u6027-&gt;VC++ \u76ee\u5f55-&gt; \u5728\u201c\u5305\u542b\u76ee\u5f55\u201d\u91cc\u6dfb\u52a0\u5934\u6587\u4ef6xxx.h\u6240\u5728\u7684\u76ee\u5f55<\/p>\n\n\n\n<p>\u9879\u76ee-&gt;\u5c5e\u6027-&gt;\u914d\u7f6e\u5c5e\u6027-&gt;VC++ \u76ee\u5f55-&gt; \u5728\u201c\u5e93\u76ee\u5f55\u201d\u91cc\u6dfb\u52a0\u5934\u6587\u4ef6xxx.lib\u6240\u5728\u7684\u76ee\u5f55<\/p>\n\n\n\n<p>\u9879\u76ee-&gt;\u5c5e\u6027-&gt;\u914d\u7f6e\u5c5e\u6027-&gt;\u94fe\u63a5\u5668-&gt;\u8f93\u5165-&gt; \u5728\u201c\u9644\u52a0\u4f9d\u8d56\u9879\u201d\u91cc\u6dfb\u52a0\u201cxxx.lib\u201d\uff08\u82e5\u6709\u591a\u4e2a lib \u5219\u4ee5\u7a7a\u683c\u9694\u5f00\uff09\u3002<\/p>\n\n\n\n<p>\u6ce8\uff1a\u4e5f\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u4e00\u884c\u8bbe\u7f6e\u5e93\u7684\u94fe\u63a5\uff0c<\/p>\n\n\n\n<p>#pragma comment(lib, &#8220;xxx.lib&#8221;)<\/p>\n\n\n\n<p>\u9690\u5f0f\u94fe\u63a5\u548cvs\u52a0\u8f7d\u914d\u7f6eopencv\u5f88\u50cf\uff0c\u4e0d\u505a\u5b9e\u4f8b\u5c55\u793a\u3002<\/p>\n\n\n\n<p>\u4e8c\uff1a\u663e\u5f0f\u94fe\u63a5\uff1a<\/p>\n\n\n\n<p>\u4eca\u5929\u53d1\u73b0\uff0c\u53ef\u4ee5\u7528\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528dll\u3002<\/p>\n\n\n\n<p><strong>\u8c03\u7528\u6b65\u9aa4:<\/strong><\/p>\n\n\n\n<p>1\u3001\u58f0\u660e\u5934\u6587\u4ef6&lt;windows.h&gt;\uff0c\u8bf4\u660e\u6211\u60f3\u7528windows32\u65b9\u6cd5\u6765\u52a0\u8f7d\u548c\u5378\u8f7dDLL<\/p>\n\n\n\n<p>\u6ce8\uff1a\u5982\u679c\u4f7f\u7528\u4e86opencv\uff0c\u4e0d\u80fd\u4f7f\u7528using namespace cv\uff0c\u53ef\u80fd\u548cwindows.h\u91cc\u9762\u7684\u547d\u540d\u7a7a\u95f4\u51b2\u7a81\uff0c\u5fc5\u987b\u4f7f\u7528cv:: \u3002<\/p>\n\n\n\n<p>2\u3001\u7136\u540e\u7528typedef\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u51fd\u6570\u7c7b\u578b.typedef void(*fun) \/\/\u8fd9\u4e2a\u6307\u9488\u7c7b\u578b\uff0c\u8981\u548c\u4f60\u8c03\u7528\u7684\u51fd\u6570\u7c7b\u578b\u548c\u53c2\u6570\u4fdd\u6301\u4e00\u81f4<\/p>\n\n\n\n<p>3\u3001\u5b9a\u4e00\u4e2a\u53e5\u67c4\u5b9e\u4f8b\uff0c\u7528\u6765\u53d6DLL\u7684\u5b9e\u4f8b\u5730\u5740\u3002HINSTANCE hdll\uff1b<\/p>\n\n\n\n<p>\u683c\u5f0f\u4e3ahdll=LoadLibrary\uff08\u201cDLL\u5730\u5740\u201d\uff09\uff1b\u4f60\u5c01\u88c5\u7684dll\u8def\u5f84\u540d\u79f0<\/p>\n\n\n\n<p>\u8981\u914d\u7f6e-\u5c5e\u6027-\u5e38\u89c4\u91cc\u9762\u628a\u9ed8\u8ba4\u5b57\u7b26\u96c6\u201cunicode\u201d\u6539\u6210\u652f\u6301\u591a\u5b57\u7b26\u6269\u5c55\u3002<\/p>\n\n\n\n<p>4\u3001\u53d6\u7684\u5730\u5740\u8981\u5224\u65ad\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u7528\u6765\u83b7\u53d6\u4f60\u8981\u7528\u7684\u51fd\u6570\u5730\u5740\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u901a\u8fc7Win32 API \u51fd\u6570GetProcAdress\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740\uff0c\u53c2\u6570\u662fDLL\u7684\u53e5\u67c4\u548c\u4f60\u8981\u8c03\u7528\u7684\u51fd\u6570\u540d\uff1a\u6bd4\u5982\uff1aFUN=(fun)GetProcAdress(hdll,&#8221;My_dll&#8221;);<\/p>\n\n\n\n<p>\u8981\u5224\u65ad\u8981\u51fd\u6570\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u6ca1\u53d6\u5230\u8981\u6c42\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u8981\u91ca\u653e\u53e5\u67c4\u3002<\/p>\n\n\n\n<p>6\u3001\u7136\u540e\u901a\u8fc7\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528\u51fd\u6570\u3002<\/p>\n\n\n\n<p>7\u3001\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u5c31\u91ca\u653e\u53e5\u67c4FreeLibrary(hdll);<\/p>\n\n\n\n<p>\u5b9e\u4f8b\u5c55\u793a\uff1a<\/p>\n\n\n\n<p>\u5c01\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;\nextern \"C\" __declspec(dllexport) int My_add(int a,int b)\n{\n\tprintf(\"%d\\n\",a+b);\n\treturn a + b;\n}\n\nextern \"C\" __declspec(dllexport) int My_sub(int a, int b)\n{\n\tprintf(\"%d\\n\", a - b);\n\treturn a - b;\n}<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;\n#include \"Windows.h\"\ntypedef int(*Dllfun)(int, int);\n\n\/\/using namespace std;\n\/\/using namespace cv;\nint main()\n{\n\n\tDllfun funName;\n\tHINSTANCE hdll;\n\t\/\/put DLL under the release path   \n\thdll = LoadLibrary((\"ThreadLocal.dll\"));\n\tif (hdll == NULL)\n\t{\n\t\tFreeLibrary(hdll);\n\t}\n\tfunName = (Dllfun)GetProcAddress(hdll, \"My_add\");\/\/My_sub\n\tif (funName == NULL)\n\t{\n\t\tFreeLibrary(hdll);\n\t}\n\tint x = 1, y = 10;\n\tint z = funName(x, y);\n\tprintf(\"z= %d\\n\", z);\n\n\tFreeLibrary(hdll);\n\n\treturn 0;\n}\n\n\n\n<\/code><\/pre>\n\n\n\n<p>\u81ea\u5df1\u5b9a\u4e49\u7684\u52a0\u6cd5\uff0cLoadLibrary GetProcAddress \u770b\u8d77\u6765\u9ebb\u70e6\uff0c\u53ef\u662f\u7701\u53bb\u4e86\u5934\u6587\u4ef6\u548clib\u7684\u52a0\u8f7d\uff0c\u66f4\u6539\u63a5\u53e3\u6d4b\u8bd5\u65b9\u4fbf\uff0c\u79fb\u690d\u5230Ubuntu\u548ccent os\u4e5f\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MFC\u6269\u5c55DLL\u4e0e\u5e38\u89c4DLL MFC\u63d0\u4f9b\u4e86\u4e09\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u652f\u6301DLL\u7684\u5f00\u53d1\uff1a 1.\u5efa\u7acb\u9759\u6001\u94fe\u63a5MFC\u7684\u5e38\u89c4DLL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":3,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zkxx.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}