Why use InitCommonControls and comctl32.lib ?

Hello everyone,
I am a complete newbie to Win32 API programming and I am also new to this forum. I would like to know why do we need to link the library comctl32.lib and why we need to use the function InitCommonControls ? I am asking because I am using code that seems to run fine and can create dialogs without the use of the above mentioned. Yet in books like Herbert Schildt's Windows 2000 programming from Ground Up it is mentioned that in order to create common dialogs we must link to comctl32.lib but in newer OS like Windows 8(the one I am using) we do not need to since I am already linking to something called comdlg32.lib. What is the difference between the two ? Is there any advantage or disadvantage of one over the other ? And why use the function InitCommonControls() since my code can work fine without them ? Will not linking to comctl32.lib cause compatibility problems in older versions of Windows? I am using Visual Studio 2012 and Windows 8 Pro x64.

The code that I am using is the following -:

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
#include "resource.h"
#include<Windows.h>
#include<CommCtrl.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM);

char szWinName[]="Test Application";

HWND hDlg = NULL;
HINSTANCE hInstance;

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
				   LPSTR lpszArgs, int nWinMode)

{
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wcl;

	wcl.cbSize = sizeof(WNDCLASSEX);

	wcl.hInstance = hThisInst;
	wcl.lpszClassName = szWinName;
	wcl.lpfnWndProc = WindowFunc;
	wcl.style = 0;

	wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcl.hIconSm = NULL;
	wcl.hCursor = LoadCursor(NULL, IDC_ARROW);

	wcl.lpszMenuName = NULL;
	wcl.cbClsExtra = 0;
	wcl.cbWndExtra = 0;

	wcl.hbrBackground = (HBRUSH) GetStockObject(HOLLOW_BRUSH);

	hInstance = hThisInst;

	if(!RegisterClassEx(&wcl)) return 0;

	hwnd = CreateWindow(
		szWinName,
		"Should not be showing",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL
		);

	while (GetMessage(&msg, NULL, 0, 0) > 0)
	if (!hDlg||!IsDialogMessage(hDlg,&msg))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, 
							WPARAM wparam, LPARAM lparam)
{ 
	switch(message){
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_CREATE:
			hDlg=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),
				hwnd,(DLGPROC)DialogFunc);
			break;
		default:
			return DefWindowProc(hwnd,message,wparam,lparam);
	}
	return 0;
}

BOOL CALLBACK DialogFunc(HWND hwnd, UINT message, 
						 WPARAM wparam, LPARAM lparam)
{

	switch (message)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd);
		hDlg=NULL;
		PostQuitMessage(0);
		return TRUE;

	default:
		return FALSE;
	}
	return FALSE;
}
Normally, yes you SHOULD link to comctl32.lib and use InitCommonControls(), or InitCommonControlsEx() if you need to. The reason why you're program is running perfectly fine without it is probably because you aren't actually using anything that requires comctrl32.dll, or even anything as part of the header <commctrl.h>.

However, if you DID need them, then if you didn't link to comctrl32.lib you would some linker error, and if you didn't use InitCommonControls() then it would might compile, but it wouldn't work.

Hope I helped.
@ NT3;

But I am using the combo box isn't that a part of the common controls ?

My resource file is as follows-:

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
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOG1 DIALOGEX 0, 0, 161, 63
STYLE DS_ABSALIGN | DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_WINDOWEDGE
CAPTION "Test Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    COMBOBOX        IDC_COMBO1,32,18,95,23,CBS_DROPDOWNLIST | CBS_SORT | WS_VSCROLL | WS_TABSTOP
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
    IDD_DIALOG1, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 154
        TOPMARGIN, 7
        BOTTOMMARGIN, 56
    END
END
#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog Info
//

IDD_DIALOG1 DLGINIT
BEGIN
    IDC_COMBO1, 0x403, 2, 0
0x0031, 
    IDC_COMBO1, 0x403, 2, 0
0x0032, 
    IDC_COMBO1, 0x403, 2, 0
0x0033, 
    IDC_COMBO1, 0x403, 2, 0
0x0034, 
    IDC_COMBO1, 0x403, 2, 0
0x0035, 
    0
END

#endif    // English (United States) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED


Can you give me some examples of some controls that are there in comctl32.dll ? Then I can test it out.
Last edited on
Can anyone give a situation where I have to link to comctl32.lib and MUST call InitCommonControls()?? Because without I can't find the relevance of these two things, my above given code in the main post seems to be working fine without them, so if I have an example then I will know EXACTLY when to use them and when not to.
Topic archived. No new replies allowed.