Windows Programming Noob - New datatypes?

Hi, I have some experience with console programming but none with programming in windows and I'm not well versed in the state of the art at all.
I downloaded MS Visual C++ which pretty much wrote a program that creates a window for me. I am trying to understand this program with the help of online resources and thus far have a good understanding of the role that most of these function play. However, it seems that windows programming involves some new datatypes that I havent encountered before, here there are:

HINSTANCE

TCHAR

ATOM

LRESULT CALLBACK

INT_PTR CALLBACK

MSG

HACCEL

WNDCLASSEX

PAINTSTRUCT

HDC

here is their context:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// first_prog1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "first_prog1.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(	HINSTANCE hInstance,
						HINSTANCE hPrevInstance,
						LPTSTR    lpCmdLine,
						int       nCmdShow)

{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_FIRST_PROG1, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FIRST_PROG1));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FIRST_PROG1));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_FIRST_PROG1);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(	szWindowClass,				//--- Define & create the window!
						TEXT("Hello World!!"), 
						WS_OVERLAPPEDWINDOW, 
						10, 
						10, 
						1000, 
						500, 
						NULL, 
						NULL, 
						hInstance, 
						NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(	HWND hWnd, 
							UINT message, 
							WPARAM wParam, 
							LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
			Ellipse( hdc, 20, 20, 160, 160 );
			Rectangle( hdc, 50, 50, 90, 90 );
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}


I am new to OOP and realize some (or all) of these may be pre-defined classes but cant help to feel like they are standardized classes that I should get very familiar with. Would someone be kind enough to explain each of those datatypes? Thus far, I am only familiar with int, double, bool, char, & string.

Thanks for any help!


Last edited on
** Update **

I found this page that explains what those datatypes are:

http://www.powerbasic.com/support/help/pbcc/comparative_data_types_c_c++.htm

Turns out those are all "SDK" types. So what is an SDK??

And how is a LRESULT, being a signed 32-bit LONG, different form an int which is also a signed 32-bit Long??

And whats the difference between signed and unsigned? Sorry for all the questions, the technical makup of programming in windows is very new to me.
Last edited on
Why dont you try C++ Builder instead? It does not generate this much code for windows (forms) and everything is based on visual components instead of code. Much easier for start.
I will check it out but I would rather have to deal with code. May be a steeper learning curve but it'll better me in the end. I am a mechanical engineering grad looking to start a career in software engineering so my goal at the moment is to build my skills enough to attain an entry-level position.
Im not really sure how LRESULT is different from an int, but like most things I just go with the flow.

The difference between a signed and unsigned int however is the length that it can hold.
For example a signed can hold positive and negative values where as an unsigned can only hold positive but can hold twice as much of it.

eg(THIS IS NOT ACTUAL VALUES OR LIMITS)

unsigned int1 can be 0 to 100
signed int2 can be -50 to +50

ANywhays I hope this helps.
Also what I do with most of the code, mostly the definition stuff, is to stick it into a header file, aptly named guiDefinitions.h so it is out of my way. I also prefer working with the code as I like knowing how stuff should link together.
ok :)

So let's begin:

HINSTANCE - this can represent a program instance or module handle. For example, if your application uses additional dll to load functions HINSTANCE object would represent that dll.

TCHAR - represent a string type. In c++ there are many string types and TCHAR is one of them. This one can be used as Unicode or Ansi type of string.

ATOM - usually this is 16bit WORD object. I think WORD is typedef for unsigned short.

LRESULT CALLBACK - LRESULT is a return value: long int. CALLBACK is the calling convention.

INT_PTR CALLBACK .. int pointer..

HDC - As I remember this is device content handler. Used when programming graphics
cool. Thanks, that makes sense.

got one more if you dont mind. what does "&" mean in C++ if you place it in front of something.

Ex. WNDCLASSEX my_new_window; ...... RegisterClassEx(&my_new_window)

..does & turn my_new_window into a pointer?

Everybody that starts with the Windows Api to do SDK style coding should get a copy of Charles Petzolds "Programming Windows". Either his latest (which is copywrite 1998) or his Windows 95 book will do. All, all, all, this stuff, plus much more, is explained in excrutiating detail. Its not a good idea to go guessing, or just accept things on faith, or go with the flow. Ya gotta KNOW!
Ill pick that up, thanks! btw, your name doesnt happen to be Charles Petzolds, does it? ;)
No. In all seriousness, his books ARE the way to get started. I will admit to being a big fan of his though.
No. In all seriousness, his books ARE the way to get started. I will admit to being a big fan of his though.


Just curious to know, are the Win SDK API remain unchanged even for the latest Windows 7 and the Windows Mobile OS also ?
As far as I know, the windows API preserves backward compatibility. You choose header versions by using macros.
http://msdn.microsoft.com/en-us/library/aa383745%28v=vs.85%29.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2007/04/11/2079137.aspx

[edit]

@qwerty1
Look here for windows data types
http://msdn.microsoft.com/en-us/library/aa383751%28v=vs.85%29.aspx
Last edited on
Thanks! that link was really helpful! * add to favorites * ;)

So here's what Im thinking..please tell me if Im right.. All theses 'datatypes' are not fundamental datatypes. That is, they're standardized datatypes written by microsoft developers (or whoever) to make writing windows code easier for programmers in the field (and themselves). i.e. instead of having to write new code for a handle every time you write a new program, you simply use the HWND datatype. Essentially they are standardized 'classes'. Am I right here? Im assuming that if you go in and look at the actual code for the HWND datatype, its actually comprised of the fundamental int, char, double, string datatypes. Right?
It depends. Unfortunately, nothing is simple anymore. In many cases these new data types of which you are speaking are simply typedefs of the primitive data types such as int, long, etc. For example, if you do a search for LRESULT in the various includes, I think you'll likely find its a typedef of a long. I havn't checked in awhile, so if somebody knows better, please correct me. Replace the

LRESULT CALLBACK

in the Window Procedure with...

long __stdcall and I think you'll see that works on Win32. I believe it won't work on Win64.

In terms of the various handles - and as you are seeing, there are lots of different kinds, they are actually virtual memory pointers - opaque pointers is another name for this, and Windows itself keeps track of them. These kinds of variables are never used in calculations or anything like that. They simply give you access to various functionalities of Windows.

Virtually all these things are 32 bit integers in Win32. Most fit in a signed int. In Win64 they are going to be 64 bit as they refer to memory locations, which there will be 64 bit.

Casting with them can be a pain. I spend about half my time coding in PowerBASIC, and in that language you don't have this proliferation of typedefs, which is nice. All these things are essentially PowerBASIC 'Long's, which is 32 bit. You can rather interchangeably use a DWORD too.

As I mentioned, Petzold covers all this stuff, as well as the fundamentals of the architecture.

Did I give you this link...

http://www.cplusplus.com/forum/windows/31963/

I posted a lot of info and programs there you could copy and paste. I also have C/C++ tutorials here...

http://www.jose.it-berater.org/smfforum/index.php?board=380.0

The GUI stuff starts around tutorial 36 or 37
Last edited on
That makes sense. Thanks for the links.. bookmarked as well. Definitely have some homework to sort through.
Topic archived. No new replies allowed.