mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.11k stars 511 forks source link

'System.BadImageFormatException' occurred in CppSharp.Generator.dll #1552

Open Silmaen opened 3 years ago

Silmaen commented 3 years ago
Brief Description

I'm new to CSharp and CppSharp. I work under VisualStudio 2019 (version 16.8.3). I'm trying to evaluatuate the capability of the librarie to generate C# binding to our C++ libraries. I download a copy of the master branch of cppSharp. I was able to build the librarie and run the UTests. No errors appeared.

I then try to make a bare minimum example:

class Foo{
public:
    int a;
    float b;
};
int FooAdd(Foo* foo);

And try to generate the C# bindings... I follow the generateBindings.md tutorial but when I try to run the binary I got:

Exception thrown: 'System.BadImageFormatException' in CppSharp.Generator.dll
An unhandled exception of type 'System.BadImageFormatException' occurred in CppSharp.Generator.dll
Could not load file or assembly 'CppSharp.Parser.CLI, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was ma
de to load a program with an incorrect format..

after some search I figure out that this issue is due to x64 vs. x86 format, but according to configurations files, everthingting should be x64... and if I change my project target to x86, I got the same error but for another lib? If I set to 'AnyCPU' the first error is still present (even with the 'prefer 32bit' option checked.

Is there any tutorial on howto setup projects for the use of CppSharp? What is missing in my configuration?

here is the configuration I use:

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <PackageVersion Include="System.CodeDom" Version="4.7.0" />
    <PackageVersion Include="Microsoft.Win32.Registry" Version="4.7.0" />
    <PackageVersion Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="2.3.2262-g94fae01e" />
    <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
    <PackageVersion Include="NUnit" Version="3.11.0" />
    <PackageVersion Include="NUnit3TestAdapter" Version="3.17.0" />
  </ItemGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootDir>$(MSBuildThisFileDirectory)\..\</RootDir>
    <Platforms>x64</Platforms>
    <BuildDir>$(RootDir)\Compilation\</BuildDir>
    <ObjDir>$(BuildDir)</ObjDir>
    <BaseOutputPath>$(RootDir)exe\</BaseOutputPath>
    <OutputPath>$(BaseOutputPath)$(Configuration)_$(PlatformTarget)\</OutputPath>
    <LangVersion>8.0</LangVersion>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <WarningLevel>4</WarningLevel>
    <GeneratorFileExtension>exe</GeneratorFileExtension>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\MySources\MySources.vcxproj" ReferenceOutputAssembly="false" />
    <Reference Include="CppSharp">
      <HintPath>..\..\CppSharp-master\bin\Release_x64\CppSharp.dll</HintPath>
    </Reference>
    <Reference Include="CppSharp.AST">
      <HintPath>..\..\CppSharp-master\bin\Release_x64\CppSharp.AST.dll</HintPath>
    </Reference>
    <Reference Include="CppSharp.Generator">
      <HintPath>..\..\CppSharp-master\bin\Release_x64\CppSharp.Generator.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>
ddobrev commented 3 years ago

Please ensure you don't have "Prefer 32-bit" checked in the project settings of your executable.