Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Win32 Tutorial Part 1 - MessageBox

hello world wcscmp

  • Please log in to reply
19 replies to this topic

#1 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 23 February 2012 - 07:48 AM

#ifndef UNICODE
#define UNICODE
#endif

#include<windows.h>


int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, PWSTR szCmdLine, int nCmdShow)
{
MessageBox(NULL,L"Body Message",L"Title Here",MB_OK);
return 0;

}

windows.h is a header that contains other important Header file. And is important if you want to create windows application.

WINAPI is a calling convention that specifies how the arguments are received by the function. Here the arguments are received right to left.
And it also specifies that the calling function is responsible for cleaning stack. (If you didn't understood a bit, do not worry as it is not important now. just remember to write it.)

wWinMain is an Entry function name similar to main() function in C.

hinstance is a handle(variable) to program running instance. It is a unique number assigned by operating system.

hprevinstance is always null. It is not used in Win32 systems.

szCmdLine is pointer to command line string.

nCmdShow is a parameter that controls how the window should be displayed on screen (i.e. maximized, minimized). We will discuss it later.

MessageBox is a function that shows a minimal dialog box.
First parameter is handle to owner of this MessageBox. In our program there is no owner of this MessageBox. More about owner window later. Since there are no owner of this window the first parameter is passed NULL.

Second Parameter is the message to be displayed.

Third Parameter is caption or title message of the MessageBox.

Fourth Message is a defined constant that represents OK button .



Important Typedefs:

typedef HANDLE HINSTANCE; //HANDLE is void*
typedef WCHAR *PWSTR; // WCHAR is wchar_t


Note:
wchar_t is UNICODE equivalent to char datatype.
Ex:-

char a = ‘m’;
wchar_t a = L’a’; //L is used to denote that ‘a’ is Unicode character and not an ASCII character.

WCHAR A =L’a’; // same as wchar_t


char a[100]=”hello world”; // ASCII String
wchar_t a[100] =L”hello world”; // Unicode string
WCHAR a[100] == L”hello world”; // WCHAR is just typedef of wchar_t



strlen(a); //returns the length of ASCII string;
wcslen(a); // returns the lenght of Unicode string;

Rule:

For string functions
strcmp --> wcscmp (ASCII --> Unicode)
strcpy --> wcscpy (ASCII --> Unicode)
strcat -->wcscat (ASCII --> Unicode)
....
....
etc.

<string.h> --> <wchar.h> (ASCII --> Unicode)
  • 0

#2 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 20 May 2012 - 11:15 PM

I am getting the following error in Code::Blocks.

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 May 2012 - 06:09 AM

I'm afraid that there is an extra 'w' prefixed with 'WinMain'. sun12zenith, can you try without that little 'w' before 'WinMain'?
  • 0

#4 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 21 May 2012 - 06:37 AM

Sorry, kernelcoder but same error after changing the extra 'w' with WinMain. I think the author has used some other compiler. What say?

||=== MessageBox, Debug ===|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|
||=== Build finished: 1 errors, 0 warnings ===|
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 May 2012 - 06:49 AM

I just tried with CodeBlcoks and 'WinMain' is fine with me. I'm not sure what caused errors like that. What type of project you created?

Follow the steps to create a win32 gui project on Windows...
  • File -->New --> Project
  • Select 'Win32 Gui Project' from the 'New from Template' wizard.

  • 0

#6 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 21 May 2012 - 07:17 AM

OK kernelcoder you were successful but here is the process i tried.

1. File>New>Project
2. Win32 Gui Project
3. Saved as MessageBox3
4. pasted the above code
#ifndef UNICODE
#define UNICODE
#endif
#include<windows.h>

int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, PWSTR szCmdLine, int nCmdShow)
{
MessageBox(NULL,L"Body Message",L"Title Here",MB_OK);
return 0;
}


5. Build>Build
6. Got the same error.

Also changed the extra 'w' and tried again but no success.
  • 0

#7 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 May 2012 - 07:35 AM

Are you trying on Windows?
  • 0

#8 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 21 May 2012 - 07:38 AM

Are you trying on Windows?


Oh! come on. Ofcourse Windows XP SP3 and Code::Blocks 10.05 the latest version.
  • 0

#9 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 May 2012 - 07:44 AM

I think you need to add the -mwindows option with MinGW for Win32 application developing. Follow -- "Project->Properties->Project Settings tab-> Project's build options->Linker settings->link libraries->other" and put -mwindows there.

If the above steps does not work for you, can you try the following code and let us know?

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR szCmdLine, int nCmdShow)
{
MessageBox(NULL,L"Body Message",L"Title Here",MB_OK);
return 0;

}

  • 1

#10 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 21 May 2012 - 08:07 AM

Hurrah !!! it worked. You are really a hardworking person and know the details or to say know the pain in the fish's stomach which is deep in the ocean by just sitting at home.

Thank you.
  • 0

#11 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 21 May 2012 - 08:09 AM

So which one worked for you -- the -mwindows linker option or the code?
  • 0

#12 sun13zenith

sun13zenith

    CC Newcomer

  • Member
  • PipPip
  • 12 posts
  • Programming Language:C, Java, C++, PHP
  • Learning:C, Java, C++, PHP

Posted 21 May 2012 - 08:15 AM

So which one worked for you -- the -mwindows linker option or the code?


No No the -mwindows thing is not present in the dialog box which u mentioned as there is no MingW thing in that . I just used the code. And and and saw first window created in my life. As a newbie you showed me the thing which was just imaginary for me and the result was a small window. I can understand at what level you work. U will be a great help as just reading books does not help.

Thank you again.
  • 0





Also tagged with one or more of these keywords: hello world, wcscmp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download