You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Flight Simulator插件开发遇System.BadImageFormatException求助

Fixing System.BadImageFormatException with SimConnect for Microsoft Flight Simulator

Common Causes & Solutions

1. Verify Complete x86 Configuration

  • Ensure both project build settings and solution build settings are set to x86. It's easy to change one but overlook the other.
  • Confirm you're referencing the x86 version of SimConnect.dll from the SDK's lib\x86 directory. Using the x64 DLL in an x86 project will trigger this error.

2. Correct Platform Target Settings

  • In Visual Studio, go to Project Properties > Build:
    • Set Platform target to x86 (not "Any CPU").
    • Uncheck "Prefer 32-bit" if enabled—this setting can cause conflicts with explicit x86 requirements.

3. Clean Up Conflicting Binaries

  • Run Build > Clean Solution to remove any leftover x64 files from previous builds.
  • Rebuild the project to ensure all binaries are compiled for x86.

4. Check SimConnect Version Compatibility

  • Ensure your SimConnect SDK version matches the version of Microsoft Flight Simulator you're running. Mismatched versions can lead to format errors.

5. Fix the Window Handle Issue in Code

Your current code uses IntPtr(0) as the window handle, which is invalid for SimConnect's message-based communication. Console apps don't have a proper message loop, so switch to using an event handle instead:

using Microsoft.FlightSimulator.SimConnect;
using System;
using System.Runtime.InteropServices;
using System.Threading;

internal class Program
{
    SimConnect? simConnect;
    private EventWaitHandle? simConnectEvent;

    void Run()
    {
        try
        {
            simConnectEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
            // Use event handle instead of window handle
            simConnect = new SimConnect("Managed Data Request", IntPtr.Zero, 0, simConnectEvent.SafeWaitHandle.DangerousGetHandle(), 0);

            // Subscribe to connection success event
            simConnect.OnRecvOpen += SimConnect_OnRecvOpen;

            // Keep the app running to process events
            while (true)
            {
                simConnectEvent.WaitOne();
                simConnect.ReceiveMessage();
            }
        }
        catch (COMException ex)
        {
            Console.WriteLine($"Connection error: {ex.Message}");
        }
        finally
        {
            simConnect?.Dispose();
            simConnectEvent?.Dispose();
        }
    }

    private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
    {
        Console.WriteLine($"Connected to MSFS! Simulator version: {data.szApplicationName}");
    }

    static void Main(string[] args)
    {
        new Program().Run();
    }
}

内容的提问来源于stack exchange,提问作者Thypa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:16:00