We help IT Professionals succeed at work.

Windows Program

rahulkothari
rahulkothari asked
on
I need help with the following program...its a very basic program. When i compile it , i get the following error : Undefine symbol wc.
I changed all instances of wc to wndClass and the programs compiles fine , but the .exe doesnt execute. Its a simple program which says " Hello Windows 98"...please help.

#define STRICT
#include <windows.h>
#pragma hdrstop

char szAppName[] = "Prog01";

#ifdef _WIN32_
char szAppTitle[] = " Program 1 (32 Bit)" ;
#else
char szAppTitle[] = " Program 1 (16 Bit)" ;
#endif

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) ;

#pragma argsused
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev,
                    LPSTR lpCmd, int nShow)
{
   if ( !hPrev) {
                  #ifdef _WIN32_
                  WNDCLASSEX wndClass ;
                  #else
                  WNDCLASSEX wndClass ;
                  #endif  

     wndClass.lpszClassName = szAppName ;
     wndClass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndClass.lpfnWndProc   = WndProc ;
     wndClass.cbClsExtra    = 0 ;
     wndClass.cbWndExtra    = 0 ;
     wndClass.hInstance     = hInst ;
     wndClass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndClass.lpszMenuName  = NULL ;
     
                  #ifdef _WIN32_
                  wndClass.cbSize = sizeof wc ;
                  wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                  if ( ! RegisterClassEx(&wc) );
                  #else
                  if ( ! RegisterClassEx(&wc) );
                  #endif
                        return 0;
                   }    
{
   HWND hwnd = CreateWindow(
                          szAppName,                  // window class name
                          szAppTitle,
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInst,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, nShow) ;
     UpdateWindow (hwnd) ;
   }
     
   {
     MSG msg ;
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
   }
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
     {

     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;

     case WM_PAINT:
       {
          PAINTSTRUCT ps;
          RECT rect;
          HDC hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rect) ;
          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
          DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

          EndPaint (hwnd, &ps) ;
          return 0L ;

     }
     }
     return DefWindowProc (hwnd, msg, wParam, lParam) ;
}
#pragma warn -stu
Comment
Watch Question

You still have references to wc in this code.
Is that intentional?
BRONZE EXPERT

Commented:
this works lovley, dont boter with teh 16 bit stuff, whos 16bit nowdays

#include <windows.h>

LRESULT CALLBACK wndProc(HWND w,UINT msg,WPARAM wp,LPARAM lp)
{
     switch(msg)
     {
     case WM_CREATE:
          return 0;
     case WM_RBUTTONDOWN:
     case WM_CLOSE:
          PostQuitMessage(0);
          DestroyWindow(w);
          return 0;
     case WM_PAINT:
           {
             PAINTSTRUCT ps;
              RECT rect;
              HDC hdc = BeginPaint (w, &ps) ;
              GetClientRect (w, &rect) ;
              DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
              DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

              EndPaint (w, &ps) ;
              return 0L;
         }
   
     }
     return DefWindowProc(w,msg,wp,lp);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,int nCmdShow)
{
     WNDCLASS cls;
     HWND w;
     MSG msg;

     memset(&cls,0,sizeof(cls));
     cls.lpfnWndProc          = wndProc;
     cls.hInstance          = GetModuleHandle(0);
     cls.lpszClassName     = "class";
     cls.hbrBackground     = (HBRUSH)(COLOR_WINDOW + 1);
     RegisterClass(&cls);                              
     w = CreateWindowEx(0,"class","test",
                              WS_POPUP|WS_VISIBLE|WS_SYSMENU|WS_CAPTION,
                            5,5,200,200,0,0,(HINSTANCE)cls.hInstance,0);
                           
     while(GetMessage(&msg,0,0,0))
     {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
     }
}

Author

Commented:
Dear Triskelion ,
                 The reference to wc is creating problems,
i am sure something else needs to be in its place , i tried to replace wc with wndClass , but the program doesnt execute.
         Please suggest.My ultimate aim is to get this program running without completely changing the code of this program.
Rahul

Author

Commented:
Dear makerp ,
              Thanx for the answer , but the purpose of me asking these questions is that , i wanna know whats wrong with this particular code. I wanna get rid of the only error i get when i compile this code , and be able to run the .exe.
         I already have other codes that run well and do the needful. But i need to see whats wrong with this one and what i am doing wrong ?

Rahul
BRONZE EXPERT

Commented:
You are still referencing wc in the RegisterClass calls, as well as filling the cbSize structure member... change that to wndClass... and as someone said before, don't worry with 16-bit Windows...

  -- LuisR
BRONZE EXPERT

Commented:
Another thing which may be the real problem...

you have this code:


                 #ifdef _WIN32_
                 wndClass.cbSize = sizeof wc ;
                 wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                 if ( ! RegisterClassEx(&wc) );
                 #else
                 if ( ! RegisterClassEx(&wc) );
                 #endif
                       return 0;
                  }    

the semicolon after the if causes the return statement to be outside of the if, so it always executes, whether the class was successfully rgistered or not. Remove thos semicolons and your problem will be solved.

  -- LuisR

Author

Commented:
Dear luisr,
           I changed the program to the following, as u said. It compiles well but doesnt run , i dont see any window.



#define STRICT
#include <windows.h>
#pragma hdrstop

char szAppName[] = "Prog01";

#ifdef _WIN32_
char szAppTitle[] = " Program 1 (32 Bit)" ;
#else
char szAppTitle[] = " Program 1 (16 Bit)" ;
#endif

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) ;

#pragma argsused
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev,
                   LPSTR lpCmd, int nShow)
{
  if ( !hPrev) {
                 #ifdef _WIN32_
                 WNDCLASSEX wndClass ;
                 #else
                 WNDCLASSEX wndClass ;
                 #endif  

    wndClass.lpszClassName = szAppName ;
    wndClass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndClass.lpfnWndProc   = WndProc ;
    wndClass.cbClsExtra    = 0 ;
    wndClass.cbWndExtra    = 0 ;
    wndClass.hInstance     = hInst ;
    wndClass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndClass.lpszMenuName  = NULL ;
   
                 #ifdef _WIN32_
                 wndClass.cbSize = sizeof wndClass ;
                 wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                 if ( ! RegisterClassEx(&wndClass) )
                 #else
                 if ( ! RegisterClassEx(&wndClass) )
                 #endif
                      return 0;
                  }    
{
  HWND hwnd = CreateWindow(
                         szAppName,                  // window class name
                         szAppTitle,
                         WS_OVERLAPPEDWINDOW,        // window style
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y position
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y size
                         NULL,                       // parent window handle
                         NULL,                       // window menu handle
                         hInst,                  // program instance handle
                         NULL) ;                     // creation parameters
   
    ShowWindow (hwnd, nShow) ;
    UpdateWindow (hwnd) ;
  }
   
  {
    MSG msg ;
    while (GetMessage (&msg, NULL, 0, 0))
    {
         TranslateMessage (&msg) ;
         DispatchMessage (&msg) ;
    }
    return msg.wParam ;
  }
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
    {

    case WM_DESTROY:
         PostQuitMessage (0) ;
         return 0 ;

    case WM_PAINT:
      {
         PAINTSTRUCT ps;
         RECT rect;
         HDC hdc = BeginPaint (hwnd, &ps) ;
         GetClientRect (hwnd, &rect) ;
         DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
         DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

         EndPaint (hwnd, &ps) ;
         return 0L ;

    }
    }
    return DefWindowProc (hwnd, msg, wParam, lParam) ;
}
#pragma warn -stu
BRONZE EXPERT

Commented:
Have you tried stepping into the code in the debugger? you can see each function as it is called, and also check return values, to see where it's failing.

To me, it looks good.

  -- LuisR

Author

Commented:
Dear Luisr,
                     Will try the same tonite , this is my first program and not a good start at all.
will keep u posted
Thanx
Rahul
BJZ

Commented:
The lines :

                #ifdef _WIN32_
                wndClass.cbSize = sizeof wndClass ;
                wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                if ( ! RegisterClassEx(&wndClass) )
                #else
                if ( ! RegisterClassEx(&wndClass) )
                #endif
                     return 0;

are equivalent to :

                #ifdef _WIN32_
                wndClass.cbSize = sizeof wndClass ;
                wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                #endif
                if ( ! RegisterClassEx(&wndClass) )
                     return 0;

easier to understand no ?

Also with a #define _WIN32_ the prog runs well here.

Commented:
I compiled and ran the latest code you posted and although I get the following warnings:

(16) : warning C4068: unknown pragma
(100) : warning C4068: unknown pragma

It runs just fine as long as I define _WIN32_.  If you do not define _WIN32_ then you are going to run into problems because you are always using a WNDCLASSEX and calling RegisterClassEx in both cases.  I think you actually want to use WNDCLASS and RegisterClass when _WIN32_ is not defined.  The problem that occurs is that when _WIN32_ is not defined the .cbSize parameter does not get set and thus the call to RegisterClassEx fails.

BTW instead of returning 0 when register class fails it might be better to return GetLastError() to help you determine the cause of the problem.

Tarang

Author

Commented:
dear tarang,
            i made the changes according to ur suggestions and the program ran fine , THANXXX
But there is one small problem :
The title window shows : Program 1 (16 Bit) where as i want it to show : Program 1 (32 Bit), since i am using 32 bit. How do i get that ???
Since i get that error , i am guessing maybe my program always runs in 16 bit. I am pasting my complete program .
The PRAGMA is a directive for Borland C++ compiler.If u compile using that compiler , u wouldnt get any warnings

#define STRICT
#include <windows.h>
#pragma hdrstop

char szAppName[] = "Prog01";

#ifdef _WIN32_
char szAppTitle[] = " Program 1 (32 Bit)" ;
#else
char szAppTitle[] = " Program 1 (16 Bit)" ;
#endif

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) ;

#pragma argsused
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev,
                   LPSTR lpCmd, int nShow)
{
  if ( !hPrev) {
                 #ifdef _WIN32_
                 WNDCLASSEX wndClass ;
                 #else
                 WNDCLASS wndClass ;
                 #endif  

    wndClass.lpszClassName = szAppName ;
    wndClass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndClass.lpfnWndProc   = WndProc ;
    wndClass.cbClsExtra    = 0 ;
    wndClass.cbWndExtra    = 0 ;
    wndClass.hInstance     = hInst ;
    wndClass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndClass.lpszMenuName  = NULL ;
   
                 #ifdef _WIN32_
                 wndClass.cbSize = sizeof wndClass ;
                 wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                 if ( ! RegisterClassEx(&wndClass) )
                 #else
                 if ( ! RegisterClass(&wndClass) )
                 #endif
                       return 0;
                  }    
{
  HWND hwnd = CreateWindow(
                         szAppName,                  // window class name
                         szAppTitle,
                         WS_OVERLAPPEDWINDOW,        // window style
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y position
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y size
                         NULL,                       // parent window handle
                         NULL,                       // window menu handle
                         hInst,                  // program instance handle
                         NULL) ;                     // creation parameters
   
    ShowWindow (hwnd, nShow) ;
    UpdateWindow (hwnd) ;
  }
   
  {
    MSG msg ;
    while (GetMessage (&msg, NULL, 0, 0))
    {
         TranslateMessage (&msg) ;
         DispatchMessage (&msg) ;
    }
    return msg.wParam ;
  }
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
    {

    case WM_DESTROY:
         PostQuitMessage (0) ;
         return 0 ;

    case WM_PAINT:
      {
         PAINTSTRUCT ps;
         RECT rect;
         HDC hdc = BeginPaint (hwnd, &ps) ;
         GetClientRect (hwnd, &rect) ;
         DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
         DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

         EndPaint (hwnd, &ps) ;
         return 0L ;

    }
    }
    return DefWindowProc (hwnd, msg, wParam, lParam) ;
}
#pragma warn -stu

Author

Commented:
dear tarang,
            i made the changes according to ur suggestions and the program ran fine , THANXXX
But there is one small problem :
The title window shows : Program 1 (16 Bit) where as i want it to show : Program 1 (32 Bit), since i am using 32 bit. How do i get that ???
Since i get that error , i am guessing maybe my program always runs in 16 bit. I am pasting my complete program .
The PRAGMA is a directive for Borland C++ compiler.If u compile using that compiler , u wouldnt get any warnings

#define STRICT
#include <windows.h>
#pragma hdrstop

char szAppName[] = "Prog01";

#ifdef _WIN32_
char szAppTitle[] = " Program 1 (32 Bit)" ;
#else
char szAppTitle[] = " Program 1 (16 Bit)" ;
#endif

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) ;

#pragma argsused
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev,
                   LPSTR lpCmd, int nShow)
{
  if ( !hPrev) {
                 #ifdef _WIN32_
                 WNDCLASSEX wndClass ;
                 #else
                 WNDCLASS wndClass ;
                 #endif  

    wndClass.lpszClassName = szAppName ;
    wndClass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndClass.lpfnWndProc   = WndProc ;
    wndClass.cbClsExtra    = 0 ;
    wndClass.cbWndExtra    = 0 ;
    wndClass.hInstance     = hInst ;
    wndClass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndClass.lpszMenuName  = NULL ;
   
                 #ifdef _WIN32_
                 wndClass.cbSize = sizeof wndClass ;
                 wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION ) ;
                 if ( ! RegisterClassEx(&wndClass) )
                 #else
                 if ( ! RegisterClass(&wndClass) )
                 #endif
                       return 0;
                  }    
{
  HWND hwnd = CreateWindow(
                         szAppName,                  // window class name
                         szAppTitle,
                         WS_OVERLAPPEDWINDOW,        // window style
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y position
                         CW_USEDEFAULT,CW_USEDEFAULT,              // initial y size
                         NULL,                       // parent window handle
                         NULL,                       // window menu handle
                         hInst,                  // program instance handle
                         NULL) ;                     // creation parameters
   
    ShowWindow (hwnd, nShow) ;
    UpdateWindow (hwnd) ;
  }
   
  {
    MSG msg ;
    while (GetMessage (&msg, NULL, 0, 0))
    {
         TranslateMessage (&msg) ;
         DispatchMessage (&msg) ;
    }
    return msg.wParam ;
  }
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
    {

    case WM_DESTROY:
         PostQuitMessage (0) ;
         return 0 ;

    case WM_PAINT:
      {
         PAINTSTRUCT ps;
         RECT rect;
         HDC hdc = BeginPaint (hwnd, &ps) ;
         GetClientRect (hwnd, &rect) ;
         DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
         DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

         EndPaint (hwnd, &ps) ;
         return 0L ;

    }
    }
    return DefWindowProc (hwnd, msg, wParam, lParam) ;
}
#pragma warn -stu
BRONZE EXPERT

Commented:
Just #define _WIN32_ at the very top of the program.

  -- LuisR
Commented:
The problem is where you expect _WIN32_ to be defined.  It seems that you are expecting it to be defined for you automatically and I don't beleive that it is.  Thus you have four alternatives: #define _WIN32_ yourself, find another #define that performs the same task, or use GetVersion() to determine the Windows version at runtime.  The last option is to either forget about Win16 altogether as nobody these days develops software for a Win16 environment, or forget about using WNDCLASSEX as it isn't really buying you much.  The small icon will be automatically be generated by Windows based on the regular icon; this of course is only an option if your large and small icons differ from each other only in size.

Author

Commented:
Thanx for the help

Explore More ContentExplore courses, solutions, and other research materials related to this topic.