Why might I be getting System.BadImageFormatException from a C# console app that calls sub20dnc_v4.dll?

57 views Asked by At

I'm trying to create a C# console app that accesses a USB device via sub20dnc_v4.dll (a third-party driver and .Net library available at http://www.xdimax.com/sub20/sub20.html#DLD). The library is available in both a 32- and 64-bit version. I'm using .Net version 7.0.203, and the library version is 1.4.28.0. The library creator claims on its website that C# is supported, and doesn't mention any version limitations.

It's a very simple test program that is just scanning for connected SUB-20 USB device(s) and displaying their serial #'s. Here's program.cs:

using Xdimax;

Sub20Enum sub20Enum = new();
Sub20 sub20 = new();

ulong deviceRef = sub20Enum.GetNext(0);
while (deviceRef > 0)
{
    if (sub20.Open(deviceRef))
    {
        var sn = sub20.GetSerialNumber();
        Console.WriteLine($"deviceRef: {deviceRef}  sn: {sn}");
        sub20.Close();
    } else
    {
        var errMessage = sub20.GetStrError(sub20.GetLastError());
        Console.WriteLine($"Open failed: {errMessage}");
    }
    deviceRef = sub20Enum.GetNext(deviceRef);
}

My program runs fine when debugging but after I've built and published it to an exe, running reports a System.BadImageFormatException on the sub20dnc_v4.dll file.

The posts I've read related to System.BadImageFormatException suggest a conflict in bitness between the app's processor target and that of the library. I've checked and re-checked values for PlatformTarget in .csproj and --runtime in my dotnet publish command to ensure they match the library version I'm using, but I continue to get this exception when running the published exe. This occurs regardless of whether I build it as a 32-bit app using their x86 library version or as a 64-bit app using their x64 library version.

I don't know if perhaps the library isn't the bitness that the creator claims (x86 version isn't really x86 or x64 version isn't really x64)? I can't imagine the library is damaged in some way because the app runs fine when debugging. I tried running corflags against both versions of the library based on another post I'd read about this sort of issue, but I'm not familiar enough with corflags to understand the output or try to change flags.

An additional challenge is the library is fairly old and the creator's forum isn't too active (I registered for a forum account so I could ask them directly about this but still waiting for my forum account to be created - argh...).

If anyone has encountered this issue with this library, or has run into a similar issue with a different 3rd-party library, I'd really appreciate advice on how to fix or diagnose further. Right now it's a black-box to me.

Thank you!

0

There are 0 answers