Fixed bug 3486 - Can't get HINSTANCE of my window
authorSam Lantinga <slouken@libsdl.org>
Sun, 20 Nov 2016 21:18:55 -0800
changeset 10634fbd8834ba7b8
parent 10633 2fa3dd0d12f6
child 10635 49026b20848e
Fixed bug 3486 - Can't get HINSTANCE of my window

realitix

SDL2 allows to create widow and to get information through SDL_SysWMinfo.
But it misses something, with Vulkan, you need the HWND and HINSTANCE of the window for Win32 system.
Sadly, SDL2 provides only HWND but not HINSTANCE.

In some context, it can be difficult to get the HINSTANCE, indeed, I'm using pySDL2 (Python) and I can only access properties that SDL2 gives me.
I have to use a dirty trick like that to get the HINSTANCE: (https://raw.githubusercontent.com/bglgwyng/pyVulkan/master/examples/win32misc.py)
include/SDL_syswm.h
src/video/windows/SDL_windowswindow.c
src/video/windows/SDL_windowswindow.h
     1.1 --- a/include/SDL_syswm.h	Sat Nov 19 23:27:51 2016 +0100
     1.2 +++ b/include/SDL_syswm.h	Sun Nov 20 21:18:55 2016 -0800
     1.3 @@ -201,6 +201,7 @@
     1.4          {
     1.5              HWND window;                /**< The window handle */
     1.6              HDC hdc;                    /**< The window device context */
     1.7 +            HINSTANCE hinstance;        /**< The instance handle */
     1.8          } win;
     1.9  #endif
    1.10  #if defined(SDL_VIDEO_DRIVER_WINRT)
     2.1 --- a/src/video/windows/SDL_windowswindow.c	Sat Nov 19 23:27:51 2016 +0100
     2.2 +++ b/src/video/windows/SDL_windowswindow.c	Sun Nov 20 21:18:55 2016 -0800
     2.3 @@ -127,6 +127,7 @@
     2.4      data->window = window;
     2.5      data->hwnd = hwnd;
     2.6      data->hdc = GetDC(hwnd);
     2.7 +    data->hinstance = (HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
     2.8      data->created = created;
     2.9      data->mouse_button_flags = 0;
    2.10      data->videodata = videodata;
    2.11 @@ -706,6 +707,10 @@
    2.12              info->info.win.hdc = data->hdc;
    2.13          }
    2.14  
    2.15 +        if (versionnum >= SDL_VERSIONNUM(2, 0, 5)) {
    2.16 +            info->info.win.hinstance = data->hinstance;
    2.17 +        }
    2.18 +
    2.19          return SDL_TRUE;
    2.20      } else {
    2.21          SDL_SetError("Application not compiled with SDL %d.%d\n",
     3.1 --- a/src/video/windows/SDL_windowswindow.h	Sat Nov 19 23:27:51 2016 +0100
     3.2 +++ b/src/video/windows/SDL_windowswindow.h	Sun Nov 20 21:18:55 2016 -0800
     3.3 @@ -33,6 +33,7 @@
     3.4      HWND hwnd;
     3.5      HDC hdc;
     3.6      HDC mdc;
     3.7 +    HINSTANCE hinstance;
     3.8      HBITMAP hbm;
     3.9      WNDPROC wndproc;
    3.10      SDL_bool created;