Loading a native unmanaged C++ DLL into a managed C# app causes the DLL to output garbage
Wait a sec: Look at the reference below:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int MultiplyByTen(int numberToMultiply);
I didn't notice that your delegate doesn't have the same attribute.
For reference, I don't see anything unusual in how you are doing the LoadLibrary:
Dynamically Loading a Native Library
I have done this myself using the exact reference without a problem. I would suggest removing ALL code temporarily that executes inside the DLL and just do a simple pass-through value. Right now Init() is always returning 0. Try something else since you have zeroed out memory before, getting a zero back may just be a side-effect of the mem-zero op. Return 1974 or something.
Ensure you have allow unsafe code enabled and use the memory viewer to look at the stack (you are getting a pointer back so you have a starting point). If you do this beside the IL, you might spot where your memory is getting trashed.
HTH
How to embed unmanaged dll into console app
Let's call the assembly of your project MyAssembly
.
Create a new folder at the root of your project in Visual Studio. Let's call it MyDlls
.
Put your assembly you want to include in this folder and set their Build Action to Embedded Resource.
Then, in your code, add the following elements:
class Program
{
// ... Your code
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve; // Called when the assembly hasn't been successfully resolved
// ... Your code
}
private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = args.Name.Split(',')[0]; // Gets the assembly name to resolve.
using (Stream stream = assembly.GetManifestResourceStream("MyAssembly.MyDlls." + assemblyName + ".dll")) // Gets the assembly in the embedded resources
{
if (stream == null)
return null;
byte[] rawAssembly = new byte[stream.Length];
stream.Read(rawAssembly, 0, (int)stream.Length);
return Assembly.Load(rawAssembly);
}
}
}
Using DllImport to load unmanaged dll into managed application
First I want to thank everyone for their help. Unfortunately I never did solve this issue (see edits in my main question). The answer turned out to be starting a totally new Visual Studio solution and creating two new projects: C# app and C++ dll. I did away with the need for wrappers as I am now just marshaling two main functions.
Thanks again.
Include managed C# DLL into unmanaged C++ DLL - all in one single file
Here is the final working version.
"c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /target:module /out:Libs.netmodule /recurse:..\Libs\*.cs
"c:\Program Files\Microsoft Visual Studio 12.0\VC\bin\cl.exe" /clr /LN /Fo /Y- /Z7 /FU Libs.netmodule ..\Links\Links.cpp /link /LIBPATH:"c:\Program Files\Microsoft Visual Studio 12.0\VC\lib" /LIBPATH:"c:\Program Files\Microsoft SDKs\Windows\v7.1A\Lib"
"c:\Program Files\Microsoft Visual Studio 12.0\VC\bin\link.exe" /DLL /LTCG /CLRIMAGETYPE:IJW /OUT:Library.dll Libs.netmodule Links.obj /LIBPATH:"c:\Program Files\Microsoft Visual Studio 12.0\VC\lib" /LIBPATH:"c:\Program Files\Microsoft SDKs\Windows\v7.1A\Lib"
Useful links :
- http://social.msdn.microsoft.com/Forums/vstudio/en-US/83218913-56df-4530-9d6c-45076bdc0acb/how-to-merge-several-managed-and-unmanaged-dlls-into-a-single-dll?forum=vcgeneral
- http://social.msdn.microsoft.com/Forums/vstudio/en-US/b0082757-b179-4cce-9c6e-847f39cfefa4/cannot-find-referenced-assemblyproject-from-clr-project?forum=msbuild
- How to link C# and C++ assemblies into a single executable?
- http://msdn.microsoft.com/en-us/library/vstudio/6ds95cz0.aspx
- http://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
- http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx
Now i am able to use exported functions from .NET in unmanaged applications. Moreover, all information from both projects - Links (C++) and Libs (C#) - was grouped in one file - Library.dll. This way i can merge into one DLL as many projects as i want and it does not matter whether they are managed or native.
Related Topics
How to Get the Calling Method Name and Type Using Reflection
How to Get the Exif Data from a File Using C#
Get Last 10 Lines of Very Large Text File > 10Gb
If Statements Matching Multiple Values
How to Catch Exception in Task
Read SQL Table into C# Datatable
ASP.NET Calling Webmethod with Jquery Ajax "401 (Unauthorized)"
How to Get a Property Value Based on the Name
How to Automatically Filter Out Soft Deleted Entities with Entity Framework
Is It Bad to Not Unregister Event Handlers
Formatting Doubles for Output in C#
How to Force Garbage Collector to Run
ASP.NET MVC 5 Group of Radio Buttons
How to Run a Winform from Console Application