如何从UWP中的DLL调用函数?

时间:2016-11-09 13:33:39

标签: c# uwp

C:

中有代码(一个)
     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 个答案:

答案 0 :(得分:0)

在项目中添加DLL并将其设置为在输出目录中复制的内容类型。然后以这种方式引用它(不要添加扩展名" .dll":

 [DllImport(@"Name",
                CallingConvention = CallingConvention.StdCall,
                CharSet = CharSet.Ansi, EntryPoint = "Function")]
    static extern void Function([In, Out] StringBuilder output,
                                [In, MarshalAs(UnmanagedType.SysUInt)] UIntPtr size);

这是由SqlLite完成的(例如:https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs):)

正如jdweng所指出的,你可能也想使用byte []而不是StringBuilder。

相关问题