WinMain()

Pages: 12
Hi, in my book its got:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)

{
static TCHAR szAppName[] = TEXT(“Skeleton”);
WNDCLASSEX wndclass;
HWND hWindow;
MSG msg;

// Create the window class for the main window

wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_SKELETON));
wndclass.hIconSm = LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_SKELETON_SM));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

I understand that winmain() is the starting point for a program, but my book only tells me to put in these lines of code, it doesnt actaully tell me what all of this means. I suppose it doesnt tell me because once i put it in a game engine theres no real need for me to understand it, BUT i'd prefer to :)

Could anyone clearly explain to me whats going on here, including what the parameters are for winmain().

It is alot to ask i know :) thanks.
Erm you would better googling this I think I know
HINSTANCE
Is a handle to an instance, I know some of the rest but agian google is you friend......
http://www.zetcode.com/tutorials/winapi/
Nice little tutorial that I found when I was starting out, might explain a little more
Thanks, ok, here is my next set of questions:

1. as hInstance is a number identifying the instance of a program within the OS environment, does windows give hInstance this number, and if it does would i get an error if i didnt include this parameter?

2. on the website im looking at, its got " hPrevInstance parameter is always NULL. It is a legacy from 16-bit Windows. ". If this is not important, why do we have to put it?

3. I understand the nCmdShow is a value which shows how the window will be displayed. Minimized, maximized or hidden. But do we have to give it a value? And do we HAVE to give WinMain this parameter?

4. im not quite sure what is stored in lpCmdLine?

:)
If you read Petzold, you'd have the answers. There's just too much depth to Windows to start answering questions like this, you have to make an effort to read the material.
Im not able to purchase that book at the moment, is there anyone who thinks they can help with those questions? :)
The signature of WinMain is as it is. It is called by the C start up code with those parameters, you cannot change this.

1. hInstance is assigned by Windows, it identifies your particular instance of the application. Think of it as something like a process id, it's a unique number that identifies your instance of the app.

2. In win16, hPrevInstance gave you the instance handle of the previous invocation of the application. In win32 this is always null.

3. As I said before, this is passed to you, you don't assign it a value, it's assigned for you.

4. lpCmdLine is the full unparsed command line passed to the app.
as "Windows programs can still be started from the command line. The parameters given are stored in lpCmdLine", what exactly is stored in lpCmdLine, how would i go about starting the program for the command line, would i have to find out what information is in lpCmdLine
Why don't you display it with MessageBox()? There's no mystery to it.

If you want to write console apps that don't have a GUI, you won't have to deal with all this. The program entry point will be main() like normal C/C++ apps. The use of WinMain is a Windows design flaw.
Ok :).

Also, as you said the value for nCmdShow is assigned for me, im guessing the default value makes the app maximized, and if i wanted it minimized i would have to tell it to be so, right?

Thanks for all your help.
Look at any Windows application shortcut. You'll see an option to start the app normal, maximized or minimized. That just selects the nCmdShow value to pass into the app. The app can use it, but doesn't have to respect it. You can make your app always start minimised, but if you want to respond to the shortcut, you'll need to use nCmdShow.
Ok, i understand what your saying, but does windows give iCmdShow a default value so that it knows how to display the window? or is it empty until we put something in there?
Last edited on
This is what i mean, take a look at this: http://msdn.microsoft.com/en-us/library/ms633559

There you will see the values what can go into this parameter, but which one is put in there by windows by default?

Also, whats the difference between iCmdShow and nCmdShow, ive seen them both used for the same purpose, is there any differnce or can i use any?
Why don't you print the value passed to your app? You can play about with setting the shortcut to different values.
but which one is put in there by windows by default?

SW_SHOWNORMAL (1)

whats the difference between iCmdShow and nCmdShow, ive seen them both used for the same purpose, is there any differnce or can i use any?

There is no difference, other than the name.
Last edited on
Thanks grey wolf, thats the answer i was looking for, and thanks kbw for the help.
I have another question, ill have to buy another book when i can as the one ive got doesnt help much and assumes you already know some of it, even though the book is supposed to be for beginners, anyway... its got:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

{

static TCHAR szAppName[] = TEXT("Skeleton");
WNDCLASSEX wndclass;
HWND hWindow;
MSG msg;


Now, i know what im doing by putting "WNDCLASSEX wndclass;", im creating a window class of my own, so i could put wndclass.style = whatever, and so on...

I THINK i know what im doing by putting "HWND hWindow;", am i creating a handle to a window? and if i wanted to created a second window, could i make a new handle and call it hWIndowTwo?

But im not certain on what im doing by putting:

static TCHAR szAppName[] = TEXT("Skeleton");
MSG msg;

What are they for, and how would i use them





Last edited on
PLUS... do you HAVE to define the attributes of a window, or does WNDCLASSEX have default values for all of the attributes?
?
Check the WinAPI documentation for that information.
Pages: 12