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)