views:

814

answers:

3

Hello,

Right after I moved from XP to Vista,I realized my C# programs don't work.

This is the situation: I wrote a C++ dll that I use in my C# application.The DLL worked fine in XP,but when I moved to Vista it's not longer working in C#.

I tested it in Delphi,works fine,but C# - no.

I wrote additional code to make my check easier in C#.

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

It doesn't need C++ runtime,because Its compiled with the libraries and it works in Delphi on Vista,but not C#.

Where could the problem be?

Thanks in advance.

+8  A: 

On x64 platform the JIT will compile your program into x64, since your native C++ is compiled to x86 it will fail to load it.
You need to explicitly tell the JIT to compile your program to x86, you can do it using CorFlags or the project settings set the CPU type to x86 (Under Build/Platform target)

Shay Erlichmen
Thanks,this solved my problem!
John
+1  A: 

Shay has the quick fix - make your whole application 32 bit so it runs under WOW64.

However, the "better" solution is to rebuild your C++ dll as 64-bit code so that your entire program can run natively on the 64-bit OS.

Jason Williams
+1  A: 

If you compile normally, the CLR will run your app as 64-bit on x64 Windows and 32-bit on x86 Windows. You have to load the correct native image for the platform. One solution is to do as Shay suggested and force your app to run in a 32-bit CLR.

You can also make your application look at the native pointer size and load the correct native image.

string blowfishdll = "blowfish.dll";
// detect 64-bit installations by looking at the native pointer size
if( 64 == IntPtr.Size * 8  )
    blowfishdll = "blowfish-x64.dll"

if (LoadLibrary( blowfishdll ) == 0)        
{
    Misc.LogToFile("error", true);            
    Application.Exit();        
}
Brian Reiter