Wrapping a DLL - Understanding why Import Fails

1

Background

I have an embedded system which runs an application originally written in C++ and compiled in Visual Studio, which results in a single executable and more than 30 DLLs. These libraries cannot be browsed in VS Object Browser or other tools such as P/Invoke Interop Assistant.

Loading some of the DLLs in Dependency Walker shows that all of them are missing some dependencies deep in their dependency tree (cdfview.dll, dwmapi.dll, w32topl.dll, ...) but according to this question, that is likely not an issue.

I have some of the source code files, and all of the compiled DLLs. The application currently runs without issue, indicating there are no real dependency issues.

I am trying to call some library functions and eventually make a wrapper using C#, but am unable to successfully import and call even the simplest of functions. I always receive the following error:

Unable to load DLL 'dllName.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

Sample Code [EDITED]

From the C++ source code header file I have the following declarations:

#define OB_API __declspec(dllexport) __cdecl 

typedef unsigned long DWORD;  // From windef.h
typedef DWORD OBSTATUS;

OBSTATUS OB_API TestObj(void);

In the C++ source code file the following definition is given (which would seem to always return true):

BOOL WINAPI DllMain(HANDLE  /* hModule */, 
                      DWORD   ul_reason_for_call, 
                      LPVOID  /* lpReserved */
                      )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_PROCESS_DETACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}

In my C# application class, I add the following declaration:

[DllImport(@"dllName.dll", CallingConvention=CallingConvention.Cdecl)
public static extern ulong TestObj();

The DLL and C# application binary reside in the same directory.

Questions

From researching the error, it seems there is a large number of reasons this particular exception could be thrown and I was wondering how I could further troubleshoot this type of issue.

Is there any way to get more detailed information on why the initialization routine failed?

(Note: target system is running .NET framework 2.0)

c#
c++
dll
dllimport
asked on Stack Overflow Dec 18, 2012 by nicholas • edited May 23, 2017 by Community

1 Answer

4

The DLL you are loading contains a DllMain() function. That's pretty common, such a function initializes the state of the DLL. Windows makes sure that this function is called whenever the DLL gets loaded, it will happen automatically the first time you pinvoke a function exported by that DLL.

Problem is, that function returned FALSE to indicate that it could not properly initialize the DLL. That of course does not give a wholeheckofalot of information about why it returned FALSE. Windows can't do anything but generate error 1114, ERROR_DLL_INIT_FAILED. If the DLL itself doesn't output any diagnostic then you can't do anything but debug the code. Start with Project + Properties, Debug tab, tick the "Enable unmanaged code debugging" option.

Fingers crossed that you see a message in the Output window. Odds are not very good. If you don't have the source code for the DLL then you'll need the help of the vendor or author of the DLL. Give him a copy of a test project that fails like this.

answered on Stack Overflow Dec 18, 2012 by Hans Passant

User contributions licensed under CC BY-SA 3.0