Search code examples
c#c++dllimportlinqpad

LINQPad: Attempting to use assembly that uses [DllImport] to access C++ dll


I added MyAssembly.dll as an additional reference to my query, and that works fine: I am able to use various classes from it with no trouble.

One of the methods for one of the classes in MyAssembly.dll references another dll (Wrapper.dll). Wrapper.dll uses [DllImport] to access functionality in a C++ dll (C.dll):

[DllImport("C.dll", CallingConvention = CallingConvention.StdCall, EntryPoint ="CPlusPlusFunction")] internal static extern UInt32 _CPlusPlusFunction([MarshalAs(UnmanagedType.LPWStr)] string file_path);

While this method works perfectly fine when executed from MyApp.exe that references MyAssembly.dll (which references Wrapper.dll, which references C.dll), in LINQPad I can't get it to work.

If I only add a reference for MyAssembly.dll and attempt to run the method, I get:

DllNotFoundException: Unable to load DLL 'C.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

If I add an Additional Reference for just Wrapper.dll, I get the same error.

If I add an Additional Reference for both Wrapper.dll AND C.dll, I get:

Metadata file 'C.dll' could not be opened -- 'An attempt was made to load a program with an incorrect format.'

If I add an Additional Reference for just C.dll I get the same error.

Any idea on how I can get this to work in LINQPad like it does in MyApp.exe?


Solution

  • You do it the exact same way, you'll have to copy it in the same directory as the EXE so Windows can find it. The LINQPad EXE in this case. C:\Program Files (x86)\LINQPad4 by default. Or any directory that's on the PATH, c:\windows\syswow64 is the infamous choice. Best not to do that.