【问题标题】:C++ Access Violation while calling dll function调用 dll 函数时 C++ 访问冲突
【发布时间】:2014-03-19 21:48:54
【问题描述】:

我实际上使用的是非托管 C++ DLL,我无法访问 .h、.cpp 或 .lib,而只能访问 .DLL。

使用PE Explorer 并找到我想使用的功能后,我得到了:

@Tdtm_Dossier@Logon$qv; Index 1310; Unmangled Borland C++ Function: qualified function Tdtm_Dossier::Logon()

这是我使用dumpbin 得到的结果:

1310 11F9 00105234 @Tdtm_Dossier@Logon$qv

这是一个例外:

Unhandled Exception at 0x034B258C (modDll.dll) in functionsCpp.exe : 0xC0000005 : 
Access violation writting to 0x000000AC.

我用来调用和使用这个函数的代码如下:

#include <stdio.h>
#include <Windows.h>
#include <iostream>

typedef int (*Logon)();

int main()
{
  HMODULE modDll;
  int resultLogon;
  modDll= LoadLibrary("C:\\dll\\modDll.dll");

  Logon logon;
  logon = (Logon)GetProcAddress(modDll,"@Tdtm_Dossier@Logon$qv");

  if(logon)
  {
    resultLogon = logon(); //<-- This is where I get the exception
    printf("Function has been loaded\n");
  }
  else
   // TODO: Error message

  FreeLibrary(modDll);
}

由于 DLL 文档没有给我任何关于如何使用该函数的有趣信息,我不能指望它。

DLL 已正确加载,GetProcAddress 确实返回了一些内容。我猜(但我不确定)它与我的typedef 有关,但我无法弄清楚这个函数的返回类型可能是什么。

【问题讨论】:

  • @cup 是的,我已经检查过了,GetProcAddress 也是。它们都不是NULL。
  • 尝试使用depends找出签名是什么。
  • 你确定函数签名吗?它真的返回int 并且不带任何参数吗?

标签: c++ exception dll


【解决方案1】:

如果您阅读例如this document on Borland C++ name mangling 您可能会发现符号名称 "@Tdtm_Dossier@Logon$qv" 表示类 Tdtm_Dossier 的非静态 member 函数。你不能像普通函数那样调用非静态成员函数,它们有一个隐藏的第一个参数,成为成员函数中的this 指针。

这里发生的情况可能Logon 成员函数试图访问对象实例的成员变量,但没有这些成员变量,这会导致未定义的行为和崩溃。

为了能够使用这个库,您需要头文件和链接库。您不能只调用函数(成员或非成员)并希望获得最好的结果。

【讨论】:

  • 所以这意味着无论如何我都必须得到那个 DLL 的 .h 和 .lib ?我猜没有其他方法可以做到吗?感谢您的评论!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多