title: How do I create a second window within a callback procedure? Win32
body: <p>I'm trying to create a second window if a dialog box from my first window returns a certain way. Unfortunately, if I call CreateWindowEx() from within the callback process of the first window, it doesn't recognise the second window's HWND. I also tried defining the first window in WinMain and making it visible within the callback, but it failed to recognise the HWND once again. Am I trying to do this wrong, or am I just missing something?</p>
  
  <p>Edit:</p>
  
  <pre><code>#include "stdafx.h"
  #include "md5.h"
  #include "Coursework.h"
  #include "accounts.h"
  
  const char g_szClassName[] = "myMainWindow";
  const char g_szLoggedInClass[] = "loggedInWindow";
  #define IDC_CREATE_ACCOUNT 101
  #define IDC_LOG_IN 110
  MYSQL *conn;
  static std::string strUserName;
  
  BOOL CALLBACK CreateDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  {
  switch(Message)
  {
      case WM_INITDIALOG:
      return TRUE;
      case WM_COMMAND: 
          switch (LOWORD(wParam))
          {
              case IDOK:
                  {
                  CreateAccount(hwnd);
                  }
                  break;
              case IDCANCEL:
                  EndDialog(hwnd, IDCANCEL);
              break;
          }
      default:
          return FALSE;
  }
  }
  BOOL CALLBACK LogInDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  {
  switch(Message)
  {
      case WM_INITDIALOG:
      return TRUE;
      case WM_COMMAND:
          switch(LOWORD(wParam))
          {
          case IDCANCEL:
              EndDialog(hwnd, IDCANCEL);
          break;
          case IDOK:
              {
                  if(bLogIn(hwnd, strUserName))
                  {
                      EndDialog(hwnd, IDOK);
  
                  }
              }
          break;
          }
          break;
      default:
          return FALSE;
  }
  }
  LRESULT CALLBACK LoggedInProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
  switch(msg)
  {
      case WM_CLOSE:
          DestroyWindow(hwnd);
          break;
      default:
          return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
  }
  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
  switch(msg)
  {
      case WM_CLOSE:
          PostQuitMessage(0);
      break;
      case WM_CREATE:
          {
          if (*mysql_error(conn))                                                                         //Checks if the connection succeeded, and
          {                                                                                               //exits the program if false.
              MessageBox(hwnd, "No connection to the server could be established.\r\nPlease contact your administrator.", "Error", MB_ICONERROR);
              exit(-1);
          }
          HWND hCreateButton = CreateWindowEx(NULL, "Button", "Create account", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 500, 250, 120, 24, hwnd, (HMENU)IDC_CREATE_ACCOUNT, GetModuleHandle(NULL), NULL);
          HWND hLogInButton = CreateWindowEx(NULL, "Button", "Log in", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 500, 210, 120, 24, hwnd, (HMENU)IDC_LOG_IN, GetModuleHandle(NULL), NULL);
          }
          break;
      case WM_DESTROY:
          PostQuitMessage(0);
      break;
      case WM_COMMAND:
          switch (LOWORD(wParam))
          {
          case IDC_CREATE_ACCOUNT:
              {
              int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, CreateDlgProc);
              if (ret == -1)
              {
                  MessageBox(hwnd, "Dialog failed to appear", "Error", MB_OK | MB_ICONINFORMATION);
              }
              }
              break;
          case IDC_LOG_IN:
              {
              int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), hwnd, LogInDlgProc);
              if(ret == -1)
              {
                  MessageBox(hwnd, "Dialog failed to appear", "Error", MB_OK | MB_ICONINFORMATION);
              }
              else if(ret == IDOK)
              {
                  ShowWindow(hwnd, SW_HIDE);
              }
              }
          }
          break;
      default:
          return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
  }
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int     nCmdShow)
  {
  
  WNDCLASSEX wc;
  WNDCLASSEX wc2;
  HWND hwnd;
  HWND hLoggedIn;
  MSG Msg;
  conn = mysql_init(NULL);                                                                    //Initialises mySQL connection
  mysql_real_connect(conn, "localhost", "root", "SecurityRules", "coursework", 0, NULL, 0);
  //Registering the Window Class
  wc.cbSize        = sizeof(WNDCLASSEX);
  wc.style         = 0;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = g_szClassName;
  wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  
  //Registering the second Window Class
  wc2.cbSize       = sizeof(WNDCLASSEX);
  wc2.style         = 0;
  wc2.lpfnWndProc   = LoggedInProc;
  wc2.cbClsExtra    = 0;
  wc2.cbWndExtra    = 0;
  wc2.hInstance     = hInstance;
  wc2.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wc2.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc2.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wc2.lpszMenuName  = NULL;
  wc2.lpszClassName = g_szLoggedInClass;
  wc2.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  
  if(!RegisterClassEx(&amp;wc))
  {
      MessageBox(NULL, "Window Registration Failed!\r\nContact your administrator.", "Error!", MB_ICONEXCLAMATION | MB_OK);
      return 0;
  }
  if(!RegisterClassEx(&amp;wc2))
  {
      MessageBox(NULL, "Window Registration failed!\r\nContact your administrator.", "Error!", MB_ICONEXCLAMATION|MB_OK);
      return 0;
  }
  //Creating the main Window
  hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Coursework", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1080, 640, NULL, NULL, hInstance, NULL);
  hLoggedIn = CreateWindowEx(WS_EX_CLIENTEDGE, g_szLoggedInClass, "Coursework", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1080, 640, NULL, NULL, hInstance, NULL);
  if(hwnd == NULL)
  {
      MessageBox(NULL, "Window Creation Failed!\r\nContact your adminstrator.", "Error!", MB_ICONEXCLAMATION | MB_OK);
      return 0;
  }
  
  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);
  
  
  // The Message Loop
  while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)
  {
      TranslateMessage(&amp;Msg);
      DispatchMessage(&amp;Msg);
  }
  mysql_close(conn);
  return Msg.wParam;
  }
  }
  </code></pre>
  
  <p>Edit 2:
  Intellisense (I'm in Visual Studio) states that 'identifier hLoggedIn is undefined', as well as nCmdShow.</p>