我在Visual Studio中编写了一个C ++类库,该类库仅定义了调用某些Python的函数:
#pragma once
#include <Python.h>
extern "C"
__declspec(dllexport)
void python()
{
Py_Initialize();
PyRun_SimpleString("2 + 2");
}
我在与C#Blank Universal应用程序相同的解决方案中创建了另一个项目。我尝试引用我提到的上一个项目生成的DLL:
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();
}
...
}
}
该应用程序处于发布配置。
每当我尝试在本地计算机上运行该应用程序时,总是会出现错误:
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应用程序的安全性不允许这样做?
还是因为Python.h?
编辑:
我用一个DLL项目和一个将其包装的运行时组件构建了该项目,现在出现此错误:
类型'System的例外
'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))
我将对象名称为“ Everyone”的用户添加到了DLL中(我不确定如何向其他人授予权限),但是仍然出现错误。
首先,UWP不能仅通过DLLImport消耗旧版C ++ dll。
如果要将传统的c ++函数公开给C#,第一个建议是使用WinRT组件包装该C ++逻辑。然后,您可以通过以下步骤在UWP应用程序中引用此组件:将其添加到项目中,在“解决方案资源管理器”窗口中打开文件的属性,并将它们标记为要包含在应用程序包中的内容。这篇文章会有所帮助。此步骤提供了更详细的步骤。
如果要PInvoke dll,则可以按照以下步骤操作(您可以参考此MSDN post):
将Win32 dll添加到您的UWP项目中,确保将其类型设置为“ content”
然后在适当的cs文件中,使用DllImport PInvoke dll。
还有一件事:您需要确保您的Python dll在WinRT中没有使用禁止的API。您可以使用dll的/ ZW编译选项进行检查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句