Forum Topic: c++ conversion trouble

(122 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

imdead597

Reply To Post Reply & Quote

Posted at: 10/27/09 10:32 PM

imdead597 DARK LEVEL 02

Sign-Up: 01/24/09

Posts: 64

Hi. I'm using c++ and i'm having trouble with this code

hWnd hWnd=CreateWindowEx(NULL, blah blah blah);
hWnd hComboBox=CreateWindow(blah blah blah);

//then, when I try to add a string to it, it turns out all weird.
SendMessage(hWnd,CB_ADDSTRING,0,(long)"b lah blah blah");

The result is a bunch of garbage. Some help please? BTW, i included all the necessary windows files.


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/27/09 10:59 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

why are you casting the string (a char* ) to a long?


None

CronoMan

Reply To Post Reply & Quote

Posted at: 10/28/09 07:32 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

At 10/27/09 10:59 PM, Glaiel-Gamer wrote: why are you casting the string (a char* ) to a long?

Because SendMessage takes two long parameters, wParam and lParam

Try using a unicode string (wchar_t, and std:wstring)

"no sound in ass"


None

robin1232

Reply To Post Reply & Quote

Posted at: 10/28/09 05:01 PM

robin1232 DARK LEVEL 15

Sign-Up: 01/19/08

Posts: 2,587

At 10/27/09 10:32 PM, imdead597 wrote: Hi. I'm using c++ and i'm having trouble with this code

hWnd hWnd=CreateWindowEx(NULL, blah blah blah);

a string with the name hWnd of type hWnd?
maybe change the name of the string

sorry if that isnt the problem, I have no experience in Windows programming


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/28/09 10:28 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

At 10/28/09 07:32 AM, CronoMan wrote:
At 10/27/09 10:59 PM, Glaiel-Gamer wrote: why are you casting the string (a char* ) to a long?
Because SendMessage takes two long parameters, wParam and lParam

Try using a unicode string (wchar_t, and std:wstring)

still doesnt make sense to me that you're casting a pointer type (char*) to an integer type (long)


None

DougyTheFreshmaker

Reply To Post Reply & Quote

Posted at: 10/29/09 04:01 AM

DougyTheFreshmaker NEUTRAL LEVEL 02

Sign-Up: 07/30/07

Posts: 519

At 10/28/09 10:28 PM, Glaiel-Gamer wrote: still doesnt make sense to me that you're casting a pointer type (char*) to an integer type (long)

I would guess that he had an error, and casting allowed compilation.

To the OP, look into the L and _T(...) style macros. For instance:

SendMessage(hWnd,CB_ADDSTRING,0, L"b lah blah blah"); // L not a typo

CronoMan's response is correct, I believe.

We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.
Freshmaking
Brainscrape

BBS Signature

None

CronoMan

Reply To Post Reply & Quote

Posted at: 10/29/09 09:22 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

At 10/28/09 10:28 PM, Glaiel-Gamer wrote: still doesnt make sense to me that you're casting a pointer type (char*) to an integer type (long)

A pointer is actually an integer, doesn't matter what type it points to. It could be double * and it would still be an integer ;)

SendMessage's two parameters, wParam and lParam are used for several things. They have chosen to use long, so that you can basically send any datatype that is less than 64bit. In this case, you cast a pointer to long, because the parameters are of long datatype, but in the other end (the receiving window) it will be cast back from long to wchar_t * or whatever datatype it uses.
C++ doesn't matter what is actually stored in the parameter, there is no bounds checking and no safe types. Therefore, you could for instance cast a double to long, and as long as the receiving application is aware that there should be a floating point there all will be well
Since C++ does not support run-time generics (C++ templates are compile-time) there is no proper way of sending different datatypes in the same function without using function overloading, which would require both ends to implement the same function on different datatypes, which basically is a waste of time for both the programmers and the application

And that's why you have to typecast! :P

"no sound in ass"


All times are Eastern Standard Time (GMT -5) | Current Time: 01:48 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!