Trying to marshal/invoke a DLL in C# but can't link it to the project

-1

I'm working with a C# SDK wrapped around a C++ library with C headers. In order to access some of the behaviors in the lower level library, I've created my own wrapper library and then added the same invoke statements from the C# SDK so that I can adjust certain accesses to public instead of internal.

This is an example invocation:

[DllImport("libX.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern DeviceStatus X_GetVersion(StringBuilder firmwareVersion, int count);

Autocompletion is able to recognize the invoke calls, but when I try to compile I get an error stating:

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

When I go to add the original C++ lib as a reference, I get an error saying that it:

[...] could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

I've checked that all levels have read/write permissions, and I've also moved the dll into my project's bin/debug and bin/release directories.

From what I've seen online, I shouldn't have to add the reference while using DllImport, but not very familiar with these commands or C# in general to be honest. In the documentation here, haven't been able to find instructions on linking the library. Next steps to resolve?

c#
c++
pinvoke
marshalling
dllimport
asked on Stack Overflow Sep 4, 2018 by Mercutio • edited Sep 4, 2018 by Uwe Keim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0