【问题标题】:How do I debug a privileged-instruction fault from calling a DLL function?如何通过调用 DLL 函数来调试特权指令错误?
【发布时间】:2012-09-06 08:45:23
【问题描述】:

我为我们无法控制的第 3 方 DLL 定义了以下函数:

const
  DLL = 'Qarapea.dll';

function QARapid_Open(IniFile: shortstring; Section: shortstring): integer; stdcall; external DLL;
procedure QARapid_EndSearch(); stdcall; external DLL;
function QARapid_Count: integer; stdcall; external DLL;
function QARapid_Search(vs: shortstring): integer; stdcall; external DLL;
function QARapid_FormatAddr(ItemNumber: integer; Buffer: PAnsiChar; BufferSize: integer):     integer; stdcall; external DLL;
procedure QARapid_Close; stdcall; external DLL;

我通过以下方式调用函数:

procedure TFormMain.ButtonStaticLookupClick(Sender: TObject);
var
  Res: integer;
  ACode: shortstring;
  IniFile, Section: shortstring;
begin
  try
    ACode := PrepareCode(EditCode.Text);
    IniFile := ExtractFilePath(ParamStr(0)) + 'DllIni.ini';
    Section := 'Default';
    QARapid_Open(IniFile, Section);
    try
      Res := QARapid_Search(ACode);
      Res := QARapid_Count;

    finally
      QARapid_Close;
    end;

  Except on E: Exception do
    MessageDlg(E.Message, mtError, [mbOK], 0);
  end;
end;

在我调用QARapid_Count 函数之前,一切似乎都正常,当我收到以下错误时:

QAS.exe 出现错误消息:0x0012eff4 处的特权指令。 进程停止。使用 step 或 run 继续。

由于打开了 CPU 调试窗口,我不知道从哪里开始查找故障。

如何追踪出了什么问题?

【问题讨论】:

  • 'QARapid_Open' 的调用约定不同吗?还是复制粘贴错误?
  • 您确定您正确翻译了 API 吗?使用 DLL,您无法保证您的标头不会过时或通过其他方式正确。假设您的 DLL 本身是用 C++Builder 或 Delphi 编写的,它可能具有不同版本的 FastMM4,并且当 EXE 和 DLL 尝试合并它们的堆池时 - 会破坏彼此的结构。不过只是推测。希望它不应该是这样的。
  • 我的意思是它缺少 stdcall 指令。是故意的吗?
  • 另外,什么样的糟糕 DLL 使用短字符串?一个只能从 Delphi 合理调用的 DLL!
  • @PietervanWyk 所以你只是在猜测如何调用这个 DLL? shortstring 不太合适。

标签: delphi debugging dll


【解决方案1】:

阅读QuickAddress Rapid API guide,然后相应地修复您的 Delphi 导入。

【讨论】:

  • 感谢您提供的文档。我用 AnsiString 替换了所有字符串变量,用 PAnsiChar 替换了 PChar(参见 GolezTrol 的评论)。函数调用现在可以正常工作了。
  • XE2 移植并没有那么困难(:获取正确的文档或/和头文件是最重要的一步(现在我正在研究 WebSphere MQ DLL 的 Delphi 接口,它是很多工作,但难度水平还可以,IBM在他们的C头文件中犯的错误数量也可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多