Win32 Applications...

Pages: 12
How To Build a Win32 Applications,when i do compile everything is ok,but when i do build got a errors :( why is that :(:(:(
it would be good if you would tell us what development environment (Visual Studio, CodeBlocks, etc.) you are using, and it wouldn't hurt either if you stated if it was a console or GUI program you are trying to build.
I trying to make a simple Win32 program and i using VS C++ 6.
closed account (z05DSL3A)
There are Win32 console and Win32 GUI project. What are the build errors? We need to know what is happening to have any chance of helping.
If you post the code I'll try to help. I have VC6.
The Code is from tutorial that i download with files and i try to build but it wont a tutorial files are :
http://www.relisoft.com/win32/index.htm

Guys please help...
I wouldn't follow this tutorial if I were you. The authors intensions are good, I agree with making the Window into an actual class instead of the half-assed C version that Win32 uses, but they don't seem to actually understand the API what I meant was that they don't actually explain a few key things about the Window properties that you are setting, I know it's an intro but this is almost too simplified for my taste.

This statement here:
The GetMessage API is an interesting example of the bizarre Microsoft Troolean (as opposed to traditional, Boolean) logic. GetMessage is defined to return a BOOL, but the documentation specifies three types of returns, non-zero, zero and -1.

Tells me this author read a book or took a class on Win32 and now thinks they are an expert in the subject. In case you are wondering BOOL is a typedef of an integer, M$ does this because a simple true or false doesn't tell you WHY something failed but an actual error code would.

EDIT: Scratch that
Last edited on
:(
I'm pretty sure this is the tutorial I started off with: http://msdn.microsoft.com/en-us/library/ms632598(VS.85).aspx
It's less "flashey" and more informative.
Here are VC6 directions...

1) Open VC6;

2) Do a File >> New;

3) Select 'Projects' tab;

4) Select 'Win32 Application';

5) In 'Project Name' textbox type Winnie;
(PUT IT WHEREVER YOU LIKE)

6) Click 'OK' button;

7)In 'Step 1 of 1' select 'Empty Project';
(ALWAYS SELECT EMPTY PROJECT)

8) After its done its work you should have a 'File View' tab
in your Workspace Explorer. Select it;

9) Select the empty 'Source Files' folder and go to File menu
and select File >> New;

10) Select C++ Source File;

11) Fill in File Name text box with 'Main.cpp';

12) Paste the code from the web site into the editor. It should compile. I fooled with
it some. Here is what I compiled...

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


class WinClass
{
 public:
 WinClass(WNDPROC winProc, char const* className, HINSTANCE hInst);
 void Register() {RegisterClass(&_class);}

 private:
 WNDCLASS _class;
};


WinClass::WinClass(WNDPROC winProc, char const* className, HINSTANCE hInst)
{
 _class.style         = 0;
 _class.lpfnWndProc   = winProc;       // window procedure: mandatory
 _class.cbClsExtra    = 0;
 _class.cbWndExtra    = 0;
 _class.hInstance     = hInst;         // owner of the class: mandatory
 _class.hIcon         = 0;
 _class.hCursor       = LoadCursor (0, IDC_ARROW);   // optional
 _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
 _class.lpszMenuName  = 0;
 _class.lpszClassName = className; // mandatory
}


class WinMaker
{
 public:
 WinMaker (): _hwnd (0) {}
 WinMaker(char const* caption, char const* className, HINSTANCE hInstance);
 void Show(int cmdShow)
 {
  ShowWindow (_hwnd, cmdShow);
  UpdateWindow (_hwnd);
 }

 protected:
 HWND _hwnd;
};


WinMaker::WinMaker(char const* caption, char const* className, HINSTANCE hInstance)
{
 _hwnd=CreateWindow(className,caption,WS_OVERLAPPEDWINDOW,100,100,400,350,0,0,hInstance,0);
}


LRESULT CALLBACK WindowProcedure(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
  case WM_DESTROY:
    PostQuitMessage (0);
    return 0;
 }

 return DefWindowProc (hwnd, message, wParam, lParam );
}


int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char* cmdParam, int cmdShow)
{
 char className [] = "Winnie";
 int status;
 MSG  msg;

 WinClass winClass(WindowProcedure, className, hInst);
 winClass.Register();
 WinMaker win("Hello Windows!", className, hInst);
 win.Show (cmdShow);
 while ((status = GetMessage (& msg, 0, 0, 0)) != 0)
 {
    if(status == -1)
       return -1;
    DispatchMessage(& msg);
 }

 return msg.wParam;
}


Last edited on
I just overloaded the WinMaker() constructor so one can specify the x, y, width, and height parameters of the CreateWindow() call...

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


class WinClass
{
 public:
 WinClass(WNDPROC winProc, char const* className, HINSTANCE hInst);
 void Register() {RegisterClass(&_class);}

 private:
 WNDCLASS _class;
};


WinClass::WinClass(WNDPROC winProc, char const* className, HINSTANCE hInst)
{
 _class.style         = 0;
 _class.lpfnWndProc   = winProc;       // window procedure: mandatory
 _class.cbClsExtra    = 0;
 _class.cbWndExtra    = 0;
 _class.hInstance     = hInst;         // owner of the class: mandatory
 _class.hIcon         = 0;
 _class.hCursor       = LoadCursor (0, IDC_ARROW);   // optional
 _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
 _class.lpszMenuName  = 0;
 _class.lpszClassName = className; // mandatory
}


class WinMaker
{
 public:
 WinMaker (): _hwnd (0) {}
 WinMaker(char const* caption, char const* className, HINSTANCE hInstance);
 WinMaker(char const* caption, char const* classname, int x, int y, int cx, int cy, HINSTANCE hInstance);
 void Show(int cmdShow)
 {
  ShowWindow (_hwnd, cmdShow);
  UpdateWindow (_hwnd);
 }

 protected:
 HWND _hwnd;
};


WinMaker::WinMaker(char const* caption, char const* className, HINSTANCE hInstance)
{
 _hwnd=CreateWindow(className,caption,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,0);
}


WinMaker::WinMaker(char const* caption, char const* className, int x, int y, int cx, int cy,  HINSTANCE hInstance)
{
 _hwnd=CreateWindow(className,caption,WS_OVERLAPPEDWINDOW,x,y,cx,cy,0,0,hInstance,0);
}


LRESULT CALLBACK WindowProcedure(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
  case WM_DESTROY:
    PostQuitMessage (0);
    return 0;
 }

 return DefWindowProc (hwnd, message, wParam, lParam );
}


int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char* cmdParam, int cmdShow)
{
 char className [] = "Winnie";
 int status;
 MSG  msg;

 WinClass winClass(WindowProcedure, className, hInst);
 winClass.Register();
 WinMaker win("Hello Windows!", className, 100, 100, 450, 400, hInst);
 win.Show (cmdShow);
 while ((status = GetMessage (& msg, 0, 0, 0)) != 0)
 {
    if(status == -1)
       return -1;
    DispatchMessage(& msg);
 }

 return msg.wParam;
}
Last edited on
Here's another style...

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

typedef struct    WindowsEventArguments
{
 HWND             hWnd;
 WPARAM           wParam;
 LPARAM           lParam;
 HINSTANCE        hIns;
}WndEventArgs, *lpWndEventArgs;


long fnWndProc_OnCreate(lpWndEventArgs Wea)
{
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
 return 0;
}


long fnWndProc_OnDestroy(lpWndEventArgs Wea)
{
 PostQuitMessage(WM_QUIT);
 return 0;
}


LRESULT CALLBACK fnWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 WndEventArgs wea;

 switch (msg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnCreate(&wea);
  case WM_DESTROY:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnDestroy(&wea);
 }

 return DefWindowProc(hwnd,msg,wParam,lParam);
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Winnie");
 WNDCLASSEX wc;
 MSG messages;
 HWND hWnd;

 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=0;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);   wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClassName,_T("Winnie"),WS_OVERLAPPEDWINDOW,200,100,400,350,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}


This one compiles a wee bit smaller.
Last edited on
Man,God Bless You it's work fine now :) Thank you very much :)
Im again bored :( im sorry if i cross the line,but i can't make a message box

#include <windows.h>



int DisplayResourceNAMessageBox()
{
int msgboxID = MessageBox(
NULL,
(LPCWSTR)L"Resource not available\nDo you want to try again?",
(LPCWSTR)L"Account Details",
MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
);

switch (msgboxID)
{
case IDCANCEL:
// TODO: add code
break;
case IDTRYAGAIN:
// TODO: add code
break;
case IDCONTINUE:
// TODO: add code
break;
}

return msgboxID;
}


.......
You likely need to learn all about ansi character strings verses wide character strings, the TCHAR macros in tchar.h, the UNICODE - _UNICODE defines, so on and so forth. I'm at the point of thinking somebody in authority here ( maybe disch ) ought to make a 'sticky' about it. Its about the 1st thing an aspiring Windows programmer in C++ ought to learn (other languages tend not to have to deal with it as much).
But to answer your question, this will work in your vc6...

MessageBox(hWnd, "Some Message", "Title", MB_OK);

Just for the record, the following are the ONLY acceptable forms:

1
2
3
4
MessageBox(hWnd, TEXT("Some Message"), TEXT("Title"), MB_OK);
MessageBox(hWnd, _T("Some Message"), _T("Title"), MB_OK);
MessageBoxA(hWnd, "Some Message", "Title", MB_OK);
MessageBoxW(hWnd, L"Some Message", L"Title", MB_OK);


So it is crystal clear for the OP, I add: ANY other combination is just wrong. And so you can apply to other function calls: The ending in A uses regular strings and char C strings; the ending in W uses Unicode literals (denoted with the letter L) and wchar_t C strings; the one that doesn't end in A or W uses literals inside the TEXT() or _T() macros and TCHAR C strings.
Actually, there is ONE OTHER MessageBox() function that I use, just to avoid all the above nonsense --

MessageBox(hWnd, IGNORE_THE_COMPILER("Some Message"), "Title", MB_STUFF_THE_COMPILER);

That way I don't have to put up with compiler annoyances. -:)
Last edited on
But where i must put it :( because i try to put after this :

WinClass winClass(WindowProcedure, className, hInst);
winClass.Register();
WinMaker win("Hello Windows!", className, hInst);
win.Show (cmdShow);
while ((status = GetMessage (& msg, 0, 0, 0)) != 0)


and it says 1 error :(
Pages: 12