C++ Problem (Winapi)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

C++ Problem (Winapi)

 
0
  #1
Jul 31st, 2009
Hello all.
I have some problems with compilation while working with WinApi.
Here is the code:
  1. // include the basic windows header file
  2. #include <windows.h>
  3. #include <windowsx.h>
  4.  
  5. // the WindowProc function prototype
  6. LRESULT CALLBACK WindowProc(HWND hWnd,
  7. UINT message,
  8. WPARAM wParam,
  9. LPARAM lParam);
  10.  
  11. // the entry point for any Windows program
  12. int WINAPI WinMain(HINSTANCE hInstance,
  13. HINSTANCE hPrevInstance,
  14. LPSTR lpCmdLine,
  15. int nCmdShow)
  16. {
  17. // the handle for the window, filled by a function
  18. HWND hWnd;
  19. // this struct holds information for the window class
  20. WNDCLASSEX wc;
  21.  
  22. // clear out the window class for use
  23. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  24.  
  25. // fill in the struct with the needed information
  26. wc.cbSize = sizeof(WNDCLASSEX);
  27. wc.style = CS_HREDRAW | CS_VREDRAW;
  28. wc.lpfnWndProc = WindowProc;
  29. wc.hInstance = hInstance;
  30. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  31. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  32. wc.lpszClassName = "WindowClass1";
  33.  
  34. // register the window class
  35. RegisterClassEx(&wc);
  36.  
  37. // create the window and use the result as the handle
  38. hWnd = CreateWindowEx(0,
  39. "WindowClass1", // name of the window class
  40. "Our First Windowed Program", // title of the window
  41. WS_OVERLAPPEDWINDOW, // window style
  42. 300, // x-position of the window
  43. 300, // y-position of the window
  44. 500, // width of the window
  45. 400, // height of the window
  46. NULL, // we have no parent window, NULL
  47. NULL, // we aren't using menus, NULL
  48. hInstance, // application handle
  49. NULL); // used with multiple windows, NULL
  50.  
  51. // display the window on the screen
  52. ShowWindow(hWnd, nCmdShow);
  53.  
  54. // enter the main loop:
  55.  
  56. // this struct holds Windows event messages
  57. MSG msg;
  58.  
  59. // wait for the next message in the queue, store the result in 'msg'
  60. while(GetMessage(&msg, NULL, 0, 0))
  61. {
  62. // translate keystroke messages into the right format
  63. TranslateMessage(&msg);
  64.  
  65. // send the message to the WindowProc function
  66. DispatchMessage(&msg);
  67. }
  68.  
  69. // return this part of the WM_QUIT message to Windows
  70. return msg.wParam;
  71. }
  72.  
  73. // this is the main message handler for the program
  74. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  75. {
  76. // sort through and find what code to run for the message given
  77. switch(message)
  78. {
  79. // this message is read when the window is closed
  80. case WM_DESTROY:
  81. {
  82. // close the application entirely
  83. PostQuitMessage(0);
  84. return 0;
  85. } break;
  86. }
  87.  
  88. // Handle any messages the switch statement didn't
  89. return DefWindowProc (hWnd, message, wParam, lParam);
  90. }
If you look at it it seams that there are no problems, but there are some errors rising while compiling it, the two most importants are :
Error 1 error C2065: 'msg' : undeclared identifier
Error 2 error C2275: 'MSG' : illegal use of this type as an expression
Thise errors are given on the next line:
  1. MSG msg;
But i think the code is correct . Im using Visual Studio Professional edition.
Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: ashishchoure is on a distinguished road 
Solved Threads: 3
ashishchoure's Avatar
ashishchoure ashishchoure is offline Offline
Light Poster

Re: C++ Problem (Winapi)

 
0
  #2
Jul 31st, 2009
Include winuser.h
structure MSG is defined in that header
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Problem (Winapi)

 
0
  #3
Jul 31st, 2009
>>Include winuser.h

That should not be necessary since he's already including windows.h

>>Im using Visual Studio Professional edition.
Which version -- there are several of them.

>>but there are some errors rising while compiling it, the two most importants are :
Are there other errors listed before the two you posted? Correct those first, recompile then see if those two errors disappear.
Last edited by Ancient Dragon; Jul 31st, 2009 at 9:23 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: C++ Problem (Winapi)

 
0
  #4
Jul 31st, 2009
The other errors rise after the two given(and are refernced to the msg variable, so if i correct the two given,i'l get rid of the rest).
The include didnt helped.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Problem (Winapi)

 
0
  #5
Jul 31st, 2009
I compiled your program with vc++ 2008 Express and had no errors or warnings. I also have Microsoft Windows SDK installed, but since you have the Pro edition that shouldn't be your problem. Are you certain you created a "win32 project" instead of a console project? Attached is the project I created.
Attached Files
File Type: zip wintest1.zip (4.9 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: C++ Problem (Winapi)

 
0
  #6
Jul 31st, 2009
Thanks for the project.
It does work and does compile, in the last project i had WIndows Sdk (latest) and DirectX Sdk only. But will try removing the DirectX Sdk.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: C++ Problem (Winapi)

 
0
  #7
Jul 31st, 2009
//Dont know why the edit button is not showed.
Edit:
Tried to remove the reference to directx sdk, reseting the windows sdk directory (resetting the lib directory also), but none of that helped .
Btw, i use Visual Studio 2008 Professional Edition.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: C++ Problem (Winapi)

 
0
  #8
Jul 31st, 2009
Once again i dont see the edit button...
I've created an Empty Project, and winapi hello world
  1. #include <windows.h>
  2. int WINAPI WinMain(HINSTANCE hInstance,
  3. HINSTANCE hPrevInstance,
  4. LPSTR lpCmdLine,
  5. int nCmdShow)
  6. {
  7. MessageBox(NULL,"a","b",0);
  8. return 0;
  9. }
Worked just fine.
I also searched for the definition of MSG and found #Define MSG UserMsg. Triyed to put it in my code and it said that UserMsg is not recognized... Maybe there is any problem with my sdk (its v6.0A) ?
Going to check other sdk i have here (Windows Server 2003 R2). Maybe thise would help...
Edit:
Tested, same problem.
Last edited by jen140; Jul 31st, 2009 at 10:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Problem (Winapi)

 
0
  #9
Aug 1st, 2009
The IDE will take you directly to where MSG is declared -- just put the mouse on the 'MSG' then right click. From the popup menu select the second item "Go To Definition". When I do that the IDE takes me to WinUser.h and tagMsg structure.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: C++ Problem (Winapi)

 
0
  #10
Aug 1st, 2009
Tryied to define :
tagMSG varxx;
FAR varxx;
And same error rised, even if i included the #WinUser.h
The interesting thing is that i tried to define tagMSG structure, and compiler said that the structure is already defined. (error C2011: 'tagMSG' : 'struct' type redefinition)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC