Loading win32 dll with Universal Windows Platform (UWP) calls from Python ctypes

5

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:

  • Creating a win32 dll file and calling C functions inside of it using the Python ctypes module
  • Building a win32 dll file containing UWP calls (using instructions here).
  • Loading the win32 dll file from a C++ win32 application and calling functions which make UWP API calls.

Here is what does not work:

  • UWP calls within the win32 DLL when that dll is loaded with Python ctypes; The same UWP call which worked when the same DLL was loaded and invoked from a win32 application fails when invoked from ctypes.

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.

python
c++
windows
asked on Stack Overflow Apr 10, 2018 by Allen Ingling

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0