{"id":2734,"date":"2015-07-25T15:09:34","date_gmt":"2015-07-25T07:09:34","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=2734"},"modified":"2015-07-25T15:14:26","modified_gmt":"2015-07-25T07:14:26","slug":"%e3%80%90windows%e3%80%91%e8%8e%b7%e5%8f%96%e5%87%bd%e6%95%b0%e5%9c%b0%e5%9d%80%e6%89%80%e5%af%b9%e5%ba%94%e7%9a%84dll%e6%88%96exe%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=2734","title":{"rendered":"\u3010Windows\u3011\u83b7\u53d6\u51fd\u6570\u5730\u5740\u6240\u5bf9\u5e94\u7684DLL\u6216EXE\u5730\u5740"},"content":{"rendered":"<p>\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8ba9\u4f60\u77e5\u9053\u4e00\u4e2a\u51fd\u6570\u6765\u81ea\u90a3\u91ccDLL\u6216\u8005\u662f\u4e0d\u662f\u5f53\u524d\u53ef\u6267\u884c\u6587\u4ef6\u5185\u90e8\u5b9e\u73b0\u7684\u51fd\u6570\uff0c\u53c2\u8003\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">uses psapi;\r\n\r\nfunction GetCodeFileName(Addr: Pointer): QStringW;\r\nvar\r\n  AModules: array of THandle;\r\n  I, ACount: Cardinal;\r\n  APriorModule: THandle;\r\n  procedure QuickSort(L, R: Integer);\r\n  var\r\n    I, J: Integer;\r\n    P, T: THandle;\r\n  begin\r\n    repeat\r\n      I := L;\r\n      J := R;\r\n      P := AModules[(L + R) shr 1];\r\n      repeat\r\n        while AModules[I] &lt; P do\r\n          Inc(I);\r\n        while AModules[J] &gt; P do\r\n          Dec(J);\r\n        if I &lt;= J then\r\n        begin\r\n          if I &lt;&gt; J then\r\n          begin\r\n            T := AModules[I];\r\n            AModules[I] := AModules[J];\r\n            AModules[J] := T;\r\n          end;\r\n          Inc(I);\r\n          Dec(J);\r\n        end;\r\n      until I &gt; J;\r\n      if L &lt; J then\r\n        QuickSort(L, J);\r\n      L := I;\r\n    until I &gt;= R;\r\n  end;\r\n  function GetModuleFileName(AModule: THandle): QStringW;\r\n  begin\r\n    SetLength(Result, MAX_PATH);\r\n    SetLength(Result, GetModuleFileNameW(AModule, PWideChar(Result), MAX_PATH));\r\n  end;\r\n\r\nbegin\r\n  Result := '';\r\n  EnumProcessModules(GetCurrentProcess, nil, 0, ACount);\r\n  SetLength(AModules, ACount);\r\n  APriorModule := 0;\r\n  if EnumProcessModules(GetCurrentProcess, @AModules[0], ACount, ACount) then\r\n  begin\r\n    QuickSort(0, ACount - 1);\r\n    for I := 0 to ACount - 1 do\r\n    begin\r\n      if AModules[I] &lt;&gt; 0 then\r\n      begin\r\n        if (THandle(Addr) &gt;= APriorModule) and (THandle(Addr) &lt; AModules[I])\r\n        then\r\n        begin\r\n          Result := GetModuleFileName(APriorModule);\r\n          Break;\r\n        end;\r\n        APriorModule := AModules[I];\r\n      end;\r\n    end;\r\n    if Length(Result) = 0 then\r\n    begin\r\n      SetLength(Result, MAX_PATH);\r\n      SetLength(Result, GetModuleFileNameW(APriorModule, PWideChar(Result),\r\n        MAX_PATH));\r\n    end;\r\n  end;\r\nend;<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u7528\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c\u6bd4\u5982\u6211\u4eec\u8981\u770b\u4e00\u4e0b IDBService \u8fd9\u4e2a\u670d\u52a1\u5b9e\u4f8b\u662f\u7531\u90a3\u4e2a DLL \u521b\u5efa\u7684\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">var\r\n  AService: IDBService;\r\nbegin\r\n  if Supports(PluginsManager, IDBService, AService) then\r\n  begin\r\n    ShowMessage('\u670d\u52a1 ' + IntToHex(IntPtr(AService), 8) + ' \u6765\u81ea\u6587\u4ef6: '#13#10 +\r\n      GetCodeFileName(PPointer(AService)^));\r\n  end;<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6267\u884c\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2015\/07\/codefilename.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2735\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2015\/07\/codefilename.jpg\" alt=\"codefilename\" width=\"366\" height=\"136\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2015\/07\/codefilename.jpg 366w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2015\/07\/codefilename-300x111.jpg 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8ba9\u4f60\u77e5\u9053\u4e00\u4e2a\u51fd\u6570\u6765\u81ea\u90a3\u91cc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2734","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":3263,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2734"}],"version-history":[{"count":1,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2734\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2734\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}