title: ANSI C & WinAPI: how to get a handle to the window from hook procedure?
body: <p>I'm writing a very simple program, which prints color of selected pixel. 
  Here is my code:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define UNICODE
  
  LRESULT CALLBACK mouse_hook_low_level(int nCode, WPARAM wParam, LPARAM lParam)
  {
      if(wParam == WM_MOUSEMOVE) {
          //Need to get a handle to the window!
          InvalidateRect(window, NULL, FALSE); 
          UpdateWindow(window);
      }
      return CallNextHookEx(NULL, nCode, wParam, lParam);
  }
  
  LRESULT CALLBACK window_process(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
  {
      HDC dc = GetDC(NULL);
      HDC hdc;
      HFONT font;
      PAINTSTRUCT ps;
      int r, g, b;
      POINT p;
      RECT background_rect, color_rect;
      wchar_t buffer[256];
  
      switch(message) {
          case WM_PAINT:
              GetCursorPos(&amp;p);
              r = GetRValue(GetPixel(dc, p.x, p.y));
              g = GetGValue(GetPixel(dc, p.x, p.y));
              b = GetBValue(GetPixel(dc, p.x, p.y));
              hdc = BeginPaint(window, &amp;ps);
              background_rect.left = 0;
              background_rect.right = 199;
              background_rect.top = 0;
              background_rect.bottom = 99;
              FillRect(hdc, &amp;background_rect, (HBRUSH)(COLOR_WINDOW+1));
              font = CreateFont(
                  14,
                  0,
                  0,
                  0,
                  FW_DONTCARE,
                  FALSE,
                  FALSE,
                  FALSE,
                  DEFAULT_CHARSET,
                  OUT_OUTLINE_PRECIS,
                  CLIP_DEFAULT_PRECIS,
                  CLEARTYPE_QUALITY,
                  VARIABLE_PITCH,
                  TEXT("Times New Roman")
              );
              SelectObject(hdc, font);
              swprintf_s(buffer, 256, L"Coordinates: (%d, %d)", p.x, p.y);
              TextOut(hdc, 70, 10, buffer, wcslen(buffer));
              swprintf_s(buffer, 256, L"RGB: (%d, %d, %d)", r, g, b);
              TextOut(hdc, 70, 40, buffer,  wcslen(buffer));
              color_rect.left = 10;
              color_rect.right = 60;
              color_rect.top = 10;
              color_rect.bottom = 60;
              FillRect(hdc, &amp;color_rect, (HBRUSH)CreateSolidBrush(RGB(r, g, b)));
              EndPaint(window, &amp;ps);
              break;
          case WM_CLOSE:
              DestroyWindow(window);
              break;
          case WM_DESTROY:
              PostQuitMessage(0);
              break;
          default:
              ReleaseDC(window, hdc);
              return DefWindowProc(window, message, wParam, lParam);
      }
      ReleaseDC(window, hdc);
      return 0;
  }
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  {
      LPCWSTR class_name = L"name";
      MSG message;
      WNDCLASSEX window_class;
      HWND window;
      HHOOK MouseHook;
  
      window_class.cbSize = sizeof(WNDCLASSEX);
      window_class.style = CS_HREDRAW | CS_VREDRAW;
      window_class.lpfnWndProc = window_process;
      window_class.cbClsExtra = 0;
      window_class.cbWndExtra = 0;
      window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
      window_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
      window_class.lpszMenuName = NULL;
      window_class.lpszClassName = class_name;
      window_class.hInstance = hInstance;
      window_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
      RegisterClassEx(&amp;window_class);
      window = CreateWindow(
          class_name,
          L"Pixel color",
          WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
          CW_USEDEFAULT,
          CW_USEDEFAULT,
          200,
          100,
          NULL,
          NULL,
          hInstance,
          NULL
      );
      ShowWindow(window, SW_SHOWNORMAL);
      MouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouse_hook_low_level, hInstance, 0);
      while(GetMessage(&amp;message, NULL, 0, 0)) {
          TranslateMessage(&amp;message);
          DispatchMessage(&amp;message);
      }
      UnhookWindowsHookEx(MouseHook);
      return message.wParam;
  }
  </code></pre>
  
  <p>The question is: how can I get a handle to the window for further window update in hook procedure at line 9? And what can you say generally about my code? I'm a student and have a little C-programming experience, could you point out my errors?</p>
  
  <p>Thanks in advance.</p>