C# – 32 bit dllimport generating incorrect format error (0x8007000b) on win7 x64 platform

64-bitcdllimportmigration

I'm trying to install and run a 32 bit application on a Win7 x64 machine. The application is built as a Win32 app. It runs fine on 32 bit platforms. On the x64 machine it installs correctly in the Programs(x86) directory and runs fine until I make a call into a 32 bit dll. At that time I get the incorrect format error (0x8007000b) indicating it is trying to load the dll of the wrong bitness. Indeed it is trying to load the 64 bit dll from the System32 directory rather than the 32 bit version in the SystemWOW64 directory.

Another 32 bit application provided by the dll vendor runs correctly and it does load the 32 bit dll from the SystemWOW64 directory. I do not have source to their application to see how they are accessing the DLL.

I'm using the DllImport function as shown below to access the dll. Is there a way to decorate the DllImport calls to force it to load the 32 bit version?

Any thoughts appreciated.

Thanks, DP

public static class Micronas
{
    [DllImport(@"UAC2.DLL")]
    public static extern short UacBuildDeviceList(uint uFlags);
    [DllImport(@"UAC2.DLL")]
    public static extern short UacGetNumberOfDevices();
    [DllImport(@"UAC2.DLL")]
    public static extern uint UacGetFirstDevice();
    [DllImport(@"UAC2.DLL")]
    public static extern uint UacGetNextDevice(uint handle);
    [DllImport(@"UAC2.DLL")]
    public static extern uint UacSetXDFP(uint handle, short adr, uint data);
    [DllImport(@"UAC2.DLL")]
    public unsafe static extern uint UacGetXDFP(uint handle, short adr, IntPtr data);
}

Best Answer

Force your .exe to run in 32-bit mode. Project + Properties, Build tab, Platform Target = x86.

Avoid getting confused by the project's Platform setting, it doesn't actually have to match the real setting. Which is Project + Properties, Build tab, Platform Target. Bit of a mess there.