Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post Posted: July 17th, 2017, 1:08 pm 
User avatar

Joined: May 22nd, 2016, 4:27 pm
Posts: 4
Hi everyone. I was trying to write a code that would create a window with "Hello World" on the title bar, but I ran in some unexpected problems. It would be pretty helpful if someone could clarify this.

The errors are as follows:

1° - Line 7 & Line 51, it shows an error in the declaration for the function that will process Windows messages(WinProc) and when I put the cursor on the word it says: "expected a ';' "

2° - Line 109, the "if" statement that checks the handle(hwnd) to see if the "CreateWindow" function was successful, it says: "expected a declaration"

3° - Line 118, the "UpdateWindow" function says that "this declaration has no storage class or type specifier", and also that the identifier (hwnd) is undefined.

There were more errors but they "disappeared" after some time.

By the way, I took this example from a book called "Programming 2D Games" by Charles Kelly. The program is on the pages 24-26. Thanks in advance.


Attachments:
File comment: The said program
HelloWorld.zip [5.63 KiB]
Downloaded 4 times
Top 
 Profile  
Reply with quote  
 Post Posted: July 17th, 2017, 2:01 pm 
 

Joined: April 19th, 2017, 12:49 pm
Posts: 33
Location: Germany
Hi,
i usually write console apps, but i was able to figure out what was necessary to make it compile and run for me at least.

Code:
LRESULT WINMAIN WinProc(HWND, UINT, WPARAM, LPARAM);


In this line you got a function called WinProc. A function usually consists of a return type, functionname, functionparameters(those within these parenthesis) and a function body.

So for your function you got the return type = LRESULT, name = WinProc, those parameters, but that WINMAIN is needless (correct me if iam wrong). I deleted it and a bunch of errors disappeared.

Another thing i had to do was to cast WinProc as (WNDPROC) here:

Code:
bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
{
   WNDCLASSEX wcx;
   HWND hwnd;

   //Fill in the window class structure with parameters that describe the main window

        wcx.cbSize = sizeof(wcx);
   wcx.style = CS_HREDRAW | CS_VREDRAW;
   wcx.lpfnWndProc = (WNDPROC) WinProc;    //<
   wcx.cbClsExtra = 0;
   wcx.cbWndExtra = 0;
   wcx.hInstance = hInstance;
   wcx.hIcon = NULL;
   wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
   wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   wcx.lpszMenuName = NULL;
   wcx.lpszClassName = CLASS_NAME;
   wcx.hIconSm = NULL;

...


Top 
 Profile  
Reply with quote  
 Post Posted: July 17th, 2017, 5:28 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2714
Location: Oklahoma, United States
Code:
//function prototypes
// This isn't necessary as it's already declared by the Win32 API
// int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);

// I don't know what WINMAIN is but the standard is CALLBACK.  CALLBACK is a macro for _stdcall so you could also just use that.
//LRESULT WINMAIN WinProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//==============================================
//Window event callback function
//==============================================

//LRESULT WINMAIN WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
   {
   case WM_DESTROY:
      //Tell windows to kill this program
      PostQuitMessage(0);
      return 0;
   }
   return DefWindowProc(hWnd, msg, wParam, lParam);
}

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: July 17th, 2017, 5:40 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2714
Location: Oklahoma, United States
@xu2201g
I believe I've read that it's never a good idea to cast the function pointer for WNDPROC. If it doesn't work for some reason it's because the signature of the function you are using doesn't match the signature of the function expected. In this case it's the missing _stdcall or CALLBACK calling convention. There's _cdecl and _stdcall among others.

Check this out for more info. I usually don't worry about it at all, except when dealing with the WinAPI.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: July 17th, 2017, 5:46 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2714
Location: Oklahoma, United States
Might not matter too much, but you are missing the UnregisterClass function call to release the Window resources before you return msg.wParam.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: July 18th, 2017, 12:16 am 
User avatar

Joined: May 22nd, 2016, 4:27 pm
Posts: 4
Thanks for the help @xu2201g and @albinopapa, I've made the modifications and the program ran just fine. And thanks for the heads up on the _stdcall, CALLBACK calling conventions, I was clueless about that.


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 6 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron