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

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

问题描述

我在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#空白通用的应用程序的另一个项目。我试图从引用以前的项目中,我提到生成的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 ++类库从C#UWP项目?还是在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的对象名每个人(我不知道怎么回事,给每个人的权限),但错误还是出现了。

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不能消耗传统的C ++ DLL只是dllimport的。

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。您可以通过使用/ ZW编译为DLL选项来检查一下。

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 ++的DLL在C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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