如何从 UWP 中的 DLL 调用函数? [英] How to call a function from DLL in UWP?

查看:18
本文介绍了如何从 UWP 中的 DLL 调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中有代码(其中之一):

There is code (one of) in 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);

如果我调用这个函数应用程序失败并出现这个错误:

And if I call this function app fails with this error:

Unable to load DLL 'C:\path\Name.dll': Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

怎么了?我该怎么办?

推荐答案

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

Add the DLL in your project and set it as content type copied in the output directory. Then reference it this way (do not add the extension ".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 ) :)

This is what is done by SqlLite (example : https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs ) :)

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

As pointed out by jdweng, you may want to use byte[] instead of StringBuilder too.

这篇关于如何从 UWP 中的 DLL 调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆