473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

WinMain()

Hunderpanzer
Hey guys. I'm trying to learn some windows programming, but i'm getting some errors with the code:


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>    // include the basic windows header file
  2.  
  3. // the entry point for any Windows program
  4. int WINAPI WinMain(HINSTANCE hInstance,
  5.                    HINSTANCE hPrevInstance,
  6.                    LPSTR lpCmdLine,
  7.                    int nShowCmd)
  8. {
  9.     // create a "Hello World" message box using MessageBox()
  10.     MessageBox(NULL,
  11.                L"Hello World!",
  12.                L"Just another Hello World program!");
  13.                MB_ICONEXCLAMATION | MB_OK);
  14.  
  15.     // return 0 to Windows
  16.     return 0;
  17. }
  18.  
The errors I get (using Dev-C++) are


In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':

cannot convert `const wchar_t*' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'



Alright, I've been using c++ in DOS (i think) so I need some guidance here.

Thanks in advance.
Oct 5 '07 #1
11 3015
When you use the L literal before a string in your code, the compiler understands you are willing to create an Unicode string (wchar_t *) rather than an ANSI string (char *).

The function MessageBox, which is secretly defined as MessageBoxA inside the windows headers in this case, must receive ANSI strings as parameters, not Unicode strings. Thats why the compiler complains.

If you want your code to compile either remove the L literals to use ANSI strings or tell the compiler you are creating an Unicode Win32 application using

#define UNICODE 1
before including the windows.h header.

When defining such preprocessing variable, the windows header will secretly define the function MessageBox as MessageBoxW, such function receive Unicode strings as parameters.
Oct 5 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
#define UNICODE 1
before including the windows.h header.
You should not do this. To undefine it, you have to change your code. Instead there is a character set property setting for your project. Set that propertry to the correct value.

When defining such preprocessing variable, the windows header will secretly define the function MessageBox as MessageBoxW, such function receive Unicode strings as parameters.
There is no secret definition. MessageBox is a macro. Based on the character set, the macro resolves to MessageBoxA or MessageBoxW. Your code with the L in front of the literal will not work with MessageBoxA. Further, the L specifes a string of wchar_t and these characters are not Unicode characters unless they contain Unicode values.

Microsoft provides a series of mappings called the TCHAR mappings that you should be using. These macros correctly switch between ASCII and Unicode without your having to change your code.

You #include <tchar.h>

The message bis code should be:
Expand|Select|Wrap|Line Numbers
  1.  MessageBox(NULL,
  2.                TEXT("Hello World!"),
  3.                TEXT("Just another Hello World program!"),
  4.                MB_ICONEXCLAMATION | MB_OK);
  5.  
Read this. Be sure to follow the links.
Oct 5 '07 #3
I know its not a secret definition, just wanted to keep the explanation clear without going into the macro declaration details.
Oct 5 '07 #4
Arghh.


I still can't get it to work.


I reading an online DirectX tutorial, but nothing makes sense or works when I type it in.


WHY is DirectX sooo complicated ?!
Oct 5 '07 #5
oler1s
671 Expert 512MB
DirectX uses COM. If you aren’t familiar with the paradigms COM introduces, either because you simply haven’t seen COM in any form, or just don’t have that much experience, then DirectX will be overwhelming initially.
Oct 6 '07 #6
DirectX uses COM. If you aren’t familiar with the paradigms COM introduces, either because you simply haven’t seen COM in any form, or just don’t have that much experience, then DirectX will be overwhelming initially.

It's extremely overwhelming. And yeah, I haven't seen COM before.

How do I get more experience with COM ? Online tutorials ? Books ?
Oct 6 '07 #7
COM+ Programming: A Practical Guide Using Visual C++ and ATL
Pradeep Tapadiya
Publisher: Prentice Hall PTR

That book is the best COM book I have come across.
Yes, I know the tittle says "COM+", but the whole first part of the book is dedicated to explain the COM basis (what is it and why was designed the way it is).
Oct 6 '07 #8
COM+ Programming: A Practical Guide Using Visual C++ and ATL
Pradeep Tapadiya
Publisher: Prentice Hall PTR

That book is the best COM book I have come across.
Yes, I know the tittle says "COM+", but the whole first part of the book is dedicated to explain the COM basis (what is it and why was designed the way it is).


I see. Thanks a lot for your help.
Oct 6 '07 #9
I don't know if it's proper because I am fairly new to C++ as well as COM but the method I used was simply to remove the L (literal?) declaration.
So instead of:
MessageBox(NULL,
L"Hello World!",
L"Just another Hello World program!");
MB_ICONEXCLAMATION | MB_OK);
Just use:
MessageBox(NULL,
"Hello World!",
"Just another Hello World program!");
MB_ICONEXCLAMATION | MB_OK);
Once again, not sure if it's proper but it is functional.
Dec 22 '08 #10
As a side note I was having the same problem exactly until I read some of the replies here, so thank you all.
Dec 22 '08 #11
weaknessforcats
9,208 Expert Mod 8TB
No one so far on this thread has mentioned TCHAR.

TCHAR is a macro that switches betrween char and wchar_t based on the character set used by the compiler. When you set Visual Studio.NET to "Use Multi-byte character set", TCHAR is a char. When you set Visual Studio.NET to "Use Unicode character set", TCHAR is a wchar_t.

Using an L in front of a literal makes the literal a wchar_t but this does not support Unicode. Use the TEXT or _T macros.

MessageBox is itself a macro that swtiches between MessageBoxA and MessageBoxW.

I really suggest tou research TCHAR in MSDN before writing a lot fo code.
Dec 22 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michael Sgier | last post by:
Hi While trying to compile the follwoing example with devC++ I get errors: D:\SDL-1.2.7MingW\lib\libSDLmain.a(SDL_win32_main.o.b)(.text+0x34b) In function `console_main': undefined reference...
2
by: Michael Sgier | last post by:
Hi While trying to compile the follwoing example with devC++ I get errors: D:\SDL-1.2.7MingW\lib\libSDLmain.a(SDL_win32_main.o.b) (.text+0x34b) In function `console_main': undefined...
2
by: B_Love | last post by:
Hey! When trying to compile the code for a ordered vector class I get the following error: undefined reference to `WinMain@16' Anyone have any idea what I might be doing wrong? I've been...
1
by: Helen | last post by:
Hi I am trying to compile a package of avi to mpeg1 C source codes by Visual C++ 6.0. But I got the link error --------------------Configuration: AviMp1 - Win32 Debug--------------------...
1
by: Steve Richter | last post by:
I am working from the C++ .net step by step book ... my project compiles and runs as a console application: #include "stdafx.h" #using <mscorlib.dll> #using <System.dll> #using...
2
by: mahmoudlemeny | last post by:
Hi I was wondering if someone can help me with this. I am modifying a code that is an interface to a scanner. In the code there is a main function and when I try to debug it , it gives two errors: ...
12
by: eric dexter | last post by:
I want to compare what I get on the command line in winmain with an if statement.. I am getting a beginers error but I don't have an example to use to fix it.. cannot convert from 'const int' to...
10
by: Jrdman | last post by:
i wrote this code : #include<windows.h> int APIENTRY WinMain(HINSTANCE hinstane, HINSTANCE hpinstance, LPSTR lpcmdline, int cmdshow){ while(1) ;
7
by: jessy | last post by:
i have a date picker js file which i downloaded and used in my script ..the problem is that this script is able to write the picked date even in disabled fields i have a date field which is...
11
by: jessy | last post by:
Hi, I have a problem with my DateTimePicker javascript code which i downloaded , the problem is when i pick the date and the date appears in my Text Field and i click Submit the date which i picked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.