{"id":299765,"date":"2024-05-20T17:25:58","date_gmt":"2024-05-20T09:25:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299765.html"},"modified":"2024-05-20T17:26:17","modified_gmt":"2024-05-20T09:26:17","slug":"dll-%e5%8a%a8%e6%80%81%e9%93%be%e6%8e%a5%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299765.html","title":{"rendered":"dll \u52a8\u6001\u94fe\u63a5\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27152026\/8bf14c97-062a-4b28-9a46-b4393f337f88.webp\" alt=\"dll \u52a8\u6001\u94fe\u63a5\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>DLL\u52a8\u6001\u94fe\u63a5\u5e93\u662f\u4e00\u79cd\u5305\u542b\u53ef\u7531\u591a\u4e2a\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u7684\u4ee3\u7801\u548c\u6570\u636e\u7684\u5e93\u3002\u4f7f\u7528\u65b9\u6cd5\u901a\u5e38\u5305\u62ec\uff1a<strong>\u52a0\u8f7dDLL\u6587\u4ef6\u3001\u67e5\u627eDLL\u4e2d\u7684\u51fd\u6570\u3001\u8c03\u7528\u51fd\u6570\u3001\u5378\u8f7dDLL<\/strong>\u3002\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528DLL\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3001\u4fc3\u8fdb\u6a21\u5757\u5316\u7f16\u7a0b\u3001\u5b9e\u73b0\u6613\u4e8e\u66f4\u65b0\u548c\u90e8\u7f72\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8981\u4f7f\u7528DLL\uff0c\u5f00\u53d1\u4eba\u5458\u9700\u8981\u901a\u8fc7\u52a8\u6001\u94fe\u63a5\u6216\u9759\u6001\u94fe\u63a5\u7684\u65b9\u5f0f\u5c06DLL\u96c6\u6210\u5230\u9879\u76ee\u4e2d\u3002\u52a8\u6001\u94fe\u63a5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a0\u8f7dDLL\uff0c\u9759\u6001\u94fe\u63a5\u5219\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u786e\u5b9a\u3002\u5728Windows\u5e73\u53f0\uff0cLoadLibrary\u3001GetProcAddress\u548cFreeLibrary\u7b49\u51fd\u6570\u662f\u8fdb\u884c\u52a8\u6001\u94fe\u63a5\u7684\u5173\u952e\u3002\u4ee5\u4e0b\u662fDLL\u4f7f\u7528\u65b9\u6cd5\u4e2d\u7684\u5173\u952e\u6b65\u9aa4\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u52a0\u8f7dDLL<\/h2>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528DLL\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u52a0\u8f7d\u76f8\u5e94\u7684DLL\u6587\u4ef6\u3002\u8fd9\u901a\u5e38\u901a\u8fc7\u8c03\u7528Windows API\u4e2d\u7684LoadLibrary\u51fd\u6570\u6765\u5b8c\u6210\u3002\u5f53LoadLibrary\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u6307\u5b9a\u7684DLL\u662f\u5426\u5df2\u7ecf\u52a0\u8f7d\u5230\u8fdb\u7a0b\u7a7a\u95f4\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u52a0\u8f7d\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">HMODULE hModule = LoadLibrary(L&quot;MyLibrary.dll&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c<code>LoadLibrary<\/code>\u51fd\u6570\u6210\u529f\uff0c\u5b83\u4f1a\u8fd4\u56deDLL\u6a21\u5757\u7684\u53e5\u67c4\uff0c\u8be5\u53e5\u67c4\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u6765\u5f15\u7528DLL\u3002\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56deNULL\uff0c\u53ef\u4ee5\u901a\u8fc7<code>GetLastError<\/code>\u51fd\u6570\u83b7\u5f97\u9519\u8bef\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u67e5\u627eDLL\u4e2d\u7684\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6DLL\u88ab\u52a0\u8f7d\uff0c\u53ef\u4ee5\u4f7f\u7528GetProcAddress\u51fd\u6570\u901a\u8fc7\u51fd\u6570\u540d\u79f0\u6216\u51fd\u6570\u5bfc\u51fa\u5e8f\u53f7\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">typedef void (*FunctionType)();<\/p>\n<p>FunctionType function = (FunctionType) GetProcAddress(hModule, &quot;MyFunction&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u51fd\u6570\u5730\u5740\u88ab\u6210\u529f\u68c0\u7d22\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5f53\u7684\u51fd\u6570\u6307\u9488\u7c7b\u578b\u4ee5\u5907\u540e\u7528\u3002\u5982\u679c\u67e5\u627e\u5931\u8d25\uff0c\u5219\u8fd4\u56deNULL\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5f97\u5230\u51fd\u6570\u5730\u5740\u540e\uff0c\u5c31\u53ef\u4ee5\u50cf\u8c03\u7528\u666e\u901a\u51fd\u6570\u4e00\u6837\u8c03\u7528DLL\u4e2d\u5bfc\u51fa\u7684\u51fd\u6570\u4e86\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">if(function != NULL) {<\/p>\n<p>    function(); \/\/ \u8c03\u7528DLL\u51fd\u6570<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u51c6\u786e\u5730\u77e5\u9053\u51fd\u6570\u7684\u6b63\u786e\u7b7e\u540d\uff0c\u56e0\u4e3a\u9519\u8bef\u7684\u7b7e\u540d\u53ef\u80fd\u5bfc\u81f4\u8fd0\u884c\u65f6\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f20\u9012\u6570\u636e\u7ed9DLL\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5f53\u8c03\u7528DLL\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5411\u5b83\u4f20\u9012\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u53c2\u6570\u3002\u8fd9\u5e94\u8be5\u6309\u7167\u51fd\u6570\u539f\u578b\u5b9a\u4e49\u7684\u7b7e\u540d\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">typedef void (*FunctionTypeWithParams)(int, const char*);<\/p>\n<p>FunctionTypeWithParams functionWithParams = (FunctionTypeWithParams) GetProcAddress(hModule, &quot;MyFunctionWithParams&quot;);<\/p>\n<p>if(functionWithParams != NULL) {<\/p>\n<p>    functionWithParams(123, &quot;example&quot;); \/\/ \u53c2\u6570\u4f20\u9012\u7ed9DLL\u51fd\u6570<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u8981\u6c42\u8c03\u7528\u65b9\u548cDLL\u5171\u4eab\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5378\u8f7dDLL<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u5b8c\u6210\u5bf9DLL\u51fd\u6570\u7684\u6240\u6709\u8c03\u7528\uff0c\u5e94\u8be5\u5378\u8f7dDLL\u3002\u8fd9\u901a\u8fc7\u8c03\u7528FreeLibrary\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">FreeLibrary(hModule);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>FreeLibrary<\/code>\u51fd\u6570\u5c06\u51cf\u5c11DLL\u6a21\u5757\u7684\u5f15\u7528\u8ba1\u6570\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u8fbe\u52300\u65f6\uff0cDLL\u6a21\u5757\u5c06\u4ece\u8fdb\u7a0b\u7a7a\u95f4\u5378\u8f7d\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528DLL\u4e2d\u7c7b\u6216\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>\u9664\u4e86C\u98ce\u683c\u7684\u51fd\u6570\u4e4b\u5916\uff0cDLL\u901a\u5e38\u4e5f\u53ef\u4ee5\u5bfc\u51fa\u7c7b\u6216\u5bf9\u8c61\uff0c\u4f7f\u5f97\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u66f4\u52a0\u65b9\u4fbf\u3002\u7136\u540e\u518d\u901a\u8fc7C++\u7279\u6709\u7684\u65b9\u5f0f\u6765\u52a8\u6001\u52a0\u8f7d\u548c\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u6216\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c++\">\/\/ \u5047\u8bbeDLL\u6709\u4e00\u4e2a\u5bfc\u51fa\u7c7bMyClass<\/p>\n<p>MyClass* myClassInstance = (MyClass*) GetProcAddress(hModule, &quot;MyClassInstance&quot;);<\/p>\n<p>if(myClassInstance != NULL) {<\/p>\n<p>    myClassInstance-&gt;MyMethod(); \/\/ \u8c03\u7528DLL\u4e2d\u7c7b\u7684\u65b9\u6cd5<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u5e38\u8fd9\u79cd\u65b9\u6cd5\u8981\u6c42\u6709\u9002\u5f53\u7684\u7f16\u7a0b\u7ea6\u5b9a\u548cABI\uff08\u5e94\u7528\u7a0b\u5e8f\u4e8c\u8fdb\u5236\u63a5\u53e3\uff09\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u5728\u52a0\u8f7d\u548c\u4f7f\u7528DLL\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u52a1\u5fc5\u8981\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u6216\u5176\u4ed6\u9884\u671f\u5916\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">if(hModule == NULL) {<\/p>\n<p>    \/\/ \u5904\u7406\u52a0\u8f7dDLL\u5931\u8d25<\/p>\n<p>}<\/p>\n<p>if(function == NULL) {<\/p>\n<p>    \/\/ \u5904\u7406\u83b7\u53d6\u51fd\u6570\u5730\u5740\u5931\u8d25<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u786e\u7684\u9519\u8bef\u5904\u7406\u5e94\u5305\u62ec\u65e5\u5fd7\u8bb0\u5f55\u3001\u7528\u6237\u901a\u77e5\u548c\u4efb\u4f55\u5fc5\u8981\u7684\u6e05\u7406\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u8de8\u5e73\u53f0\u5171\u4eab\u5e93\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728Linux\u548c\u5176\u4ed6\u7c7bUnix\u7cfb\u7edf\u4e0a\uff0c\u5171\u4eab\u5e93\u7684\u6982\u5ff5\u4e0eDLL\u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u4f7f\u7528\u7684API\u4e0d\u4e00\u6837\u3002\u6bd4\u5982\u4f7f\u7528dlopen\u3001dlsym\u548cdlclose\u7b49\u51fd\u6570\u3002\u5e73\u53f0\u95f4\u7684\u5dee\u5f02\u610f\u5473\u7740\uff0c\u8de8\u5e73\u53f0\u7684\u8f6f\u4ef6\u5f00\u53d1\u53ef\u80fd\u9700\u8981\u6761\u4ef6\u7f16\u8bd1\u6216\u62bd\u8c61\u5c42\u6765\u5904\u7406\u4e0d\u540c\u7cfb\u7edf\u4e0a\u7684\u5171\u4eab\u5e93\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><p>\u5f00\u53d1\u8005\u5728\u4f7f\u7528DLL\u52a8\u6001\u94fe\u63a5\u5e93\u65f6\uff0c\u5fc5\u987b\u6ce8\u610f\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3001\u5185\u5b58\u7ba1\u7406\u548c\u5b89\u5168\u6027\u3002\u9075\u5b88\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u5de5\u5177\u548c\u6280\u672f\u6765\u5206\u6790\u548c\u8c03\u8bd5\u4e0eDLL\u76f8\u5173\u7684\u95ee\u9898\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\n\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u662f\u4e00\u79cdWindows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5e38\u7528\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u5b83\u5305\u542b\u9884\u7f16\u8bd1\u7684\u51fd\u6570\u548c\u7a0b\u5e8f\u4ee3\u7801\uff0c\u53ef\u4f9b\u5176\u4ed6\u7a0b\u5e8f\u8c03\u7528\u548c\u4f7f\u7528\u3002DLL\u6587\u4ef6\u5177\u6709\u72ec\u7acb\u6027\uff0c\u53ef\u4ee5\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5171\u4eab\uff0c\u5e76\u80fd\u591f\u63d0\u4f9b\u53ef\u590d\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\uff1f<\/strong><br \/>\n\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u6216\u6253\u5f00\u73b0\u6709\u9879\u76ee\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u5728\u9879\u76ee\u4e2d\u9009\u62e9\u6dfb\u52a0\u5f15\u7528\uff08\u6216\u4f9d\u8d56\uff09\u5e76\u6d4f\u89c8\u81f3DLL\u6587\u4ef6\u6240\u5728\u4f4d\u7f6e\u3002<\/li>\n<li>\u7136\u540e\uff0c\u7f16\u5199\u4ee3\u7801\u6765\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u3002<\/li>\n<li>\u6700\u540e\uff0c\u5bf9\u9879\u76ee\u8fdb\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u786e\u4fdd\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u7684\u6b63\u786e\u4f7f\u7528\uff1f<\/strong><br \/>\n\u786e\u4fdd\u6b63\u786e\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u8ba4DLL\u6587\u4ef6\u8def\u5f84\u5728\u9879\u76ee\u4e2d\u6b63\u786e\u8bbe\u7f6e\u3002<\/li>\n<li>\u68c0\u67e5\u6240\u5f15\u7528\u7684DLL\u6587\u4ef6\u662f\u5426\u4e0e\u9879\u76ee\u7684\u76ee\u6807\u5e73\u53f0\u76f8\u5339\u914d\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u8c03\u7528DLL\u51fd\u6570\u4e4b\u524d\u5148\u5bfc\u5165\u76f8\u5e94\u7684\u51fd\u6570\u58f0\u660e\u3002<\/li>\n<li>\u5bf9\u4e8e\u6d89\u53ca\u5185\u5b58\u7ba1\u7406\u7684\u51fd\u6570\uff0c\u8981\u786e\u4fdd\u9002\u5f53\u5730\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u3002<\/li>\n<li>\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684DLL\u52a0\u8f7d\u9519\u8bef\u6216\u7248\u672c\u4e0d\u517c\u5bb9\u7b49\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/li>\n<li>\u6ce8\u91cdDLL\u7248\u672c\u7684\u66f4\u65b0\u548c\u7ef4\u62a4\uff0c\u4ee5\u786e\u4fdd\u83b7\u53d6\u6700\u65b0\u7684\u529f\u80fd\u548c\u6027\u80fd\u4f18\u5316\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"DLL\u52a8\u6001\u94fe\u63a5\u5e93\u662f\u4e00\u79cd\u5305\u542b\u53ef\u7531\u591a\u4e2a\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u7684\u4ee3\u7801\u548c\u6570\u636e\u7684\u5e93\u3002\u4f7f\u7528\u65b9\u6cd5\u901a\u5e38\u5305\u62ec\uff1a\u52a0\u8f7dDLL\u6587\u4ef6\u3001\u67e5\u627eDLL\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":299792,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299765"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=299765"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299792"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}