吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1374|回复: 5
收起左侧

[求助] 编译成功,加载DLL时报错。

[复制链接]
冥界3大法王 发表于 2025-10-15 13:37
[Pascal] 纯文本查看 复制代码
library MoreTool进阶版;
 {$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
 {$WEAKLINKRTTI ON}

uses
  Windows,
  Messages,
  SysUtils,
  Vcl.Dialogs,
  IniFiles,
  bridgemain in 'plugin\bridgemain.pas',
  _plugins in 'plugin\_plugins.pas';

// 根据平台选择DLL名称
{$IFDEF WIN64}
const
  X64DBG_CORE_DLL = 'x64dbg.dll';
{$ELSE}
const
  X64DBG_CORE_DLL = 'x32dbg.dll';
{$ENDIF}

{$ALIGN 1}
        {$WARN UNSAFE_CODE OFF}
        {$WARN UNSAFE_TYPE OFF}
        {$WARN UNSAFE_CAST OFF}
  {$IFDEF WIN64}
  {$E dp64}
  {$ELSE}
  {$E dp32}
  {$ENDIF}

var
  SaveDLLProc: TDLLProc;
  g_pluginHandle: THandle = 0;
  g_hMenu: Cardinal = 0;
  g_hMenuDisasm: Cardinal = 0;
  g_Inst: Cardinal = 0;
  g_hWnD: Cardinal = 0;
  g_loadedname: array[0..8] of PAnsiChar;

function ShellExecuteA(hWnd: hWnd; Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer): HINST; stdcall; external 'shell32.dll' name 'ShellExecuteA';

function _plugin_gethmenu(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenudisasm(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenudump(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenustack(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;

const
  PLUGIN_NAME: PAChar = PAnsiChar(UTF8String('牛二'));
  PLUGIN_AUTH: PAChar = PAnsiChar(UTF8String('冥界3大法王'));
  PLUGIN_VERS: Integer = 01;
  MENU_CALC = 1;
  MENU_NOTEPAD = 2;
  MENU_DISASM_CALC = 3;
  MENU_DISASM_NOTEPAD = 4;
  MENU_LOAD_EXTERNAL_PLUGINS = 5;
  MENU_DISASM_LOAD_EXTERNAL_PLUGINS = 6;

{$R MoreTool.res}

type
  TPlugInit = function(PlugInitInfo: PPLUG_INITSTRUCT): Boolean; cdecl;

procedure RegisterInitProc(cbType: cbType; callbackInfo: Pointer); cdecl;
var
  info: PPLUG_CB_INITDEBUG;
begin
  ZeroMemory(@g_loadedname, SizeOf(g_loadedname));
  info := PPLUG_CB_INITDEBUG(callbackInfo);
  g_loadedname[0] := info^.szFileName;
  BridgeSettingSet('Last File', 'Last', g_loadedname[0]);
end;

procedure RegisterMenuProc(cbType: cbType; callbackinfo: Pointer); cdecl;
var
  info: PPLUG_CB_MENUENTRY;
  IniFilePath: string;
  PluginDir: string;
  SearchRec: TSearchRec;
  PluginName: string;
  PluginHandle: THandle;
  PlugInit: TPlugInit;
  pInitStruct: PPLUG_INITSTRUCT;
  Ini: TIniFile;
  // 重新声明这些变量,确保没有语法问题
  hMainWindow: THandle; // 使用THandle替代HWND,它们在Windows API中是等价的
  hMenu: THandle;
  hMenuDisasm: THandle;
  hMenuDump: THandle;
  hMenuStack: THandle;
  PlugSetupFunc: function(PlugSetupInfo: PPLUG_SETUPSTRUCT): Boolean; cdecl;
  pSetupStruct: PPLUG_SETUPSTRUCT;
begin
  info := PPLUG_CB_MENUENTRY(callbackinfo);
  case (info^.hEntry) of
    MENU_LOAD_EXTERNAL_PLUGINS, MENU_DISASM_LOAD_EXTERNAL_PLUGINS:
      begin
        hMainWindow := GuiGetWindowHandle();
        hMenu := _plugin_gethmenu(g_pluginHandle);
        hMenuDisasm := _plugin_gethmenudisasm(g_pluginHandle);
        hMenuDump := _plugin_gethmenudump(g_pluginHandle);
        hMenuStack := _plugin_gethmenustack(g_pluginHandle);

        IniFilePath := InputBox('加载外部插件', '请输入.ini文件的完整路径:', '');
        if IniFilePath <> '' then
        begin
          try
            if FileExists(IniFilePath) then
            begin
              Ini := TIniFile.Create(IniFilePath);
              try
                PluginDir := Ini.ReadString('Your_Plugins_Folder', 'Path', '');

                if PluginDir <> '' then
                begin
                  if (PluginDir <> '') and (PluginDir[Length(PluginDir)] <> '\') then
                    PluginDir := PluginDir + '\';

                  if not DirectoryExists(PluginDir) then
                  begin
                    _plugin_logprintf('[MoreTool] 插件目录不存在: %s', PAnsiChar(UTF8String(PluginDir)));
                  end
                  else
                  begin
                    if FindFirst(PluginDir + '*.dp32', faAnyFile, SearchRec) = 0 then
                    begin
                      repeat
                        if (SearchRec.Attr and faDirectory) = 0 then
                        begin
                          PluginName := PluginDir + SearchRec.Name;
                          PluginHandle := LoadLibrary(PChar(PluginName));
                          if PluginHandle <> 0 then
                          begin
                            @PlugInit := GetProcAddress(PluginHandle, 'pluginit');
                            if Assigned(PlugInit) then
                            begin
                              GetMem(pInitStruct, SizeOf(PLUG_INITSTRUCT));
                              try
                                ZeroMemory(pInitStruct, SizeOf(PLUG_INITSTRUCT));
                                pInitStruct^.pluginHandle := PluginHandle;
                                pInitStruct^.sdkVersion := PLUG_SDKVERSION;
                                pInitStruct^.pluginVersion := 1;
                                StrPLCopy(pInitStruct^.pluginName, ExtractFileName(SearchRec.Name), 256);

                                if PlugInit(pInitStruct) then
                                begin
                                  _plugin_logprintf('[MoreTool] 成功加载插件: %s', PAnsiChar(UTF8String(SearchRec.Name)));

                                  @PlugSetupFunc := GetProcAddress(PluginHandle, 'plugsetup');
                                  if Assigned(PlugSetupFunc) then
                                  begin
                                    GetMem(pSetupStruct, SizeOf(PLUG_SETUPSTRUCT));
                                    try
                                      ZeroMemory(pSetupStruct, SizeOf(PLUG_SETUPSTRUCT));
                                      pSetupStruct^.hwndDlg := hMainWindow;
                                      pSetupStruct^.hMenu := hMenu;
                                      pSetupStruct^.hMenuDisasm := hMenuDisasm;
                                      pSetupStruct^.hMenuDump := hMenuDump;
                                      pSetupStruct^.hMenuStack := hMenuStack;
                                      PlugSetupFunc(pSetupStruct);
                                    finally
                                      FreeMem(pSetupStruct);
                                    end;
                                  end;
                                end
                                else
                                begin
                                  _plugin_logprintf('[MoreTool] 插件初始化失败: %s', PAnsiChar(UTF8String(SearchRec.Name)));
                                  FreeLibrary(PluginHandle);
                                end;
                              finally
                                FreeMem(pInitStruct);
                              end;
                            end
                            else
                            begin
                              _plugin_logprintf('[MoreTool] 找不到插件初始化函数: %s', PAnsiChar(UTF8String(SearchRec.Name)));
                              FreeLibrary(PluginHandle);
                            end;
                          end
                          else
                            _plugin_logprintf('[MoreTool] 加载插件失败: %s, 错误: %d', PAnsiChar(UTF8String(SearchRec.Name)), GetLastError());
                        end;
                      until FindNext(SearchRec) <> 0;
                      FindClose(SearchRec);
                    end;

                    {$IFDEF WIN64}
                    if FindFirst(PluginDir + '*.dp64', faAnyFile, SearchRec) = 0 then
                    begin
                      repeat
                        if (SearchRec.Attr and faDirectory) = 0 then
                        begin
                          PluginName := PluginDir + SearchRec.Name;
                          PluginHandle := LoadLibrary(PChar(PluginName));
                          if PluginHandle <> 0 then
                          begin
                            @PlugInit := GetProcAddress(PluginHandle, 'pluginit');
                            if Assigned(PlugInit) then
                            begin
                              GetMem(pInitStruct, SizeOf(PLUG_INITSTRUCT));
                              try
                                ZeroMemory(pInitStruct, SizeOf(PLUG_INITSTRUCT));
                                pInitStruct^.pluginHandle := PluginHandle;
                                pInitStruct^.sdkVersion := PLUG_SDKVERSION;
                                pInitStruct^.pluginVersion := 1;
                                StrPLCopy(pInitStruct^.pluginName, ExtractFileName(SearchRec.Name), 256);

                                if PlugInit(pInitStruct) then
                                begin
                                  _plugin_logprintf('[MoreTool] 成功加载插件: %s', PAnsiChar(UTF8String(SearchRec.Name)));

                                  @PlugSetupFunc := GetProcAddress(PluginHandle, 'plugsetup');
                                  if Assigned(PlugSetupFunc) then
                                  begin
                                    GetMem(pSetupStruct, SizeOf(PLUG_SETUPSTRUCT));
                                    try
                                      ZeroMemory(pSetupStruct, SizeOf(PLUG_SETUPSTRUCT));
                                      pSetupStruct^.hwndDlg := hMainWindow;
                                      pSetupStruct^.hMenu := hMenu;
                                      pSetupStruct^.hMenuDisasm := hMenuDisasm;
                                      pSetupStruct^.hMenuDump := hMenuDump;
                                      pSetupStruct^.hMenuStack := hMenuStack;
                                      PlugSetupFunc(pSetupStruct);
                                    finally
                                      FreeMem(pSetupStruct);
                                    end;
                                  end;
                                end
                                else
                                begin
                                  _plugin_logprintf('[MoreTool] 插件初始化失败: %s', PAnsiChar(UTF8String(SearchRec.Name)));
                                  FreeLibrary(PluginHandle);
                                end;
                              finally
                                FreeMem(pInitStruct);
                              end;
                            end
                            else
                            begin
                              _plugin_logprintf('[MoreTool] 找不到插件初始化函数: %s', PAnsiChar(UTF8String(SearchRec.Name)));
                              FreeLibrary(PluginHandle);
                            end;
                          end
                          else
                            _plugin_logprintf('[MoreTool] 加载插件失败: %s, 错误: %d', PAnsiChar(UTF8String(SearchRec.Name)), GetLastError());
                        end;
                      until FindNext(SearchRec) <> 0;
                      FindClose(SearchRec);
                    end;
                    {$ENDIF}
                  end;
                end
                else
                begin
                  _plugin_logprintf('[MoreTool] INI文件中未找到[Your_Plugins_Folder]节或Path键');
                end;
              finally
                Ini.Free;
              end;
            end
            else
            begin
              _plugin_logprintf('[MoreTool] INI文件不存在: %s', PAnsiChar(UTF8String(IniFilePath)));
            end;
          except
            on E: Exception do
              _plugin_logprintf('[MoreTool] 加载插件时发生错误: %s', PAnsiChar(UTF8String(E.Message)));
          end;
        end;
      end;
  end;
end;

function calc(argc: Integer; argv: PPAnsiChar): Boolean; cdecl;
begin
  ShellExecuteA(GuiGetWindowHandle, 'OPEN', 'calc.exe', '', 'C:\Windows\system32', SW_SHOWNORMAL);
end;

function notepad(argc: Integer; argv: PPAnsiChar): Boolean; cdecl;
begin
  ShellExecuteA(GuiGetWindowHandle, 'OPEN', 'notepad.exe', '', 'C:\Windows\system32', SW_SHOWNORMAL);
end;

function x_dbg_Plugininit(PlugInitInfo: PPLUG_INITSTRUCT): Boolean; cdecl;
begin
  g_pluginHandle := PlugInitInfo^.pluginHandle;
  PlugInitInfo^.sdkVersion := PLUG_SDKVERSION;
  PlugInitInfo^.pluginVersion := PLUGIN_VERS;
  lstrcpyA(PlugInitInfo^.pluginName, PLUGIN_NAME);
  _plugin_registercallback(g_pluginHandle, CB_MENUENTRY, RegisterMenuProc);
  _plugin_registercallback(g_pluginHandle, CB_INITDEBUG, RegisterInitProc);
  Result := True;
end;

procedure x_dbg_Pluginsetup(PlugSetupInfo: PPLUG_SETUPSTRUCT); cdecl;
begin
  g_hMenu := PlugSetupInfo^.hMenu;
  g_hMenuDisasm := PlugSetupInfo^.hMenuDisasm;

  _plugin_menuaddentry(g_hMenuDisasm, MENU_DISASM_NOTEPAD, 'Open Notepad');
  _plugin_menuaddentry(g_hMenuDisasm, MENU_DISASM_CALC, 'Open Calc');
  _plugin_menuaddseparator(g_hMenuDisasm);
  _plugin_menuaddentry(g_hMenuDisasm, MENU_DISASM_LOAD_EXTERNAL_PLUGINS, PAnsiChar(UTF8String('加载外部插件')));

  _plugin_menuaddentry(g_hMenu, MENU_CALC, 'Open Calc');
  _plugin_menuaddentry(g_hMenu, MENU_NOTEPAD, 'Open Notepad');
  _plugin_menuaddseparator(g_hMenu);
  _plugin_menuaddentry(g_hMenu, MENU_LOAD_EXTERNAL_PLUGINS, PAnsiChar(UTF8String('加载外部插件')));

  if not (_plugin_registercommand(g_pluginHandle, 'Calc', calc, false)) then
    _plugin_logputs('[MapMaster] ErroR Registering The "Calc" command! ');
  if not (_plugin_registercommand(g_pluginHandle, 'Notepad', notepad, false)) then
    _plugin_logputs('[MapMaster] ErroR Registering The "Notepad" command! ');
  _plugin_logprintf('[***] %s Plugin v%i by %s '#10, PLUGIN_NAME, PLUGIN_VERS, PLUGIN_AUTH);
end;

function x_dbg_plugstop(): Boolean; cdecl;
begin
  _plugin_unregistercallback(g_pluginHandle, CB_MENUENTRY);
  _plugin_unregistercallback(g_pluginHandle, CB_INITDEBUG);
  Result := True;
end;

exports
  x_dbg_Plugininit name 'pluginit',
  x_dbg_Pluginsetup name 'plugsetup',
  x_dbg_plugstop name 'plugstop';

procedure DLLEntryPoint(dwReason: DWORD);
var
  szPluginName: array[0..MAX_PATH - 1] of ACHAR;
begin
  if (dwReason = DLL_PROCESS_DETACH) then
  begin
    lstrcatA(szPluginName, PLUGIN_NAME);
    lstrcatA(szPluginName, ' Unloaded By DLL_PROCESS_DETACH');
    OutputDebugStringA(szPluginName);
  end;
  if Assigned(SaveDLLProc) then
    SaveDLLProc(dwReason);
end;

begin
  g_Inst := HInstance;
  SaveDLLProc := @DLLProc;
  DLLProc := @DLLEntryPoint;
end.

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
一只梦蝶 + 1 + 1 哇,法王竟然在求助,围观围观
倾情 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

killerzeno 发表于 2025-10-15 14:16
本帖最后由 killerzeno 于 2025-10-15 14:25 编辑

删除声明:
[Asm] 纯文本查看 复制代码
function _plugin_gethmenu(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenudisasm(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenudump(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;
function _plugin_gethmenustack(pluginHandle: THandle): HMENU; cdecl; external X64DBG_CORE_DLL;

改用 bridgemain 提供的函数

[Asm] 纯文本查看 复制代码
g_hMenu := gui_get_hmenu();
g_hMenuDisasm := gui_get_hmenu_disasm();


声明了会导致 Windows 加载器在加载你的插件 DLL 时尝试解析 x64dbg.dll 中的符号,但它 根本不在搜索路径中。

还有确定导出函数大小写等问题是否有缺失。
 楼主| 冥界3大法王 发表于 2025-10-15 17:43
killerzeno 发表于 2025-10-15 14:16
删除声明:[mw_shl_code=asm,true]function _plugin_gethmenu(pluginHandle: THandle): HMENU; cdecl; exte ...

@killerzeno
修改了好多个小时,还是没有整明白。。。
改来改去,不是编译成功了,插件没加载。。。又或者编译错误。。。
头像被屏蔽
xuexiba 发表于 2025-10-15 21:15
头像被屏蔽
lc5715232 发表于 2025-10-16 08:24
提示: 该帖被管理员或版主屏蔽
m_h 发表于 2025-10-17 20:53
本帖最后由 m_h 于 2025-10-17 20:55 编辑

sizeof
内存对齐
call 传递方式     cdecl;  还有好几个 我记得 vc++的好像还有一个pascal没有的?
https://blog.csdn.net/dbyoung/article/details/112646948  我就记得这老哥  在搞 d c++ 通用。。。。
https://github.com/dbyoung720/DOpenCV   Delphi 调用 OpenCV C++ 类 Dll
这个也是他的  https://github.com/dbyoung720/DCallCppClassDll  Delphi 调用 C++ 类 dll
不管你用做什么 最少保证 传递参数一样  结构体大小一样  结构体 内存对齐 一行 才能做基本的通过


我只搞过 c 的 让d调用
cl gcc 的 obj o 让d调用 windows(gcc有bug不能用) linux(gcc)  安卓没测试 用不着懒得搞。
clang的还没试过呢。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-6-4 06:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表