Skip to main content
GameDev.net gamedev.net
🔒 Locked

Game Programming Genesis Part I : Beginning Windows Programming

Started by Myopic Rhino Feb 17, 2001 at 5:33 AM 23 replies 10.9k views
Sir Cloud
Sir Cloud
I get exactly the same error.

There is probably something wrong with the settings, but when I try to figure it out and change some of them, I get even more errors

Please tell me how to fix this.
Oluseyi
Oluseyi
Did you ever read your documentation? You know, MSDN (especially where it says Visual C++ Documentation )? Or the QuickStart card that comes with MSVC (I assume you''re using MSVC)?

No? Well, go do so. It''ll answer a lot of your questions.

(btw, the answer to your problem is to explicitly create a "Windows Application" project and add your source files to it. Letting the compiler assign a "default workspace" _always_ results in a Win32 Console App.)
Sir Cloud
Sir Cloud
Hey, thanks, it worked!
I''m only starting to learn programming, so I don''t know all those things. I searched for that error in Help, and it found something which I did not understand anyway
sofsenint
sofsenint
I''m really new to C++ and am used to the ease of Visual Basic. So, of course, I have no idea at all what is wrong with my program, and I get 8 errors! I would really appreciated if you would look over my code and the errors and see if you know what''s up.

Code:

#include
#include
//These two are almost always included in Windows programming#include
#include
#define WIN32_LEAN_AND_MEAN


typedef struct _WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX;

HWND CreateWindowEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu, or child-window identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);

LRESULT CALLBACK MsgHandler(
HWND hwnd, // window handle
UINT msg, // the message identifier
WPARAM wparam, // message parameters
LPARAM lparam // more message parameters
);

BOOL PeekMessage(
LPMSG lpMsg, // pointer to structure for message
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax, // last message
UINT wRemoveMsg // removal flags
);
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


BOOL TranslateMessage(CONST MSG *lpmsg);
LONG DispatchMessage(CONST MSG *lpmsg);

//All Windows program start with the WinMain function.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)

{



HWND hwnd;
if (!(hwnd = CreateWindowEx(NULL, // extended style, not needed
"Sample Class", // class identifier
"Sample Window", // window title
WS_POPUP | WS_VISIBLE, // parameters
0, 0, 320, 240, // initial position, size
NULL, // handle to parent (the desktop)
NULL, // handle to menu (none)
hinstance, // application instance handle
NULL))) // who needs it?
return(0);

}

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
return(DefWindowProc(hwnd, msg, wparam, lparam));

}

Errors:
c:\ned''s turkey farm\application\applicationsource.cpp(21) : error C2371: ''WNDCLASSEX'' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\winuser.h(1177) : see declaration of ''WNDCLASSEX''
c:\ned''s turkey farm\application\applicationsource.cpp(36) : error C2373: ''CreateWindowExA'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2993) : see declaration of ''CreateWindowExA''
c:\ned''s turkey farm\application\applicationsource.cpp(51) : error C2373: ''PeekMessageA'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2388) : see declaration of ''PeekMessageA''
c:\ned''s turkey farm\application\applicationsource.cpp(57) : error C2373: ''PostMessageA'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2683) : see declaration of ''PostMessageA''
c:\ned''s turkey farm\application\applicationsource.cpp(64) : error C2373: ''SendMessageA'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2511) : see declaration of ''SendMessageA''
c:\ned''s turkey farm\application\applicationsource.cpp(67) : error C2373: ''TranslateMessage'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2359) : see declaration of ''TranslateMessage''
c:\ned''s turkey farm\application\applicationsource.cpp(68) : error C2373: ''DispatchMessageA'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winuser.h(2365) : see declaration of ''DispatchMessageA''
c:\ned''s turkey farm\application\applicationsource.cpp(87) : error C2440: ''='' : cannot convert from ''struct HWND__ *(__stdcall *)(unsigned long,const char *,const char *,unsigned long,int,int,int,int,struct HWND__ *,struct HMENU__ *,struct HINSTANCE
__ *,void *)'' to ''struct HWND__ *''
There is no context in which this conversion is possible
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
chillah
chillah
typedef struct _WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX;

HWND CreateWindowEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu, or child-window identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);

LRESULT CALLBACK MsgHandler(
HWND hwnd, // window handle
UINT msg, // the message identifier
WPARAM wparam, // message parameters
LPARAM lparam // more message parameters
);

BOOL PeekMessage(
LPMSG lpMsg, // pointer to structure for message
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax, // last message
UINT wRemoveMsg // removal flags
);
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


BOOL TranslateMessage(CONST MSG *lpmsg);
LONG DispatchMessage(CONST MSG *lpmsg);






Do you include these in your source file? If so, there is no need. Take them out and see if it works.
"I'm never alone, I'm alone all the time" - Glycerine-Bush"It starts with one thing" In The End-Linkin Park
Oluseyi
Oluseyi
First, defining WIN32_LEAN_AND_MEAN after including windows.h and windowsx.h is useless. Move it before.

Second, you don''t quite have a message pump. You need to continuously process messages - usually with a while loop:

while( bRunning )
{
if( PeekMessage( &msg, hwnd, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}


Other than that I really can''t point to any critical errors. Perhaps your development environment isn''t properly set up?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
CaptainChainsaw
CaptainChainsaw
In reply to the anon poster:

LRESULT CALLBACK MsgHandler(HWND hwnd, UNIT msg, WPARAM w, LPARAM l);

shouldn''t this be:

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM w, LPARAM l);

???

OK I''ve got a 3 errors with my code, they are:

error C2664: ''RegisterClassA'' : cannot convert parameter 1 from ''struct _WNDCLASSEX *'' to ''const struct tagWNDCLASSA *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

error C2373: ''CreateWindowExA'' : redefinition; different type modifiers: see declaration of ''CreateWindowExA''

error C2065: ''msg'' : undeclared identifier
Error executing cl.exe.


Can anyone see where I''m going wrong? I''m completely lost in win32 !! :''(

thanks in advance for any help/advice etc,

CaptainChainsaw


Here is my complete program:

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>


typedef struct _WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX;



LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
return (DefWindowProc(hwnd, msg, wparam, lparam));
}

//BOOL PeekMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg);
//BOOL TranslateMessage(CONST MSG *lpMsg);
//BOOL DispatchMessage(CONST MSG *lpMsg);


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

WNDCLASSEX sampleClass;


sampleClass.cbSize = sizeof(WNDCLASSEX);
sampleClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
sampleClass.lpfnWndProc = MsgHandler;
sampleClass.cbClsExtra = 0;
sampleClass.cbWndExtra = 0;
sampleClass.hInstance = hinstance;
sampleClass.hIcon = LoadIcon(NULL,IDI_WINLOGO);
sampleClass.hCursor = LoadCursor(NULL, IDC_ARROW);
sampleClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
sampleClass.lpszMenuName = NULL;
sampleClass.lpszClassName = "Sample Class";
sampleClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

RegisterClass(&sampleClass);


HWND CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle
, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu,
HINSTANCE hInstance, LPVOID lpParam);

HWND hwnd;

if(!(hwnd = CreateWindowEx(NULL, "Sample Class", "Sample Window", WS_POPUP | WS_VISIBLE,
0, 0, 400, 400, 0, 0, hinstance, NULL)))

if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

return 0;
}
}
Public Sub House()On Error Resume drink If Pint.empty = True Then Pint.refill Else Pint.drink End ifstomach.add PintMsgBox " I''ve had .... " & stomach.count & " Pints"MsgBox "VERY DRUNK"End Sub
gandalf835
gandalf835
I Have no idea about windows programming but would like to learn. Can someone please help me I tried putting the lessons together for Beginning Windows Programming but am getting a lot of errors.

My code so far is as follows:

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <conio.h>

typedef struct _WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX;

LRESULT CALLBACK MsgHandler(
HWND hwnd, // window handle
UINT msg, // the message identifier
WPARAM wparam, // message parameters
LPARAM lparam // more message parameters
);

BOOL PeekMessage(
LPMSG lpMsg, // pointer to structure for message
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax, // last message
UINT wRemoveMsg // removal flags
);
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

HWND CreateWindowEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu, or child-window identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);


BOOL TranslateMessage(CONST MSG *lpmsg);
LONG DispatchMessage(CONST MSG *lpmsg);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX sampleClass;

sampleClass.cbSize = sizeof(WNDCLASSEX);
sampleClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
sampleClass.lpfnWndProc = MsgHandler;
sampleClass.cbClsExtra = 0;
sampleClass.cbWndExtra = 0;
sampleClass.hInstance = hinstance;
sampleClass.hIcon = LoadIcon(NULL,IDI_WINLOGO);
sampleClass.hCursor = LoadCursor(NULL, IDC_ARROW);
sampleClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
sampleClass.lpszMenuName = NULL;
sampleClass.lpszClassName = "Sample Class";
sampleClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

RegisterClass(&sampleClass);

HWND hwnd;
if (!(hwnd = CreateWindowEx(NULL, // extended style, not needed
"Sample Class", // class identifier
"Sample Window", // window title
WS_POPUP | WS_VISIBLE, // parameters
0, 0, 320, 240, // initial position, size
NULL, // handle to parent (the desktop)
NULL, // handle to menu (none)
hinstance, // application instance handle
NULL))) // who needs it?
{
return(0);
}

return (0);
}//End WinMain()


---------ERRORS:::::


c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(21): error C2371: ''WNDCLASSEX'' : redefinition; different basic types
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(64): error C2373: ''CreateWindowExA'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(68): error C2373: ''DispatchMessageA'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(36): error C2373: ''PeekMessageA'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(42): error C2373: ''PostMessageA'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(49): error C2373: ''SendMessageA'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(67): error C2373: ''TranslateMessage'' : redefinition; different type modifiers
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(99): error C2440: ''='' : cannot convert from ''HWND (__stdcall *)(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)'' to ''HWND''
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(99): error C2440: ''='' : cannot convert from ''HWND (__stdcall *)(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)'' to ''HWND''
c:\C_Sharp_Solutions\RpgGame\Shinku.cpp(88): error C2664: ''RegisterClassA'' : cannot convert parameter 1 from ''WNDCLASSEX *__w64 '' to ''const WNDCLASSA *''
yohann305
yohann305
Why isn''t there any source file to download???
It''s our first window, right?
Neo_Phyte
Neo_Phyte
quote:
Original post by CaptainChainsaw
In reply to the anon poster:


OK I've got a 3 errors with my code, they are:

error C2664: 'RegisterClassA' : cannot convert parameter 1 from 'struct _WNDCLASSEX *' to 'const struct tagWNDCLASSA *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

error C2373: 'CreateWindowExA' : redefinition; different type modifiers: see declaration of 'CreateWindowExA'

error C2065: 'msg' : undeclared identifier
Error executing cl.exe.



/////////////////////////////////////////////////////////////////

Hello CaptainChainsaw


I know i am a bit late, and you have probably already solve these problems,but i had the same one and to help those who would make the same mistake i wish to post the solution here.

1)error C2664: 'RegisterClassA' : cannot convert parameter 1 from 'struct _WNDCLASSEX *' to 'const struct tagWNDCLASSA *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

and

2)error C2373: 'CreateWindowExA' : redefinition; different type modifiers: see declaration of 'CreateWindowExA'

These are due to redifinition of the _WNDCLASSEX structure.He gave us the protype of the structure,but it is already defined in winuser.cpp or the like.Basicly,you don't need to recreate structure.


3)error C2065: 'msg' : undeclared identifier

This is a MSG type that you need to declare before you past it's adresse to the PeekMessage().

You have to declare:

MSG msg;
then pass it's it's adresse to a pointer that will fill it with a message if there is one in the queue.

I have one question though.Why is my WINAPI WinMain not created with WINAPI but instead is created with APIENTRY.Does it matter and why?

I have changed this back to WINAPI, but i am curious.



[edited by - Neo_Phyte on January 18, 2004 1:54:50 PM]

[edited by - Neo_Phyte on January 18, 2004 1:55:25 PM]

[edited by - Neo_Phyte on January 18, 2004 1:56:21 PM]
The true Don juan is the one that seduce the same woman night after night.
Neo_Phyte
Neo_Phyte
Well as i have stated it''s for people that,like me and him, are having the same kind of errors while doing this, and since it was never answered, i wanted to put it up here.

Simple no!

And thank you for the answer!

The true Don juan is the one that seduce the same woman night after night.
The true Don juan is the one that seduce the same woman night after night.
Oberon_Command
Oberon_Command
By far the most common error that I''ve seen above is putting the definition of WNDCLASSEX in their source file. This should not be done. Don''t define structures such as these in your source file, because they are already defined in windows.h and windowsx.h. Also, winmain doesn''t need a prototype, and nor does the message handler.

PaganX
PaganX
Trying to find some info on adding the mouse wheel.
waZim
waZim
Quote:
Original post by ZealousElixir
"A bit late"? He posted freaking a year and a half ago... Why on earth did you necro this (again) just to make that comment?



I think i am even more late ,but i just want to discuss something in the article thats how i find this Post....

in that article i see..

Quote:

UINT wMsgFilterMin, wMsgFilterMax: The indices of the first and last messages in the queue to check. Most of the time, you'll only be interested in the first message on the queue, so you would set both of these parameters to 0.


Which to my understanding and MSDN, Not correct. these wMsgFilterMin and wMsgFilterMax filters are used for deciding which TYPES of messages to handle.And to be more precise, Range of Types of Messages to Handle...

Refer to : http://msdn.microsoft.com/en-us/library/ms644943(VS.85).aspx

For example should it Handle mouse messages or not, and such... but according to this article it says to specify the "indices of the first and last messages in the queue to check" , which does not makes sense because you only have access to the first element in a queue anyways...

Example:
Lets say WM_MOUSEFIRST = 5; and WM_MOUSELAST = 10; and we call

PeekMessage(&message, window, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE)


and now lets say our Message Queue is full with Mouse Messages ONLY.. so according to this article. our Message Queue will only processes from 5th to 10th Messages in the queue ignorning the first 4 messages which are still Mouse Messages.
While in real thats not the case. the PeekMessage() will handle all those messages in the Queue. since they all must be in the range of WM_MOUSEFIRST and WM_MOUSELAST....

..
waZim
Reggi
Reggi
I started reading this article a few days ago and i realized it was from nearly a decade ago Dx. Is it still worth reading and learning from this article :o? And i was also wondering whether there are C++ windows programming tutorials similar to this one since this article was written about C. Or is it that C and C++ windows programming are similar enough that this article is still good if i want to write in C++?
Wan
Wan
Quote:
Original post by Reggi
I started reading this article a few days ago and i realized it was from nearly a decade ago Dx. Is it still worth reading and learning from this article :o?

The WinApi hasn't changed in all these years when it comes to simply creating a window and handling its messages, so it still applies. I haven't read the article, so I can't say if it's any good or not, or whether better tutorials exist (but there probably are).
Quote:
And i was also wondering whether there are C++ windows programming tutorials similar to this one since this article was written about C. Or is it that C and C++ windows programming are similar enough that this article is still good if i want to write in C++?

The WinApi itself was written in C, but it there's no problem implementing it in a C++ based project. Both language are similar enough that you don't really need to know C in order to use it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.