Window programming question

Started by
9 comments, last by MetalicFog 23 years, 1 month ago
I am writing a test window program that does not use winmain. Instead I use regular main and get handle to the window instance using function call "GetModuleHandle(NULL)". It seems to be working since I get a window. However when I insert GetLastError at various place I get an error messages "System could not find the file specified" during RegisterClass call. The reason I'm trying to write window program like this is because I can use regular iostream to write to the console. I'm concerned if what I'm doing is working by accident. Can anyone comment on this? Thanks. Below is a code snippet;
  
int Create(HINSTANCE hInstance)
{
   WNDCLASS   wndclass; 
   extern LONG WINAPI EventHandler(HWND, UINT, WPARAM, LPARAM);

   cerr << "--- Create called" << endl;

   /* Register the frame class */ 
   wndclass.style         = 0; 
   wndclass.lpfnWndProc   = (WNDPROC)(WndProc); 
   wndclass.cbClsExtra    = 0; 
   wndclass.cbWndExtra    = 0; 
   wndclass.hInstance     = hInstance; 
   wndclass.hIcon         = LoadIcon (hInstance, szAppName); 
   wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW); 
   wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
   wndclass.lpszMenuName  = szAppName; 
   wndclass.lpszClassName = szAppName; 
 
   if (!RegisterClass (&wndclass) ) 
       return FALSE; 

   ErrorProcessor("RegisterClass", GetLastError());

   m_hWnd = CreateWindow (
            szAppName, 
            "Pong", 
            WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS 
                                | WS_CLIPCHILDREN, 
            CW_USEDEFAULT, 
            CW_USEDEFAULT, 
            m_width, 
            m_height, 
            NULL, 
            NULL, 
            hInstance, 
            NULL); 
 
   ErrorProcessor("CreateWindow", GetLastError());
   return (m_hWnd ? TRUE : FALSE);
}

int Run()
{
   MSG msg;

   cerr << "--- Run called" << endl;

   if (m_hWnd)
   {
      ShowWindow (m_hWnd, SW_SHOW); 
      ErrorProcessor("ShowWindow", GetLastError());
      UpdateWindow (m_hWnd); 
      ErrorProcessor("UpdateWindow", GetLastError());
   }

   int done = FALSE;
   while (!done) 
   { 
      if (PeekMessage(&msg, m_hWnd, 0, 0, PM_REMOVE) == TRUE) 
      { 
         ErrorProcessor("PeekMessage == TRUE", GetLastError());
         if (msg.message == WM_QUIT)
         {
            done = TRUE;
         }
         else
         {
            TranslateMessage(&msg); 
            ErrorProcessor("TranslateMessage", GetLastError());
            DispatchMessage(&msg); 
            ErrorProcessor("DispatchMessage", GetLastError());
         }
      }
      else
      {
         ErrorProcessor("PeekMessage == FALSE", GetLastError());
      }
   } 
 
   ErrorProcessor("End of Run", GetLastError());
   return msg.wParam;
}

int main()
{
   ErrorProcessor("main", GetLastError());
   Create(GetModuleHandle(NULL));
   ErrorProcessor("main after Create", GetLastError());

   Run();

   ErrorProcessor("main", GetLastError());
   return 0;
}
  
Edited by - MetalicFog on March 18, 2001 2:13:31 AM
Advertisement
Try this and see what the error is:


int main(){
ErrorProcessor("main", GetLastError());
HINSTANCE hMyInstance;
hMyInstance = GetModuleHandle(NULL);
ErrorProcessor("main after GetModuleHandle()", GetLastError());

if (hMyInstance == NULL)
ErrorProcessor("hMyInstance == NULL", GetLastError());

Create(hMyInstance);
ErrorProcessor("main after Create", GetLastError());
Run();
ErrorProcessor("main", GetLastError());
return 0;
}


I suspect that Window''s is "thinking" that this isn''t a windows program as such, and so no hinstance should be given. Of course I could be wrong.
Regards,Nekosion
Or you could get window''s to create a console for you:
Look up this and associated functions in msdn, this would let you
create a windows application.

"CreateConsoleScreenBuffer()"


Regards,
Nekosion

Resist Windows XP''s Invasive Production Activation Technology!
Regards,Nekosion
Another option I just thought of:

Write a class that create''s a window (the console) with just a text box in it, (or two if you want to be able to accept input).

Then you can just use the class''s functions to write/read/do other stuff to the "console".

This is pretty easy, and would take maybe a hour or two to code, which is probably less than the time that it will take you to figure out what the hell windows is doing with your code.
Regards,Nekosion
Nokosian, thanks for your reply. I tried creating HINSTANCE separately and check for NULL. There are no errors. The exact error message after RegisterClass in Create function is "The system cannot find the file specified" and error number is 2.
I heard some time ago that all main entry point to windows program is in fact "main". The windows compiler crt fudges it so that there are transition from main to winmain. I also heard that for Boland compiler the code for crt is available and it shows how this fudging is done (unfortunately I don''t have Boland compiler). I''m curious to see if I can just create a console project in Visual C++ and use it to write GUI program. I think it will be nice to do it this way because then Windows program will look a lot like programs in other platforms.
MS also provides the source code to their CRT startup,

VC98\CRT\SRC\CRTEXE.C
uhmm....where''s the source for the Borland CRT startup? I only seem to have source for the VCL, OWL and MFC.
well, that''s what I heard. Anyway, I did find crt source in VC++ directory. Crt does in fact calls GetModuleHandle(NULL) when it calls winmain after C runtime initialization. I also looked at the source for GLUT, and there the main entry is defined as "main" and it calls GetModuleHandle in one of its init function call. So I think maybe the error messages I was getting don''t mean much. Then, my question becomes, why did MS define its main entry point as winmain? It seems needless to say the least.
To clarify.

The entry point for all Windows applications is WinMain. WinMain is mapped to main.

CRT source code is available where it was stated to be available by mhkrause.
VK

This topic is closed to new replies.

Advertisement