{"id":360,"date":"2013-12-12T16:04:11","date_gmt":"2013-12-12T08:04:11","guid":{"rendered":"http:\/\/blog.sorayuki.net\/?p=360"},"modified":"2013-12-12T16:05:05","modified_gmt":"2013-12-12T08:05:05","slug":"getprocaddress%e7%bb%99%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/blog.sorayuki.net\/?p=360","title":{"rendered":"GetProcAddress\u7ed9\u51fd\u6570\u6307\u9488\u8d4b\u503c"},"content":{"rendered":"<p>\u6839\u672c\u4e0d\u662f\u4ec0\u4e48\u65b0\u6280\u672f\uff0c\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u5e94\u7528\uff0c\u53ea\u662f\u5acc\u9ebb\u70e6\uff0c\u6240\u4ee5\u2026\u2026<\/p>\n<p><!--more--><\/p>\n<p>\u8d77\u56e0\u662f\u8fd9\u6837\u7684\uff0c\u7528LoadLibrary+GetProcAddress\u52a8\u6001\u8c03\u7528DLL\u91cc\u7684\u51fd\u6570\u5df2\u7ecf\u662f\u5f88\u7ecf\u5178\u7684\u7528\u6cd5\u4e86\u3002\u4f46\u662fGetProcAddress\u8fd4\u56de\u7c7b\u578b\u662fFARPROC\uff0c\u660e\u663e\u6ca1\u529e\u6cd5\u9002\u5e94\u5404\u79cd\u51fd\u6570\u7684\uff0c\u6240\u4ee5\u8981\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362\u624d\u884c\u3002MSDN\u4e0a\u7684\u4f8b\u5b50\u662f\u8fd9\u6837\uff0c\u4e5f\u662f\u6700\u5e38\u89c1\u5230\u7684\u5199\u6cd5<\/p>\n<pre class=\"prettyprint\">typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);\nPGNSI pGNSI;\npGNSI = (PGNSI) GetProcAddress(\n   GetModuleHandle(TEXT(\"kernel32.dll\")), \n   \"GetNativeSystemInfo\");\n<\/pre>\n<p>\u8fd9\u5f53\u7136\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u90a3\u4e00\u53e5typedef\u633a\u70e6\u4eba\u3002\u4e3a\u4e86\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u53c8\u591a\u4e86\u4e00\u79cd\u7c7b\u578b\u51fa\u6765\uff0c\u5bf9\u4e8e\u5f3a\u8feb\u75c7\u7684\u4eba\u6765\u8bf4\u5b9e\u5728\u6709\u70b9\u2026\u2026<\/p>\n<p>\u4e8e\u662f\u5462\uff0ctypedef\u5176\u5b9e\u53ea\u662f\u628a\u7c7b\u578b\u7528\u53e6\u4e00\u79cd\u65b9\u5f0f\u8868\u793a\u800c\u5df2\uff0c\u6240\u4ee5\u5b8c\u5168\u53ef\u4ee5\u4e0d\u7528\u7684\u3002\u4e0d\u7528\u7684\u8bdd\uff0c\u4ee3\u7801\u5c31\u53d8\u6210\u8fd9\u6837\u5b50<\/p>\n<pre class=\"prettyprint\">void (WINAPI *pGNSI)(LPSYSTEM_INFO);\npGNSI = (void (WINAPI *)(LPSYSTEM_INFO)) GetProcAddress(\n   GetModuleHandle(TEXT(\"kernel32.dll\")), \n   \"GetNativeSystemInfo\");\n<\/pre>\n<p>\u90a3\u4e00\u957f\u4e32\u7684\u201c\u7c7b\u578b\u201d\u2026\u2026\u2026\u2026\u8fd9\u8f6c\u6362\u5199\u5f97\u6709\u70b9\u7d2f\u4eba\u554a\u3002\u8fd9\u4e00\u4e2a\u53c2\u6570\u7684\u8fd8\u597d\uff0c\u5230\u4e86CreateRemoteThread\u554aCreateFile\u554aCreateProcess\u554a\uff0c\u5199\u8d77\u6765\u5b9e\u5728\u4f53\u529b\u6d3b<\/p>\n<p><pre class=\"prettyprint\">HANDLE (WINAPI *pCRT)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);\npCRT = (HANDLE (WINAPI *)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD))\n    GetProcAddress(hKernel32, \"CreateRemoteThread\");\n<\/pre>\n<p>\u2026\u2026\u2026\u2026\uff08\u6342\u8138<\/p>\n<p>\u56e0\u4e3a\u5f88\u9ebb\u70e6\uff0c\u6240\u4ee5\u6211\u524d\u524d\u540e\u540e\u7528\u4e86\u597d\u591a\u79cd\u65b9\u5f0f\u6765\u5bf9\u4ed8\u3002\u6bd4\u5982\u8bf4\uff0cC\u7684\u5171\u7528\u4f53\u3002<\/p>\n<pre class=\"prettyprint\">union {\n    HANDLE (WINAPI *pCRT)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);\n    void* val;\n} u;\nu.val = GetProcAddress(hKernel32, \"CreateRemoteThread\");\n<\/pre>\n<p>\u8fd9\u6837\u5c31\u662f\u7528\u8d77\u6765\u7684\u65f6\u5019\uff0c\u524d\u9762\u8fd8\u5f97\u8ddf\u7740 u. \uff0c\u633a\u70e6\u7684\u3002\u867d\u7136\u8f6c\u6362\u662f\u4e0d\u7528\u8f6c\u4e86\uff1b<\/p>\n<p>\u518d\u6bd4\u5982\u8bf4\uff0cC++\u7684\u6a21\u677f\uff0c<\/p>\n<pre class=\"prettyprint\">template&lt;class T1, class T2&gt;\nvoid AssignFuncPtr(T1&amp; a, T2 b) {\n    a = (T1) b;\n}\n\n...\nHANDLE (WINAPI *pCRT)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);\nAssignFuncPtr(pCRT, GetProcAddress(hKernel32, \"CreateRemoteThread\"));\n<\/pre>\n<p>\u7a0d\u5fae\u8212\u670d\u4e86\u4e00\u70b9\uff0c\u4f46\u662f\u524d\u9762\u8fd8\u5f97\u5199\u4e2a\u6a21\u677f\u3002<\/p>\n<p>\u524d\u4e00\u6bb5\u65f6\u95f4\uff0c\u5c1d\u8bd5\u5728VC6\u4e0d\u88c5GdiPlus SDK\u7684\u60c5\u51b5\u4e0b\u8c03\u7528GdiPlus\uff0c\u53c8\u9047\u4e0a\u4e86\u8fd9\u6837\u7684\u4e8b\u60c5\u3002\u4e8e\u662f\uff0c<\/p>\n<pre class=\"prettyprint\">LRESULT (WINAPI* GdipCreateBitmapFromFile)(LPCWSTR, LPVOID*);\nvoid* faddr = GetProcAddress(hGdiPlus, \"GdipCreateBitmapFromFile\");\nmemcpy(&amp;GdipCreateBitmapFromFile, &amp;faddr, sizeof(faddr));<\/pre>\n<p>\u5df2\u7ecf\u5f88\u8212\u670d\u4e86\uff0c\u4e0d\u9700\u8981\u7ba1\u7c7b\u578b\u4e86\uff1f\u4f46\u662f\u4e00\u4e2afaddr\u591a\u51fa\u6765\uff0c\u800c\u4e14\u8fd8\u8981\u8c03\u7528memcpy\uff0c\u55ef\u2026\u2026\u2026\u2026<\/p>\n<p>\u4eca\u5929\u8111\u5b50\u4e00\u62bd\uff0c\u6765\u4e86\u8fd9\u4e2a<\/p>\n<pre class=\"prettyprint\">LRESULT (WINAPI* GdipCreateBitmapFromFile)(LPCWSTR, \"LPVOID*);\n*(LPVOID*)&amp;GdipCreateBitmapFromFile\" = GetProcAddress(hGdiPlus, \"GdipCreateBitmapFromFile\");\n<\/pre>\n<p>\u5367\u69fd\u8001\u5b50\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u5c4c\uff08\u9524\u9524\u9524\uff09\u4f60\u8f6c\u8fc7\u53bb\u5f88\u9ebb\u70e6\u90a3\u6211\u8f6c\u8fc7\u53bb\u4e0d\u5c31\u5f97\u4e86\uff0c\u56e0\u4e3aLPVOID\uff08void*\uff09\u662f\u63a5\u8fd1\u4e07\u80fd\u7684\u6307\u9488\u554a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u672c\u4e0d\u662f\u4ec0\u4e48\u65b0\u6280\u672f\uff0c\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u5e94\u7528\uff0c\u53ea\u662f\u5acc\u9ebb\u70e6\uff0c\u6240\u4ee5\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360"}],"version-history":[{"count":0,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}