question

KitajimaYuki-9901 avatar image
0 Votes"
KitajimaYuki-9901 asked lveyou answered

Access violation in XInputUap.dll when closing the window

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-apic++
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please tell me if the close button you mentioned is the button on the gamepad? Are you using Xbox Gamepad when you are testing?

0 Votes 0 ·

The close button I mentioned is the [x] button in the upper right corner of the window.
I tested it without any Xbox GamePad or other gamepads.

0 Votes 0 ·

You are developing a C++/WinRT UWP app, right?

0 Votes 0 ·
Show more comments

I cannot reproduce it (Windows 10, 1909)
I just had to change "WNDCLASSEX GetWindowClass(WNDPROC WndProc, LPCWSTR className)" to compile
and I don't have any Access violation when I close one of the 2 windows

0 Votes 0 ·

Thank you for your reply and reproduction test.
I'm not sure, but it may depend on some environment.

0 Votes 0 ·

I reproduced the issue on Win 10 20H2 for both 64 and 32 bit debug builds. The call stack at the time of the access violation showed that it occurred in Windows.Gaming.Input.dll. The call stack is below -

      Windows.Gaming.Input.dll!Microsoft::WRL::ActivationFactory<struct Microsoft::WRL::Implements<class Microsoft::WRL::FtmBase,struct Microsoft::WRL::CloakedIid<struct Windows::Gaming::Input::Custom::ICustomGameControllerFactory> >,struct Windows::Gaming::Input::IArcadeStickStatics,struct Windows::Gaming::Input::IArcadeStickStatics2,0>::Release(void)    Unknown
      XInputUap.dll!XInputCore::XInputManager::~XInputManager(void)    Unknown
      XInputUap.dll!_CRT_INIT ()    Unknown
      XInputUap.dll!__DllMainCRTStartup()    Unknown
      ntdll.dll!LdrpCallInitRoutine()    Unknown
      ntdll.dll!LdrShutdownProcess()    Unknown
      ntdll.dll!RtlExitUserProcess()    Unknown
      kernel32.dll!ExitProcessImplementation ()    Unknown
      ucrtbased.dll!exit_or_terminate_process(const unsigned int return_code) Line 144    C++
      ucrtbased.dll!common_exit(const int return_code, const _crt_exit_cleanup_mode cleanup_mode, const _crt_exit_return_mode return_mode) Line 282    C++
      ucrtbased.dll!exit(int return_code) Line 294    C++
     XTest.exe!__scrt_common_main_seh() Line 297    C++
      XTest.exe!__scrt_common_main() Line 331    C++
      XTest.exe!mainCRTStartup(void * __formal) Line 17    C++
      kernel32.dll!BaseThreadInitThunk ()    Unknown
      ntdll.dll!RtlUserThreadStart ()    Unknown
0 Votes 0 ·
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered KitajimaYuki-9901 commented

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.

· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I get #define CO_E_NOTINITIALIZED _HRESULT_TYPEDEF_(0x800401F0L) when running the code. And then get ERROR_DEVICE_NOT_CONNECTED 0x0000048f return value after CoInitializeEx.

0 Votes 0 ·

I also noticed the runtime COM messages. In my testing, initializing COM eliminated the runtime messages but the access violation when closing the process continued to happen. And CoInitialize returned S_OK..

0 Votes 0 ·

Yes. And the program even will not load Windows.Gaming.Input.dll if linking Xinput.lib or Xinput9_1_0.lib.

0 Votes 0 ·
Show more comments
lveyou avatar image
0 Votes"
lveyou answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.