Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get window handle of SDL 2 application

Tags:

c

winapi

sdl

sdl-2

I would like to get the handle of a SDL2 window, to use it with WinApi.

I retrieve that handle with the following code :

/* All the SDL initalisation... */
SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED,
                        SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (window == NULL || renderer == NULL) {
    MessageBox(NULL, L"SDL initialisation error", NULL, MB_OK);
    exit(-1);
}

SDL_SysWMinfo wmInfo;
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;

But at this point, hwnd adress is 0xcccccccc (unused).

Did I do something wrong?

like image 394
Antoine C. Avatar asked Mar 20 '23 09:03

Antoine C.


1 Answers

SDL Wiki page in remarks section says that info.version must be initialised before usage. Code example suggests using SDL_VERSION(&info.version); before querying WM info.

SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;
like image 89
keltar Avatar answered Mar 31 '23 23:03

keltar