Unable to load C ++ DLL in C #

> My previous thread <

I created this because I installed WinXP on VMBox and I cannot get it to work again.

This time I created an OnLoad event in my form

if (LoadLibrary("blowfish.dll") == 0) { Misc.LogToFile("Could not load dll", true); Application.Exit(); } 

Works fine on my PC, but on VMBox the LoadLibrary returns 0.

Some users mentioned that the problem is mixing the old version of the .NET Framework (2.0) with the dll created on the latest MS Visual Studio (2008 SP1), so I took measures and now the properties of the program that he set to work with NET 3.5

On VMBox, I have NET 2.0, but this is not a problem - the program itself works fine. I also have C ++ Redistributable (2005,2005 SP1 and 2008).

What could be the problem?

+2
c ++ c # dll crash
source share
4 answers

For further problems call

 Marshal.GetLastWin32Error(); 

which should give you an error code.

Is it possible that you have deployed a debug version of your native dll that also requires a debug version of MSVCR90 D .DLL? You had to distribute the release version because a different set of DLLs must be installed on the target system for the debug version.

It clearly works on your development machine because all debug versions of the required libraries come with Visual Studio.

Here's how you get the message belonging to the error code:

 [DllImport("kernel32.dll")] private static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, out string lpBuffer, int nSize, IntPtr pArguments); public static string GetErrorMessage(int errorCode) { const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; string lpMsgBuf; int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; int retVal = FormatMessage(dwFlags, IntPtr.Zero, errorCode, 0, out lpMsgBuf, 0, IntPtr.Zero); if (0 == retVal) { return null; } return lpMsgBuf; } 
+3
source share

Call GetLastError after LoadLibrary, check the error code here: http://msdn.microsoft.com/en-us/library/ms681381.aspx and see if that helps.

+1
source share

Perhaps the dll location is on the path in one environment and not in another. It may also be permissions in one environment, not like others.

0
source share

Try to run the dependent walker in the DLL - see if any modules are missing.

0
source share

All Articles