Question

We have a visual studio Unicode application where we use some external dlls. In this application mfc100ud.dll is loaded (notice the u, which stands for Unicode). The application also uses some external dlls which are linked with mfc100d.dll (so no Unicode).

In our application I want to disable the memory leak dump which is part of Afx by calling AfxEnableMemoryLeakDump(FALSE). When I call this function I end up in mfc100ud.dll since we directly link with this dll. However later on the external dll is loaded, and hence mfc100d.dll is also loaded. When the application is closed, mfc100d.dll is unloaded and since AfxEnableMemoryLeakDump wasn't called for this dll, we MemoryLeakDump still happens.

To solve this I tried to explicitly call the function in the dll by doing this:

  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);
  }

I used dumpbin.exe to look up the decorated function names.

But this doesn't work since GetProcAddress returns a nullptr both for 32-bit and 64-bit. Can somebody help with this?

Was it helpful?

Solution

AfxenableMemoryLeakDump is not exported by name but by its ordinal value. You can tell this by the [NONAME] marker shown by dumpbin. Here's what I get:

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

The first value, 15902 is the ordinal value. The GetProcAddress documentation explains:

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.

So try

const WORD AfxEnableMemoryLeakDumpOrdinal = 15902;
GetProcAddress( GetModuleHandle( ... ), (LPCSTR)AfxEnableMemoryLeakDumpOrdinal );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top