有一个在C代码(之一):如何从UWP中的DLL调用函数?
void Function(char *output, size_t size);
有代码在C#:
[DllImport(@"C:\path\Name.dll",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi, EntryPoint = "Function")]
static extern void Function([In, Out] StringBuilder output,
[In, MarshalAs(UnmanagedType.SysUInt)] UIntPtr size);
如果我调用该函数的应用程序失败,出现此错误:
Unable to load DLL 'C:\path\Name.dll': Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
什么是错的?我必须做什么?
您有两个问题1)您必须在调用DLL之前分配变量输出。 char [] output = new char [256](); 2)c#中的字符是两个字节,而在c中是一个字节。通常你应该使用byte []而不是char []。 – jdweng
@jdweng如果我调用另一个没有params应用程序的方法失败,出现此错误。 (对不起,我的英语不好) –
查看帖子:https://stackoverflow.com/questions/38146181/passing-a-char-array-from-c-sharp-to-c-dll。使用byte []而不是char []。不要使用String Builder。 – jdweng