您可以在 UWP 的 C# 代码中使用 C++ DLL 吗? [英] Can you use C++ DLLs in C# code in a UWP?

查看:29
本文介绍了您可以在 UWP 的 C# 代码中使用 C++ DLL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 中编写了一个 C++ 类库,它只定义了一个调用 Python 的函数:

I wrote a C++ Class Library in Visual Studio that just defines a function that invokes some Python:

#pragma once

#include <Python.h>

extern "C"
__declspec(dllexport)
void python()
{
    Py_Initialize();
    PyRun_SimpleString("2 + 2");
}

我在 C# Blank 通用应用程序的同一解决方案中制作了另一个项目.我尝试引用从我提到的上一个项目生成的 DLL:

I made another project in the same solution that was a C# Blank Universal app. I tried to reference the DLL generated from the previous project I mentioned:

using System;
...

namespace StartupApp
{
    ...
    sealed partial class App : Application
    {
        private const string CPPPythonInterfaceDLL = @"pathtodll";

        [DllImport(CPPPythonInterfaceDLL, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
        private static extern void python();

        public static void Python()
        {
            python();
        }
        ...
        public App()
        {
            ...

            Python();
        }

        ...
    }
}

应用处于发布配置中.

每当我尝试在本地计算机上运行该应用程序时,它总是会出现错误:

Whenever I try to run the app on my Local Machine, it always gives an error:

The program '[2272] StartupApp.exe' has exited with code -1073741790 (0xc0000022).
Activation of the Windows Store app 'ab6a8ef2-1fa8-4cc7-b7b3-fb7420af7dc3_7dk3a6v9mg4g6!App' failed with error 'The app didn't start'.

所以我的问题是:我可以从 C# UWP 项目中引用 C++ 类库吗?还是 UWP 应用的安全性不允许这样做?

So my question is this: can I reference a C++ class library from a C# UWP project? Or does the security on UWP apps not allow this?

还是因为Python.h?

Or is it because of Python.h?

我用一个 DLL 项目和一个包装它的运行时组件构建了这个项目,现在出现了这个错误:

I built the project with a DLL project and a Runtime Component that wrapped it, and now I have this error:

系统"类型的异常

'System.DllNotFoundException' occurred in StartupApp.exe but was not handled in user code

Additional information: Unable to load DLL 'pathtodll': Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

我在 DLL 中添加了一个对象名为Everyone"的用户(我不知道如何给每个人权限),但错误仍然出现.

I added a user to the DLL with the object name "Everyone" (I am not sure how else to give everyone permissions) but the error still comes up.

推荐答案

首先,UWP 不能仅通过 DLLImport 使用旧的 C++ dll.

Firstly, UWP can't consume a legacy C++ dll just by DLLImport.

如果您想向 C# 公开旧的 C++ 函数,第一个建议是使用 WinRT 组件包装该 C++ 逻辑.然后您可以通过以下步骤在 UWP 应用程序中引用此组件:将其添加到项目中,在解决方案资源管理器窗口中打开文件的属性,并将它们标记为要包含在应用程序包中的内容.这篇文章会有所帮助.这个提供了更详细的步骤.

If you want to expose legacy c++ functions to C#, the first suggestion is to wrap that C++ logic using a WinRT component. Then you can reference this component in UWP application by following steps: adding it to the project, open the files' properties in the Solution Explorer window, and mark them as content to be included in the app package. This post would be helpful. This one provides more detailed steps.

如果你想PInvoke dll,你可以按照以下步骤操作(你可以参考这个MSDN 帖子):

If you want to PInvoke the dll, you can follow these steps (You can refer to this MSDN post):

  1. 将 win32 dll 添加到您的 UWP 项目中,确保将其类型设置为内容"

  1. Add win32 dll into your UWP project making sure to set its type as 'content'

然后在适当的 cs 文件中,使用 DllImport 来 PInvoke dll.

Then in the proper cs file, using DllImport to PInvoke the dll.

还有一件事:您需要确保您的 Python dll 没有使用 WinRT 中禁止的 API.您可以使用 dll 的/ZW 编译选项来检查这一点.

There is one more thing: You need to make sure your Python dll is not using prohibited APIs in WinRT. You can check this by using /ZW compile option for the dll.

这篇关于您可以在 UWP 的 C# 代码中使用 C++ DLL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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