Access violation in XInputUap.dll when closing the window

Kitajima Yuki 21 Reputation points
2021-08-17T05:29:26.217+00:00

I am developing software to manage gamepads connected to a PC.
It almost works, but pressing the close button causes an access violation error in XInputUap.dll.

This program raises an exception when you close one of the two windows you created.
As far as I tried it at hand, just creating and closing one window didn't raise an exception.
I can also select libraries to link exclusively in xinput9_1_0.lib, xinput.lib, and windowsapp.lib, but only reproduce when you link windowsapp.lib only

・Use C++/WinRT, linking windowsapp.lib
・OS:Windows 10 Pro 2004 64bit, 19041.1052 Build
・VisualStudio 2019, version 16.10.1
・Windows SDK version:10.0.19041.0
・MSVC Toolset version:14.27.29110, 14.28.29910, 14.29.30037 (Occured in any version)

The following is the reproduction code.

#include <windows.h>
#include <XInput.h>

//#pragma comment(lib, "xinput9_1_0.lib")     // use xinput9_1_0.dll & xinput1_4.dll, OK
//#pragma comment(lib, "xinput.lib")          // use xinput1_4.dll, OK
#pragma comment(lib, "windowsapp.lib")      // use xinpuntuap.dll, Access violation in XInputUap.dll

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CREATE:
    {
        XINPUT_CAPABILITIES capabilities = {};
        XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &capabilities);
        return 0;
    }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

WNDCLASSEX GetWindowClass(LRESULT (*WndProc)(HWND, UINT, WPARAM, LPARAM), LPCWSTR className)
{
    WNDCLASSEX windowClass;
    windowClass.cbSize = sizeof(WNDCLASSEX);
    windowClass.style = 0;
    windowClass.lpfnWndProc = WndProc;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = GetModuleHandle(nullptr);
    windowClass.hIcon = nullptr;
    windowClass.hCursor = nullptr;
    windowClass.hbrBackground = nullptr;
    windowClass.lpszMenuName = nullptr;
    windowClass.lpszClassName = className;
    windowClass.hIconSm = nullptr;
    return windowClass;
}

int main()
{
    {
        auto wndCls = GetWindowClass(WndProc, L"Window1");
        if (RegisterClassEx(&wndCls) == 0)
            return 0;
        HWND hwnd = CreateWindowEx(0, L"Window1", L"WindowName1",
            WS_VISIBLE | WS_SYSMENU, 0, 0, 100, 100,
            nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    }
    {
        auto wndCls = GetWindowClass(WndProc, L"Window2");
        if (RegisterClassEx(&wndCls) == 0)
            return 0;
        HWND hwnd = CreateWindowEx(0, L"Window2", L"WindowName2",
            WS_VISIBLE | WS_SYSMENU, 200, 0, 100, 100,
            nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    }

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
Windows development | Windows API - Win32
Developer technologies | C++
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2021-08-18T08:15:07.793+00:00

    I reproduce it.
    But According to the document, WindowsApp.lib is used for UWP.

    This topic lists the Win32 APIs that are part of the Universal Windows Platform (UWP) and that are implemented by all Windows 10 devices. For convenience, an umbrella library named WindowsApp.lib is provided in the Microsoft Windows Software Development Kit (SDK), which provides the exports for this set of Win32 APIs. Link your app with WindowsApp.lib (and no other libraries) to access these APIs.

    And The Requirements section of XInputGetCapabilities function doesn’t mention WindowsApp.lib also.


1 additional answer

Sort by: Most helpful
  1. 略游 1 Reputation point
    2022-01-16T11:03:03.15+00:00

    I had this problem too, I solved it by linking "xinput.lib" before "WindowsApp.lib", thanks for the answers above.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.