从Python ctypes加载带有通用Windows平台(UWP)调用的win32 dll

从Python ctypes加载带有通用Windows平台(UWP)调用的win32 dll,python,c++,windows,Python,C++,Windows,我想从Python调用通用Windows平台(UWP)API。我试图通过使用ctypes调用包含调用UWP API的函数的win32 dll来实现这一点 要总结该流程,请执行以下操作: Python->cytpes->dll->UWP 我一直在调试它。以下是工作原理: 创建win32 dll文件并使用调用其中的C函数 PythoncTypes模块 生成包含UWP调用的win32 dll文件(使用指令) 从一个C++ Win32应用程序加载Win32 DLL文件,调用UWP API调用的函数。李

我想从Python调用通用Windows平台(UWP)API。我试图通过使用ctypes调用包含调用UWP API的函数的win32 dll来实现这一点

要总结该流程,请执行以下操作: Python->cytpes->dll->UWP

我一直在调试它。以下是工作原理:

  • 创建win32 dll文件并使用调用其中的C函数 PythoncTypes模块
  • 生成包含UWP调用的win32 dll文件(使用指令)
  • 从一个C++ Win32应用程序加载Win32 DLL文件,调用UWP API调用的函数。李>
以下是不起作用的:

  • 当win32 DLL加载Python ctypes时,在该DLL内进行UWP调用;从ctypes调用时,从win32应用程序加载和调用同一DLL时工作的同一UWP调用失败
Python 3.6.4代码如下所示:

dll = ctypes.CDLL(path_to_dll)
dll.IsKeyboardPresent.restype = ctypes.c_bool
is_keyboard_present = dll.IsKeyboardPresent()
崩溃后的回溯以以下内容结束:

is_keyboard_present = dll.IsKeyboardPresent()  
OSError: [WinError -529697949] Windows Error [0xe06d7363][2]
dll文件的源代码如下:

#include "stdafx.h"

using namespace Platform;
using namespace Windows::Devices::Input;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;


extern "C" {

__declspec(dllexport) bool IsKeyboardPresent()
{
    bool is_present = 0;

    KeyboardCapabilities^ kbdCapabilities = ref new KeyboardCapabilities();
    is_present = (bool)kbdCapabilities->KeyboardPresent;

    return is_present;
}

再次,从C++中工作,但不是从cType工作。注释掉这一行不会使其从ctypes崩溃:

KeyboardCapabilities^ kbdCapabilities = ref new KeyboardCapabilities();
<> >加载WD32应用程序中的C++片段,加载DLL并调用包含的函数,没有错误:

typedef int(__stdcall *ISKEYBOARDPRESENT)();

HINSTANCE hinstLib;
ISKEYBOARDPRESENT IsKeyboardPresentProc;
BOOL fFreeResult;

bool is_keyboard_present;

hinstLib = LoadLibrary(TEXT("Win32Project2.dll"));
if (NULL != hinstLib) {
    IsKeyboardPresentProc = (ISKEYBOARDPRESENT)GetProcAddress(hinstLib, "IsKeyboardPresent");
    if (NULL != IsKeyboardPresentProc)
    {
        is_keyboard_present = (IsKeyboardPresentProc)();
    }
    fFreeResult = FreeLibrary(hinstLib);

}  
这是在使用Visual Studio 2015和32位Python 3.6.4的Windows 10上实现的


感觉我真的很接近,但被困在这里。一个原因是,Windows错误代码太笼统,无法将我指向任何方向。另外,当我从Python调用DLL时,它不会达到我在VisualStudio中设置的调试点

您是否在调试器中运行python.exe本身(或在调试器启动后附加到它)?