4

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?

Antoine C.
  • 3,730
  • 5
  • 32
  • 56

1 Answers1

10

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;
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
keltar
  • 17,711
  • 2
  • 37
  • 42