Commit 4a089ca1c811825f2f15e69e85060cbac924c6b9

Sam Lantinga 2016-11-20T21:18:55

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)

diff --git a/include/SDL_syswm.h b/include/SDL_syswm.h
index 71ba5f1..f8835f6 100644
--- a/include/SDL_syswm.h
+++ b/include/SDL_syswm.h
@@ -201,6 +201,7 @@ struct SDL_SysWMinfo
         {
             HWND window;                /**< The window handle */
             HDC hdc;                    /**< The window device context */
+            HINSTANCE hinstance;        /**< The instance handle */
         } win;
 #endif
 #if defined(SDL_VIDEO_DRIVER_WINRT)
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index e079e29..7776c46 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -127,6 +127,7 @@ SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
     data->window = window;
     data->hwnd = hwnd;
     data->hdc = GetDC(hwnd);
+    data->hinstance = (HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
     data->created = created;
     data->mouse_button_flags = 0;
     data->videodata = videodata;
@@ -706,6 +707,10 @@ WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
             info->info.win.hdc = data->hdc;
         }
 
+        if (versionnum >= SDL_VERSIONNUM(2, 0, 5)) {
+            info->info.win.hinstance = data->hinstance;
+        }
+
         return SDL_TRUE;
     } else {
         SDL_SetError("Application not compiled with SDL %d.%d\n",
diff --git a/src/video/windows/SDL_windowswindow.h b/src/video/windows/SDL_windowswindow.h
index 7d50ba6..b61bcb3 100644
--- a/src/video/windows/SDL_windowswindow.h
+++ b/src/video/windows/SDL_windowswindow.h
@@ -33,6 +33,7 @@ typedef struct
     HWND hwnd;
     HDC hdc;
     HDC mdc;
+    HINSTANCE hinstance;
     HBITMAP hbm;
     WNDPROC wndproc;
     SDL_bool created;