"HelloWorld" on win32 programming

Hi all

i am new to windows programming, and i write an "HelloWorld" sample, it's:
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
#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(HWND hwnd,
							UINT uMsg,
							WPARAM wParam,
							LPARAM lParam
);

int _WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   WNDCLASS wndcls;

   wndcls.cbClsExtra = 0;
   wndcls.cbWndExtra = 0;
   wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   wndcls.hCursor = LoadCursor(NULL,IDC_CROSS);
   wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
   wndcls.hInstance = hInstance;
   wndcls.lpfnWndProc = WinSunProc;
   wndcls.lpszClassName = "2009";
   wndcls.lpszMenuName = NULL;
   wndcls.style = CS_HREDRAW | CS_VREDRAW;

   RegisterClass(&wndcls);

   HWND hwnd;
   hwnd = CreateWindow("2009","中国北京",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

   ShowWindow(hwnd, SW_SHOWNORMAL);
   UpdateWindow(hwnd);

   MSG msg;
   while(GetMessage(&msg,NULL,0,0))
   {
       TranslateMessage(&msg);
	   DispatchMessage(&msg);
   }
   return 0;
}

LRESULT CALLBACK WinSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   switch(uMsg)
   {
   case WM_CHAR:
	   char szChar[20];
	   sprintf(szChar,"Char is %d", wParam);
        MessageBox(hwnd,szChar,"中国北京",MB_OK);
	    break;
   case WM_LBUTTONDOWN:
	    MessageBox(hwnd,"Mouse Click","中国北京",MB_OK);
		HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc,0,50,"Win32应用程序",strlen("Win32应用程序"));
		ReleaseDC(hwnd,hdc);
	    break;
   case WM_PAINT:
	    HDC hDc;
		PAINTSTRUCT ps;
		hDc = BeginPaint(hwnd,&ps);
		TextOut(hDc,0,0,"中国",strlen("中国"));
		EndPaint(hwnd,&ps);
	    break;
   case WM_CLOSE:
	    if (IDYES == MessageBox(hwnd,"是否真的结束?","应用实例",MB_YESNO))
	    {
			DestroyWindow(hwnd);
	    }
	    break;
   case WM_DESTROY:
	    PostQuitMessage(0);
	    break;
   default:
	    return DefWindowProc(hwnd,uMsg,wParam,lParam);
   }

   return 0;
}








but i got some error msg as this:
1
2
3
4
5
6
7
8
C:\MinGW\example\Win32Test>gcc -Wall test.c -I c:\MinGW\include -o test
test.c: In function `WinSunProc':
test.c:47: error: syntax error before "char"
test.c:48: error: `szChar' undeclared (first use in this function)
test.c:48: error: (Each undeclared identifier is reported only once
test.c:48: error: for each function it appears in.)
test.c:59: error: syntax error before "hDc"
test.c:61: error: `hDc' undeclared (first use in this function) 


by the way, i install minGW to compile the source with default setting.


how to fix it, help~~
You can't declare your variables inside the switch statement. You need to define szChar[20] and hDc before you enter the switch statements. It would look like this...

1
2
3
4
5
char szChar[20];
HDC hDc;

switch(uMsg)
{
Last edited on
Thank Lamblion

i update it with your code, but i still give some error msg as following:

1
2
3
4
5
C:\MinGW\example\Win32Test>gcc -Wall test.c -I c:\MinGW\includ
test.c: In function `WinSunProc':
test.c:70: error: `hDc' undeclared (first use in this function
test.c:70: error: (Each undeclared identifier is reported only
test.c:70: error: for each function it appears in.)



thks
The error message indicates you haven't properly declared the hDc variable, or else you have forgotten to erase the declaration from your switch statement.
reply to Lamblio,

i have update my source, but it still report some error

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
#include <windows.h>
#include <stdio.h>

//声明回调函数
LRESULT CALLBACK WinSunProc(HWND hwnd,
							UINT uMsg,
							WPARAM wParam,
							LPARAM lParam
);

int _WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   WNDCLASS wndcls;

   //设计窗口类
   wndcls.cbClsExtra = 0;
   wndcls.cbWndExtra = 0;
   wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   wndcls.hCursor = LoadCursor(NULL,IDC_CROSS);
   wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
   wndcls.hInstance = hInstance;
   wndcls.lpfnWndProc = WinSunProc;
   wndcls.lpszClassName = "2009";
   wndcls.lpszMenuName = NULL;
   wndcls.style = CS_HREDRAW | CS_VREDRAW;

   //注册窗口类
   RegisterClass(&wndcls);

   //创建窗口
   HWND hwnd;
   hwnd = CreateWindow("2009","中国北京",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

   //显示窗口
   ShowWindow(hwnd, SW_SHOWNORMAL);
   UpdateWindow(hwnd);

   //消息循环
   MSG msg;
   while(GetMessage(&msg,NULL,0,0))
   {
       TranslateMessage(&msg);
	   DispatchMessage(&msg);
   }
   return 0;
}

LRESULT CALLBACK WinSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
       
	char szChar[20];
	HDC hdc;
	PAINTSTRUCT ps;
	
   switch(uMsg)
   {
   case WM_CHAR:
	    //char szChar[20];
	    sprintf(szChar,"Char is %d", wParam);
        MessageBox(hwnd,szChar,"中国北京",MB_OK);
	    break;
   case WM_LBUTTONDOWN:
	    MessageBox(hwnd,"Mouse Click","中国北京",MB_OK);
		//HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc,0,50,"Win32应用程序",strlen("Win32应用程序"));
		ReleaseDC(hwnd,hdc);
	    break;
   case WM_PAINT:
	    //HDC hDc;
		//PAINTSTRUCT ps;
		
		hDc = BeginPaint(hwnd,&ps);
		TextOut(hDc,0,0,"中国",strlen("中国"));
		EndPaint(hwnd,&ps);
	    break;
   case WM_CLOSE:
	    if (IDYES == MessageBox(hwnd,"是否真的结束?","应用实例",MB_YESNO))
	    {
			DestroyWindow(hwnd);
	    }
	    break;
   case WM_DESTROY:
	    PostQuitMessage(0);
	    break;
   default:
	    return DefWindowProc(hwnd,uMsg,wParam,lParam);
   }
        
       
   return 0;
}



and it show the error msg:
1
2
3
4
5
6
7
C:\MinGW\example\HelloWin32>
C:\MinGW\example\HelloWin32>
C:\MinGW\example\HelloWin32>mingw32-gcc hello.c -o test
hello.c: In function `WinSunProc':
hello.c:73: error: `hDc' undeclared (first use in this function)
hello.c:73: error: (Each undeclared identifier is reported only once
hello.c:73: error: for each function it appears in.)
You have --

HDC hdc

but then you have "hDc" instead of "hdc".

You can't do that. It has to match the case exactly.

but after i update with your code, it report some strange error msg:


1. source code : hello.c
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
#include <windows.h>
#include <stdio.h>

//声明回调函数
LRESULT CALLBACK WinSunProc(HWND hwnd,
							UINT uMsg,
							WPARAM wParam,
							LPARAM lParam
);

int _WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   WNDCLASS wndcls;

   //设计窗口类
   wndcls.cbClsExtra = 0;
   wndcls.cbWndExtra = 0;
   wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   wndcls.hCursor = LoadCursor(NULL,IDC_CROSS);
   wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
   wndcls.hInstance = hInstance;
   wndcls.lpfnWndProc = WinSunProc;
   wndcls.lpszClassName = "2009";
   wndcls.lpszMenuName = NULL;
   wndcls.style = CS_HREDRAW | CS_VREDRAW;

   //注册窗口类
   RegisterClass(&wndcls);

   //创建窗口
   HWND hwnd;
   hwnd = CreateWindow("2009","中国北京",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

   //显示窗口
   ShowWindow(hwnd, SW_SHOWNORMAL);
   UpdateWindow(hwnd);

   //消息循环
   MSG msg;
   while(GetMessage(&msg,NULL,0,0))
   {
       TranslateMessage(&msg);
	   DispatchMessage(&msg);
   }
   return 0;
}

LRESULT CALLBACK WinSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
       
	char szChar[20];
	HDC hdc;
	PAINTSTRUCT ps;
	
   switch(uMsg)
   {
   case WM_CHAR:
	    //char szChar[20];
	    sprintf(szChar,"Char is %d", wParam);
        MessageBox(hwnd,szChar,"中国北京",MB_OK);
	    break;
   case WM_LBUTTONDOWN:
	    MessageBox(hwnd,"Mouse Click","中国北京",MB_OK);
		//HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc,0,50,"Win32应用程序",strlen("Win32应用程序"));
		ReleaseDC(hwnd,hdc);
	    break;
   case WM_PAINT:
		//PAINTSTRUCT ps;
		
		hdc = BeginPaint(hwnd,&ps);
		TextOut(hdc,0,0,"中国",strlen("中国"));
		EndPaint(hwnd,&ps);
	
	    break;
   case WM_CLOSE:
	    if (IDYES == MessageBox(hwnd,"是否真的结束?","应用实例",MB_YESNO))
	    {
			DestroyWindow(hwnd);
	    }
	    break;
   case WM_DESTROY:
	    PostQuitMessage(0);
	    break;
   default:
	    return DefWindowProc(hwnd,uMsg,wParam,lParam);
   }
        
       
   return 0;
}


2. compile error from console:
1
2
3
4
5
6
7
8
9
10
C:\MinGW\example\HelloWin32>mingw32-gcc hello.c -o test
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccMeI9ly.o:hello.c:(.text+0x1f)锛氬鈥楪etSt
ockObject@4鈥欐湭瀹氫箟鐨勫紩鐢
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccMeI9ly.o:hello.c:(.text+0x25a)锛氬鈥楾ext
OutA@20鈥欐湭瀹氫箟鐨勫紩鐢
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccMeI9ly.o:hello.c:(.text+0x2ba)锛氬鈥楾ext
OutA@20鈥欐湭瀹氫箟鐨勫紩鐢
C:/MinGW/lib/libmingw32.a(main.o):main.c:(.text+0xd2)锛氬鈥榃inMain@16鈥欐湭瀹
氫箟鐨勫紩鐢
collect2: ld returned 1 exit status


why? thks
Last edited on
I don't know. I'm not familiar with your compiler. Someone else will have to jump in here. It doesn't appear that your syntax is a problem anymore, so I don't know why it's giving you these errors.
to Lamblion

i see, but thk u.

actually, i suppose my MinGW installation lead to this error, but i do this with default .


Thanks
change int _WinMain() to int WINAPI WinMain()


compile
mingw32-gcc.exe -Wall -O2 -c hello.c


link
mingw32-g++.exe -o hello.exe hello.o -s -lgdi32 -luser32 -lkernel32 -mwindows


i suggest you use an IDE instead of manually compiling from the command line.
http://www.codeblocks.org/downloads/5

EDIT: this compiled very well in Code::Blocks using Mingw compiler
Last edited on
all,

thks, i fix this issuse.

and i suppose my compile - minGW lead to this error, because this code could be executed in visual c++

thks all.
Topic archived. No new replies allowed.