Merge C# and C++ code into single EXE

2

I have a small WPF application written in C# using VS2008. For one particular task I would like to use some C++/CLI code (since it requires heavy native interop).

I know I can compile the C++/CLI code as a DLL and then reference this from the C# app, but that seems silly when the code in question is going to be fairly small. I want to build them into the same file. (And without extracting a DLL at runtime.)

Via some command-line-compiler magic (similar to that shown here) I have managed to successfully get this to compile and work when compiling C++ code into a C# DLL, for a different application.

When trying the same thing to compile C++ code into a C# EXE, however, while the compile and link works fine, trying to run the resulting executable results in the following error:

Error while trying to run project: Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)

The command lines are basically:

cl /nologo /clr /Zi /MDd /DWIN32 /D_LIB /FUmanaged.dll /c interface.cpp /FoDebug\interface.obj
csc /target:module /addmodule:Debug\interface.obj /r:managed.dll App.cs MainWindow.xaml.cs Debug\MainWindow.xaml.g.cs Properties\AssemblyInfo.cs /out:Debug\Program.netmodule
link /entry:TestApp.App.Main /subsystem:Windows /clrimagetype:ijw /debug /ltcg /manifest /out:Debug\TestApp.exe Debug\App.netmodule Debug\interface.obj

Any ideas where it's going wrong? (managed.dll is a separate C# DLL containing common definitions required by both parts of the code. I'm ok with keeping this separate and it's not really related to the problem at hand, but I've included it for completeness.)

The link command line for the working DLL version is basically the same except that it uses /dll instead of /entry: /subsystem: /clrimagetype:. I've tried removing each of those and only the last seems optional and removing it did not help.

c#
.net
dll
linker
c++-cli
asked on Stack Overflow Sep 26, 2013 by Miral

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0