I want to call the Universal Windows Platform (UWP) API from Python. I tried to do that by using ctypes to call a win32 dll containing functions which call the UWP API.
To summarize the flow of that: Python->cytpes->dll->UWP
I am stuck on debugging that. Here is what does work:
Here is what does not work:
The Python 3.6.4 code looks like this:
dll = ctypes.CDLL(path_to_dll)
dll.IsKeyboardPresent.restype = ctypes.c_bool
is_keyboard_present = dll.IsKeyboardPresent()
The traceback after it crashes ends with this:
is_keyboard_present = dll.IsKeyboardPresent()
OSError: [WinError -529697949] Windows Error [0xe06d7363][2]
The source code to the dll file is this:
#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;
}
Again, works from C++ but not from ctypes. Commenting out this one line makes it not crash from ctypes:
KeyboardCapabilities^ kbdCapabilities = ref new KeyboardCapabilities();
The C++ snippet in the win32 App which loads the dll and invokes the contained function without error:
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);
}
This is on Windows 10 with Visual Studio 2015 and 32-bit Python 3.6.4
Feels like I am really close but got stuck here. For one reason, the Windows Error code is too general to point me in any direction. Also, when I call the DLL from Python it does not hit the debugging point which I set in Visual Studio.
User contributions licensed under CC BY-SA 3.0