Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm stuck as to why I can't load my dll "interfac" using LoadLibrary. It seems to be failing when loading a dependency but I'm not sure why.

Here's the code:

AfxMessageBox(L"before load library");
HMODULE interfacDll = LoadLibrary(TEXT("C:\QA\Pcdlrn\Win32\Release\INTERFAC.DLL"));
if (!interfacDll)
  DWORD dw = GetLastError(); // returns 0xc1 (193)
AfxMessageBox(L"after load library");

And here's the output from gflags (x86)'s loader snaps:

18a0:2a40 @ 06858973 - LdrGetDllHandleEx - ENTER: DLL name: ntdll.dll DLL path: NULL
18a0:2a40 @ 06858973 - LdrGetDllHandleEx - INFO: Locating DLL ntdll.dll in path C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:Windows
18a0:2a40 @ 06858989 - LdrpFindLoadedDll - ENTER: DLL name: ntdll.dll DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:Windows
18a0:2a40 @ 06858989 - LdrpFindLoadedDll - RETURN: Status: 0x00000000
18a0:2a40 @ 06858989 - LdrGetDllHandleEx - RETURN: Status: 0x00000000
18a0:2a40 @ 06858989 - LdrGetProcedureAddressEx - INFO: Locating procedure "KiUserExceptionDispatcher" by name
18a0:2a40 @ 06858989 - LdrLoadDll - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssyste
18a0:2a40 @ 06858989 - LdrpLoadDll - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssyst
18a0:2a40 @ 06858989 - LdrpLoadDll - INFO: Loading DLL C:QAPcdlrnWin32ReleaseINTERFAC.DLL from path C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssys
18a0:2a40 @ 06858989 - LdrpFindOrMapDll - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windows
18a0:2a40 @ 06858989 - LdrpSearchPath - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowss
18a0:2a40 @ 06858989 - LdrpResolveFileName - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL
18a0:2a40 @ 06858989 - LdrpResolveFileName - RETURN: Status: 0x00000000
18a0:2a40 @ 06858989 - LdrpResolveDllName - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL
18a0:2a40 @ 06858989 - LdrpResolveDllName - RETURN: Status: 0x00000000
18a0:2a40 @ 06858989 - LdrpSearchPath - RETURN: Status: 0x00000000
18a0:2a40 @ 06859036 - LdrpMapViewOfSection - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL
'CNCServer.exe': Loaded 'C:QAPcdlrnWin32ReleaseINTERFAC.dll', Symbols loaded.
18a0:2a40 @ 06859098 - LdrpMapViewOfSection - RETURN: Status: 0x40000003
18a0:2a40 @ 06859098 - LdrpRelocateImage - ENTER: DLL name: C:QAPcdlrnWin32ReleaseINTERFAC.DLL
18a0:2a40 @ 06859098 - LdrpProtectAndRelocateImage - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpRelocateImage - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpFindOrMapDll - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpHandleOneOldFormatImportDescriptor - INFO: DLL "C:QAPcdlrnWin32ReleaseINTERFAC.DLL" imports "PCDLRN.exe"
18a0:2a40 @ 06859098 - LdrpLoadImportModule - ENTER: DLL name: PCDLRN.exe DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:W
18a0:2a40 @ 06859098 - LdrpFindOrMapDll - ENTER: DLL name: PCDLRN.exe DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:Windo
18a0:2a40 @ 06859098 - LdrpFindKnownDll - ENTER: DLL name: PCDLRN.exe
18a0:2a40 @ 06859098 - LdrpFindKnownDll - RETURN: Status: 0xc0000135
18a0:2a40 @ 06859098 - LdrpSearchPath - ENTER: DLL name: PCDLRN.exe DLL path: C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:Windows
18a0:2a40 @ 06859098 - LdrpResolveFileName - ENTER: DLL name: C:QAPcdlrnCNCSERVERWin32ReleasePCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveFileName - RETURN: Status: 0xc0000135
18a0:2a40 @ 06859098 - LdrpResolveFileName - ENTER: DLL name: C:Windowssystem32PCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveFileName - RETURN: Status: 0xc0000135
18a0:2a40 @ 06859098 - LdrpResolveFileName - ENTER: DLL name: C:WindowssystemPCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveFileName - RETURN: Status: 0xc0000135
18a0:2a40 @ 06859098 - LdrpResolveFileName - ENTER: DLL name: C:WindowsPCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveFileName - RETURN: Status: 0xc0000135
18a0:2a40 @ 06859098 - LdrpResolveFileName - ENTER: DLL name: .PCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveFileName - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpResolveDllName - ENTER: DLL name: .PCDLRN.exe
18a0:2a40 @ 06859098 - LdrpResolveDllName - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpSearchPath - RETURN: Status: 0x00000000
18a0:2a40 @ 06859098 - LdrpMapViewOfSection - ENTER: DLL name: C:QAPcdlrnWin32ReleasePCDLRN.exe
'CNCServer.exe': Loaded 'C:QAPcdlrnWin32ReleasePCDLRN.exe', Symbols loaded.
18a0:2a40 @ 06859597 - LdrpMapViewOfSection - RETURN: Status: 0x40000003
'CNCServer.exe': Unloaded 'C:QAPcdlrnWin32ReleasePCDLRN.exe'
18a0:2a40 @ 06859597 - LdrpFindOrMapDll - RETURN: Status: 0xc000007b
18a0:2a40 @ 06859597 - LdrpLoadImportModule - ERROR: Loading DLL PCDLRN.exe from path C:QAPcdlrnCNCSERVERWin32Release;C:Windowssystem32;C:Windowssystem;C:Windows;.;C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDECommonExtensionsMicrosoftTeamFoundationTeam ExplorerNativeBinariesx86;C:windowssystem32;C:windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:
18a0:2a40 @ 06859597 - LdrpLoadImportModule - RETURN: Status: 0xc000007b
18a0:2a40 @ 06859597 - LdrpHandleOneOldFormatImportDescriptor - ERROR: Loading "?????" from the import table of DLL "C:QAPcdlrnWin32ReleaseINTERFAC.DLL" failed with status 0xc000007b
18a0:2a40 @ 06859613 - LdrpUnloadDll - INFO: Unmapping DLL "C:QAPcdlrnWin32ReleaseINTERFAC.DLL"
'CNCServer.exe': Unloaded 'C:QAPcdlrnWin32ReleaseINTERFAC.dll'
18a0:2a40 @ 06859613 - LdrpLoadDll - RETURN: Status: 0xc000007b
18a0:2a40 @ 06859613 - LdrLoadDll - RETURN: Status: 0xc000007b

I should mention that I am using 64-bit windows but all my executable code is either targeted to x86 (c#) or win32 (c++).

How do I resolve this issue?

Any suggestions would be greatly appreciated. Thank you very much.

p.s. Here's the dependency walker output: INTERFAC.dll

p.p.s. here's a screenshot showing that my DLLs are x86:

x86

p.p.p.s. here's the linker inputs for interfac.dll:

linker inputs

Edit. Here's the output of dumpbin:

Microsoft (R) COFF/PE Dumper Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file pcdlrn.exe

File Type: EXECUTABLE IMAGE

Section contains the following exports for PCDLRN.exe

00000000 characteristics
5796AD2A time date stamp Mon Jul 25 20:22:02 2016
    0.00 version
       1 ordinal base
    5784 number of functions
    5784 number of names

ordinal hint RVA      name

     1    0 0029CDF0 ??0?$CArray@V?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@V12@@@QAE@XZ = ??0?$CArray@V?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@V12@@@QAE@XZ (public: __thiscall CArray<class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >,class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > >::CArray<class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >,class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > >(void))
...
5783 1696 02593550 ?zoomOutFine@VisionTargetControls@UIEvents@@YAXXZ = ?zoomOutFine@VisionTargetControls@UIEvents@@YAXXZ (void __cdecl UIEvents::VisionTargetControls::zoomOutFine(void))
5784 1697 02593640 ?zoomOutMaximum@VisionTargetControls@UIEvents@@YAXXZ = ?zoomOutMaximum@VisionTargetControls@UIEvents@@YAXXZ (void __cdecl UIEvents::VisionTargetControls::zoomOutMaximum(void))

Summary

  2C0000 .data
    1000 .fnp_dir
    1000 .fnp_mar
  AFA000 .rdata
   1B000 .rsrc
 2E7A000 .text
   E5000 .textidx
    1000 .tls
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
314 views
Welcome To Ask or Share your Answers For Others

1 Answer

One likely explanation for the ERROR_BAD_EXE_FORMAT error from LoadLibrary is that INTERFAC.DLL was linked with a PCDLRN.LIB import library that declares PCDLRN exports, but the PCDLRN.EXE found at runtime does not have an exports table.

  • The gflags (x86)'s loader snaps shows that the error is triggered while loading PCDLRN.EXE, specifically while resolving the import table of INTERFAC.DLL:

    18a0:2a40 @ 06859098 - LdrpHandleOneOldFormatImportDescriptor - INFO: DLL "C:QAPcdlrnWin32ReleaseINTERFAC.DLL" imports "PCDLRN.exe"
    [...]
    18a0:2a40 @ 06859597 - LdrpHandleOneOldFormatImportDescriptor - ERROR: Loading "?????" from the import table of DLL "C:QAPcdlrnWin32ReleaseINTERFAC.DLL" failed with status 0xc000007b
  • Status 0xc000007b is STATUS_INVALID_IMAGE_FORMAT:

    {Bad Image} %hs is either not designed to run on Windows or it contains an error. Try installing the program again using the original installation media or contact your system administrator or the software vendor for support.

  • NTSTATUS STATUS_INVALID_IMAGE_FORMAT is mapped to Win32 error code ERROR_BAD_EXE_FORMAT per INFO: Mapping NT Status Error Codes to Win32 Error Codes.

I was able to duplicate the same ERROR_BAD_EXE_FORMAT with a mockup of the given scenario: link INTERFAC.DLL to a PCDLRN.LIB that declares an exported foo, then substitute a PCDLRN.EXE without an exports table, and finally attempt to LoadLibrary INTERFAC.DLL.

As a side note, linking a DLL to the import library of another EXE is uncommon and fraught with pitfalls. In the case here, even if error 193 is fixed, the functions in the EXE would not be directly usable from the DLL. See for example Load EXE as DLL: Mission Possible for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...