How can I C# and C++ Code in a single Executable and will the Final Exe need Net Framework. I am using Visual Studio 2013.
-
1You can't easily do this in the same executable. But, it's straightforward to write a C++/CLI class library (.DLL assembly) that is used by your main C# executable.Jonathon Reinhart– Jonathon Reinhart2014-05-25 06:26:18 +00:00Commented May 25, 2014 at 6:26
-
Check this Stack Overflow post stackoverflow.com/questions/935664/…NirMH– NirMH2014-05-25 06:27:31 +00:00Commented May 25, 2014 at 6:27
2 Answers
From here
If your C++ code is not compiled with /clr:safe (i.e. it is compiled with /clr or /clr:pure), do the following:
- compile your C++ code into .obj files
- compile your C# code into a .netmodule, using /AddModule to reference the C++ .obj files
- link the C# .netmodule directly with the C++ object files using the C++ linker to create a mixed language assembly
If your C++ code is compiled with /clr:safe, build your C++ code as a .netmodule. You can use it just like you would use a .netmodule from any other language.
1 Comment
Another way, which would work with any managed assembly (written in C++/CLI, or any .Net language) is to add that assembly as a resource to your executable project. So if that assembly does not appear in the directory with your exe, it would be loaded from resources with the following code.
In your exe, you should register to the assembly resolve event:
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
{
Assembly asm = null;
if (e.Name == "otherAssembly.dll")
{
byte[] assemblyBytes = // code to retrieve assembly from resources.
asm = Assembly.Load(assemblyBytes);
}
return asm;
};