I have a managed C# dll that uses an unmanaged C++ dll using DLLImport. All is working great.
However, I want to embed that unmanaged DLL inside my managed DLL as explain by Microsoft there:
So I added the unmanaged dll file to my managed dll project, set the property to 'Embedded Resource' and modify the DLLImport to something like:
[DllImport("Unmanaged Driver.dll, Wrapper Engine, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null",
CallingConvention = CallingConvention.Winapi)]
where
'Wrapper Engine' is the assembly name of my managed DLL
'Unmanaged Driver.dll' is the unmanaged DLL
When I run, I get:
Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))
I saw from MSDN and from http://blogs.msdn.com/suzcook/ that's supposed to be possible…
Best Answer
You can embed the unmanaged DLL as a resource if you extract it yourself to a temporary directory during initialization, and load it explicitly with LoadLibrary before using P/Invoke. I have used this technique and it works well. You may prefer to just link it to the assembly as a separate file as Michael noted, but having it all in one file has its advantages. Here's the approach I used: