DllNotFoundException From C# but not VB.NET

0

I have a third party DLL to integrate with their application. It is a 32 bit C++ DLL that does not expose itself via COM, and I have not been supplied source code or a header file.

It was supplied with working VB.NET example code in a WinForms example. This works, as long as the executable is run from the same directory as the API DLL and the application for which the API is interfacing.

I was using the example function declaration:

Declare Ansi Function GetVersion Lib "ThirdPartyAPI.dll" () As Integer

However, we’ll be using the API calls from a C# library to be used in a web site or web service, so I converted the example VB code to

[DllImport("ThirdPartyAPI.dll", SetLastError = true, CharSet = CharSet.Ansi)]
public static extern int GetVersion();

If I call this from within the C# library I get the error

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

I took an alternative route and put the original working VB code from the example into its own VB.NET DLL and called that via C#. I got the same error. Strangely, a call to the VB.NET DLL from a VB.NET console app works.

In summary:

  • A simple VB.NET console app will work
  • A simple VB.NET WinForms app will work
  • A simple VB.NET console app can call GetVersion via a VB.NET DLL
  • A simple C# console app cannot call GetVersion via a VB.NET DLL
  • A simple C# console app cannot call GetVersion directly. This could be to do with incorrectly defining the function calls. At this stage, I’m not too worried about this.
  • 32 bit compilation does not fix the issue
  • I have tried this on Server 2008 R2 and Windows 7 Enterprise

I created a WCF host via an NT service to expose the functions, hoping this would work around file locations and dependencies. This was written in VB.NET and placed in the same folder as ThirdPartyApi.dll.

  • None of the code in the NT service could access GetVersion
  • None of the code in the WCF service could access GetVersion

Please note, in all of the above cases, the executables are in the same directory as ThirdPartyApi.dll. All of its dependencies also exist in this directory.

Can anyone explain this behaviour, or suggest how this can be made to work within a C# web site?

Thanks

c#
c++
vb.net
pinvoke
dllnotfoundexception
asked on Stack Overflow Jun 18, 2014 by mooncat69 • edited Jun 19, 2014 by mooncat69

1 Answer

0

The error code is 0x8007045A. That is a COM error code that maps to the Win32 error code ERROR_DLL_INIT_FAILED. Which is described as:

A dynamic link library (DLL) initialization routine failed.

What this means is that your DLL was found, in spite of the perhaps misleading exception class DllNotFoundException. However, the DLL failed to load. You can gain no more information from the system. There is no mechanism for the DLL to report reasons why it failed to load. I suggest that you contact the vendor of the DLL and ask for support.

answered on Stack Overflow Jun 19, 2014 by David Heffernan

User contributions licensed under CC BY-SA 3.0