BadImageFormatException with C# and .NET 2.0 under Vista x64

I can’t even load the k8055d.dll when trying to OpenDevice(0).
All I get is an annoying BadImageFormatException.

I have tried the original dll as well as rev 2 and 3.0.2 with the same result. Everything worked just fine under Windows XP and .NET 1.1.

How to do to get it work under .NET 2.0 with C# under Vista x64?
Down below is the approach that worked fine under .NET 1.1 and Windows XP.

Does anyone know a solution for this?


public class USBIO
{
[DllImport(“k8055d.dll”)]
public static extern int OpenDevice(int cardAddress);
}

USBIO.OpenDevice(0);

BadImageFormatException Class
The exception that is thrown when the file image of a DLL or an executable program is invalid.

The 64-bit .NET framework cannot load a 32-bit native dll (non-.NET dll). If you wrote the application yourself, you can try forcing it into 32-bit mode.

Thanks for the reply.

Now it works after have forcing my application to run in 32 bit mode!

Nice :slight_smile:

Could you post how you forced it into 32-bit mode? In case someone else runs into the same problem :slight_smile:

Howto force an application to run in 32-bit mode on x64 using Visual Studio 2005 and 2005 Express versions:

For C# Projects:

  1. Right click the project in the solution explorer and open ‘properties’
  2. Choose the Build tab
  3. Set the Platform Target property to ‘X86’

For VB Projects:

  1. Right click the project in the solution explorer and open ‘properties’
  2. Choose the Compile tab
  3. Press the Advanced Compile Options… button
  4. Set the Target CPU property to ‘X86’

Express Editions:
The VB and C# Express products do not expose the Target property inside the development environment. You will need to carefully modify the project file using a text or XML editor.

  1. Close the project and/or solution
  2. Select Open File from the File menu
  3. Navigate to the project directory, and highlight the project file
  4. Press the Open button, the project file should open in the XML editor
  5. Locate the first section and add the following line:
    x86
  6. Save the project file
  7. Reopen the project and/or solution using Open Project/Solution from the File menu
  8. Continue with development, debugging, and testing

I had to edit the project file since I’m using the Express version. Hope this helps someone!