-1

Developing in win32 context and trying to get the input from my dropdown as a string. strText is the correct value in type char[255]. I'm trying to convert this char array to a string, but it fails

char strText[13];
SendMessage(dropDown, CB_GETLBTEXT, dropDownSelection, reinterpret_cast<LPARAM>(strText));

std::string test(strText);  // outputs W
std::string test2("WORKS"); // outputs WORKS

thanks for any hints

** EDIT **

strText

strText 0x003beeec "M"  char[13]
77 'M'
0 '\0'
79 'O'
0 '\0'
78 'N'
0 '\0'
71 'G'
0 '\0'
79 'O'
0 '\0'
68 'D'
0 '\0'
66 'B'
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
abdoe
  • 359
  • 1
  • 4
  • 11
  • 3
    What is the contents of `strText` after `SendMessage(dropDown, CB_GETLBTEXT, dropDownSelection, reinterpret_cast(strText));`? – NathanOliver Jul 06 '16 at 17:29
  • @NathanOliver Same no? Because he is passing a copy and not a reference and function returns void? – Khalil Khalaf Jul 06 '16 at 17:30
  • @FirstStep Not sure. The array decays to a pointer so if they just fill the array it should have something. passing by value only applies if they try to change where it points to(not what). – NathanOliver Jul 06 '16 at 17:32
  • Even *if* reinterpret_cast was the right way to do this (which it most likely is not), what is the return value? You don't use it anywhere. Is this really the code you are using? – Jesper Juhl Jul 06 '16 at 17:32
  • I'll update my question – abdoe Jul 06 '16 at 17:34
  • @NathanOliver oh right right makes sense – Khalil Khalaf Jul 06 '16 at 17:34
  • 2
    Are you compiling with `UNICODE` defined? if so it's `WCHAR` and `std::wstring` because `SendMessage` is #defined as `SendMessageW` (This would likely only show you the first character since Windows uses UTF-16LE, so the API string looks like `W\0O\0R\0K\0S\0`) – theB Jul 06 '16 at 17:34
  • I wish to know why this is getting downvoted constantly? thought this here is for Q&A or is this question too dump? – abdoe Jul 06 '16 at 17:36
  • @abdoe the string constructor will turn a NULL-terminated C-string into a string, but since your second character is `\0` then its only using the first letter – BackDoorNoBaby Jul 06 '16 at 17:43
  • @abdoe The question's not dumb, it looks incomplete. Technically it is incomplete as it lacks an [mcve]. The problem is obvious after the edit and the cause very likely before, Thomas has it nailed in his answer below. – user4581301 Jul 06 '16 at 17:43
  • **−1** For selecting Really Ungood Advice™ as solution. This downvote is not like a punishment, but it's (unfortunately) about the only device available to make current readers and later googlers more likely aware of the **extremely low quality** of the selected solution. – Cheers and hth. - Alf Jul 06 '16 at 17:46
  • I didn't see your answer here, so it's hard to compare, but downvoting will help – abdoe Jul 06 '16 at 17:47
  • 1
    @Cheersandhth.-Alf: You have been around for a while and should know, how the voting system is supposed to work. You don't downvote a question because you disagree with the choice of the OP's accepted answer. To quote [yourself](http://stackoverflow.com/questions/38230134/c-char-array-not-correctly-converted-to-stdstring#comment63882497_38230230): *"What were you thinking of? Jeez."* – IInspectable Jul 06 '16 at 18:09
  • @IInspectable: I agree, mostly. I wish SO was more technical, and that I was not at the end of my day. Then I'd just write an answer. Maybe you can do it? Thx. – Cheers and hth. - Alf Jul 06 '16 at 18:12
  • OK, it's fixed now. – Cheers and hth. - Alf Jul 06 '16 at 18:14

1 Answers1

4

Most likely, your program is compiled in Unicode mode, so strText receives an UTF-16 encoded string. Since the W character fits in a single byte, the second byte will be 0 (little-endian encoding).

Don't use char when interfacing with the Windows API in Unicode mode. Use wchar_t and and std::wstring instead.

Or read and understand this question and its answers and make up your own mind on how you want to handle strings.

Read more about Unicode in the Windows API. And about Unicode in general.

Community
  • 1
  • 1
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • so I need to initialze the array with something like TCHAR arr[13]; then I guess?:) – abdoe Jul 06 '16 at 17:37
  • thanks it works! I would never found this to be the issue. thanks a lot. It's tought to get started here – abdoe Jul 06 '16 at 17:42
  • Yeah, the Windows API is not exactly the most forgiving. Neither is C++, for that matter. – Thomas Jul 06 '16 at 17:43
  • **−1** “Use TCHAR instead”. Please don't offer how-to-make-suicide-more-painful advice. What were you thinking of? Jeez. – Cheers and hth. - Alf Jul 06 '16 at 17:43
  • @Cheersandhth.-Alf Please explain more. I don't see why `TCHAR` is a bad idea, when wielded correctly (which is of course OP's responsibility). – Thomas Jul 06 '16 at 17:44
  • 1
    @Thomas: You can't wield that "correctly". It's a 16 years obsolete 10% solution to a 10 years obsolete problem (namely supporting Windows 9x to some degree), and it has only negative utility. That means that you don't know anything about what you're talking about. – Cheers and hth. - Alf Jul 06 '16 at 17:48
  • @Cheersandhth.-Alf OK then, let me google it for me: http://stackoverflow.com/questions/234365/is-tchar-still-relevant So would your advice be to use `wchar_t` instead? If so, feel free to edit that in. (And yeah, the last time I worked with the Windows API must have been around 10 years ago...) – Thomas Jul 06 '16 at 17:53
  • @Thomas: The "obsolete" in my comment referred to Layer for Unicode introduced in year 2000. Of course nobody uses it now. Well, except probably some company somewhere with a lot of legacy code and using an ages-old version of VS to make their code compile... :) – Cheers and hth. - Alf Jul 06 '16 at 17:59
  • I looked at the answer in your link and I liked [@dan04's answer](http://stackoverflow.com/a/3002494/464581), except the advice to use UTF-8 also for internal byte-based strings. That creates problems with e.g. file paths, because as a matter of in-practice the C++ runtime libraries in C++ passes byte strings directly down to the API, which assumes Windows ANSI by default (the other possibility is OEM). So this is a complex matter, but what everbody except the silly accepted solution there agrees on, is that `TCHAR` is Really Bad™. – Cheers and hth. - Alf Jul 06 '16 at 18:04
  • @Cheersandhth.-Alf Edited to the best of my abilities. I'll leave any further corrections to you. – Thomas Jul 06 '16 at 18:12