c++ - 为特定的 mfc dll 调用 AfxEnableMemoryLeakDump

标签 c++ visual-studio-2010 mfc afx

我们有一个 visual studio Unicode 应用程序,我们在其中使用了一些外部 dll。在此应用程序中加载了 mfc100ud.dll(注意 u,它代表 Unicode)。该应用程序还使用了一些与 mfc100d.dll 链接的外部 dll(因此没有 Unicode)。

在我们的应用程序中,我想通过调用 AfxEnableMemoryLeakDump(FALSE) 禁用作为 Afx 一部分的内存泄漏转储。当我调用这个函数时,我最终进入了 mfc100ud.dll,因为我们直接链接到这个 dll。但是稍后加载了外部 dll,因此也加载了 mfc100d.dll。 当应用程序关闭时,mfc100d.dll 被卸载,并且由于没有为此 dll 调用 AfxEnableMemoryLeakDump,我们仍然会发生 MemoryLeakDump。

为了解决这个问题,我尝试通过以下方式显式调用 dll 中的函数:

  PGNSI pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("mfc100d.dll")),
       "?AfxEnableMemoryLeakDump@@YGHH@Z"); // 64-bit
  if (pGNSI!=nullptr)
  {
    pGNSI(FALSE);
  }
  pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("mfc100d.dll")),
       "?AfxEnableMemoryLeakDump@@YAHH@Z"); // 32-bit
  if (pGNSI!=nullptr)
  {
    pGNSI(FALSE);
  }

我使用 dumpbin.exe 查找修饰函数名称。

但这不起作用,因为 GetProcAddress 为 32 位和 64 位返回一个 nullptr。 有人可以帮忙吗?

最佳答案

AfxenableMemoryLeakDump 不是按名称导出,而是按序数值导出。您可以通过 dumpbin 显示的 [NONAME] 标记来判断这一点。这是我得到的:

C:\Windows\System32>dumpbin /exports mfc100d.dll | grep AfxEnableMemoryLeakDump
      15902      003A20D0 [NONAME] ?AfxEnableMemoryLeakDump@@YGHH@Z (int __stdcall AfxEnableMemoryLeakDump(int))

第一个值 15902 是序数值。 GetProcAddress文档说明:

lpProcName [in] The function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.

那么试试

const WORD AfxEnableMemoryLeakDumpOrdinal = 15902;
GetProcAddress( GetModuleHandle( ... ), (LPCSTR)AfxEnableMemoryLeakDumpOrdinal );

关于c++ - 为特定的 mfc dll 调用 AfxEnableMemoryLeakDump,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11412650/

相关文章:

c++ - Win32/MFC 从客户端矩形获取窗口矩形

c++ - 使用 CFileDialog 打开文件失败时如何捕获异常

c++ - 在 C++ 中包含文件?

c++ - 移动构造函数似乎没有执行

visual-studio-2010 - 如何在 VS2010 中公开 MSBuild 'Conditions'(对于 vc++)

visual-studio - Visual Studio 不断构建一切

c# - 比较 Winform 按钮颜色

c++ - Visual Studio MFC CListCtrl 复选框 - 空格键

c++ - 来自宏的警告

c++ - QMessageBox 不显示整个标题