Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by stringa
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


thanks for this, i didnt know this can be done

Share this post


Link to post
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
Linking...
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Exershio
try changing this

int APIENTRY WinMain(HINSTANCE hInstance,

to this

int 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 Exershio
try 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Reegan
Woohoo!
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. :S
Anyways 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Reegan
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.

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 this post


Link to post
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

Share this post


Link to post
Share on other sites
@Reegan:

Please start posting your error messages when your are posting. This would have made your original problem much easier to find, and so as your second. Thanks :)

I am glad that you have got your first problem working, however it would be helpful for us if you post your error messages so we can help you with the second problem.

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this