2

Just jumped into c++ with win32 in Visual Studio 2012. I am experienced in C and C#, but my c++ is a little lacking. The win32 stuff just seems to take any hint of intuition and throw it into a volcano. So far it's been a pain.

Here is what I'm trying to do: 1) Extract text from a textbox "edit" control 2) Convert this text to an int 3) Form a string using a sprintf type formatter for a double-floating point number 4) Take the resulting string and display it in a different text box.

I've tried several different things I've found on the web, but they all come up short. This is the best I could do:

wchar_t buffer[30];
const wchar_t formatString[] = {'%','f','\0'}; //Yes I know this is awful, I don't know how to       convert a string literal into a wchar_t array.

GetWindowText(txtFixedPtToFloatInputHandle, &buffer[0], 15);

//Convert to signed integer
fixedPtValue = _wtoi(&buffer[0]);

//get a float
floatVal = 12.50;

//Use formatter to create a string representation
swprintf(buffer,  30, &formatString[0], floatVal);

SetWindowText(txtFixedPtToFloatOutputHandle, buffer);

This is the closest I have come. It's nasty I know, but all other things I've found on the web fell short (LPWSTR, boost::, stdio.h). In this code, all of the buffers get loaded with the correct strings! The problem is that my program closes/exits when the function returns! Any help??

user2913869
  • 481
  • 2
  • 5
  • 23
  • 2
    *"I don't know how to convert a string literal into a wchar_t array."* -- Put an `L` in front of it. -- `const wchar_t formatString[] = L"%f";` – Benjamin Lindley Oct 24 '13 at 02:56
  • Unrelated: Do you like to type =P ? You pass `buffer` to `SetWindowText` as most should, but pass `&buffer[0]` to functions like `GetWindowText`. Not any sort of earth-shattering error; its a style thing only. Both are perfectly valid, and if thats how you just do things so be it. I just found it a little strange it wasn't consistent. – WhozCraig Oct 24 '13 at 03:10
  • `The problem is that my program closes/exits when the function returns!` It should'n? It exits normally or with error? If with error, use debugger. If normally, I don't see any reason to post strings code. Do you have `main loop`? BTW, consider reading C++ book, you still writing in C. – Ivan Aksamentov - Drop Oct 24 '13 at 03:13
  • Use [StringCchPrintf](http://msdn.microsoft.com/en-us/library/windows/desktop/ms647541(v=vs.85).aspx) instead of swprintf(). Make sure you're building with UNICODE on so you're getting wide versions of Get/SetWindowText. What was your problem with LPWSTR? It's just a typedef. – i_am_jorf Oct 24 '13 at 03:41
  • @WhozCraig: Not that it's relevant here, but thought I'd mention that the two methods of passing the array technically mean different things (http://stackoverflow.com/a/4810668/218597). – icabod Oct 24 '13 at 08:55
  • @icabod were `&buffer` begin passed, I would concur. As typed parameters `buffer` and `&buffer[0]` reduce to a simple address of the same type:`wchar_t*`. They just get there in different ways. `&buffer` on the other hand, is definitely *not* the same; it is of type `wchar_t (*)[30]` and the only legal reduction without a cast to equivalence with `buffer` and `&buffer[0]` is through the only allowable intermediary: `void*`. Thus why passing all three to a target of a `memcpy` is legitimate, while only the first two reduce correctly to `wcscpy`. [See it live](http://ideone.com/p1ff6Y). – WhozCraig Oct 24 '13 at 09:24

1 Answers1

2

If you want to be able to build an ANSI version and an UNICODE version, you may use the Generic-Text Mappings in Tchar.h

#include <tchar.h>

_TCHAR EditText[ 32 ];
int cbCopied = GetWindowText( hWndInput, EditText, sizeof( EditText ) / sizeof( _TCHAR ) );
// Eventually use GetLastError if cbCopied == 0

// -1 because the snprint familly functions do not write a 0 if len == count, see docs
size_t cbMaxCarToOutput = ( sizeof( EditText ) / sizeof( _TCHAR ) ) - 1;
_sntprintf( EditText, cbMaxCarToOutput, _TEXT( "%f" ), floatVal );

SetWindowText( hWndInput, EditText );
manuell
  • 7,528
  • 5
  • 31
  • 58