# Trouble with windows programming C++

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, im trying to figure out where im going wrong im following this tutorial online about windows programming. im using visual C++ 2008 express edition and here is the code that i have been asked to type.
#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR     lpCmdLine,
int       nCmdShow)
{

MessageBox(NULL, "\tHello World!", "My first windows app", NULL);
return 0;
}

Im assuming the code is supposed to go in an empty native console application but i could be wrong. It says something about the second paranthesis in MessageBox thing having an error. If someone could correct the error and direct me to a better place to learn about windows programming, i would be very greatful. Thanks ~Reegan PS: i hate windows programming.. looks so difficult an complicated compared to other types of programming.

##### Share on other sites
Having just gone through a windows programming class myself I believe I know the problem you are getting. If you surround the strings with _T it should compile and run. I forget the specific reason for this, it has something to do with a unicode/ascii compatibility or something of that nature. See below for what I was talking about:

MessageBox(NULL, _T("\tHello World!"), _T("My first windows app"), NULL);

I believe this should fix your problem. If it doesn't let me know.

##### Share on other sites
fortunately for you its just a simple sintax error where you pass text info to the messagebox method you wrote:

MessageBox(NULL, "\tHello World!", "My first windows app", NULL);

your passing text as char where it should be pass as LPCWSTR, so simple add a L before the coutes, it should look something like this

MessageBox(NULL, L"\tHello World!", L"My first windows app", NULL);

hope this helps you, please let me know if this works

##### Share on other sites
Under Project Properties, the "Character Set" option default is "UNICODE", you can change that to either "Not Set" or "MultiByte", then you can use your strings as normal.

I'm not saying one way is better than the other. I'm just saying it's possible

##### Share on other sites
Quote:
 Original post by stringaUnder Project Properties, the "Character Set" option default is "UNICODE", you can change that to either "Not Set" or "MultiByte", then you can use your strings as normal.I'm not saying one way is better than the other. I'm just saying it's possible

thanks for this, i didnt know this can be done

##### Share on other sites
Thanks for trying but neither of those worked they both returned the error "identifer not found".

any other suggestions? *hopeful*

##### Share on other sites
try changing this

int APIENTRY WinMain(HINSTANCE hInstance,

to this

int WINAPI WinMain(HINSTANCE hInstance,

That should fix the problem.

##### Share on other sites
i dont know whats wrong, it is like the f of a float(0.0f), it worked in my pc( Windows XP, Visual Studio 2008 Professional,C++), here is my complete code with character set set to Unicode:

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

MessageBox(NULL, L"\tHello World!", L"My first windows app", NULL);
return 0;
}

and my output:

------ Build started: Project: Help, Configuration: Debug Win32 ------
Compiling...
main.cpp
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\GameDev Help\Help\Help\Debug\BuildLog.htm"
Help - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

##### Share on other sites
Still no luck, ill post the origional code and the error message i got at first

Code:
#include <windows.h>int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow){ 		MessageBox(NULL, "\tHello World!", "My first windows app", NULL);	return 0;}

Error messgage:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

##### Share on other sites
try getting rid of \t in the second parameter. Why do you have it there anyway?

##### Share on other sites
Quote:
 Original post by Exershiotry changing thisint APIENTRY WinMain(HINSTANCE hInstance,to thisint WINAPI WinMain(HINSTANCE hInstance,That should fix the problem.

it is the same

int APIENTRY WinMain(HINSTANCE hInstance,

than

int WINAPI WinMain(HINSTANCE hInstance,

APIENTRY is just

#define APIENTRY WINAPI

Quote:
 Original post by Exershiotry getting rid of \t in the second parameter. Why do you have it there anyway?

\t is a parameter to center the text into the box

[Edited by - fladur on March 22, 2008 9:32:22 PM]

##### Share on other sites
One thing you'll notice in Windows API is that the programmers of Windows apparently like to use typedefs; For example, LPCWSTR is an acronym for "long pointer c-style wide string", a wchar_t*; or "a pointer to a c-style string with wide traits". HINSTNACE is a handle (a pointer of sorts) to an instance, and <t>LPSTR, as you can guess is your classic char* string.

Make sure you know what they are :)

##### Share on other sites
Woohoo!
Thanks to fladur it now works!
it wasnt the code that was wrong with his first post it was the fact i was using a empty Win32 console application rather than a Win32 project. i dont know why this makes a difference because they are both empty to begin with. :S
Anyways thanks everyone for helping me out

~Reegan

EDIT: slow computer causes many typos =[

##### Share on other sites
Quote:
 Original post by ReeganWoohoo!Thanks to fladur it now works!it was he code that was wrong with his first post it was the act i was using a empty Win32 console application rather than a Win32 project. i dont know why this makes a difference because they are both empty to begin with. :SAnyways thanks everyone for helping me out~Reegan

great to hear that, c++ is a complex language, there is more than code, there are configurations for Console, for Window, for a lib, etc, so be sure to select the correct type

##### Share on other sites
Ok now i have that worked out i moved onto the next tutorial and tried the code there, it had plenty of errors and i dont know where to begin.. is there anywhere better i could learn about windows programming.. some website that applies to my IDE.

##### Share on other sites

the beginners Bible to the Win32 API

very, very simple steps to follow.

m0ng00se

##### Share on other sites
Quote:
 Original post by ReeganOk now i have that worked out i moved onto the next tutorial and tried the code there, it had plenty of errors and i dont know where to begin.. is there anywhere better i could learn about windows programming.. some website that applies to my IDE.

It shouldn't matter what IDE you use - the Windows API doesn't change.

Could you post some of the errors you're getting? Perhaps we can help.

##### Share on other sites
I recomend that u read this tutorial http://www.cplusplus.com/doc/tutorial/ before getting into specific win32 then you try this http://www.winprog.org/tutorial/ hope this helps you

@Reegan:

##### Share on other sites

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628682
• Total Posts
2984205

• 13
• 13
• 9
• 10
• 10