# Win32 Data type conversions

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

## Recommended Posts

Okay, i dunno if this is a stupid question, but since I am in the beginner section, I'll give it a wizz. Here's my problem: I recently migrated from VB to c++ and now I'm busy learning the Win32 API, so i created a dialog where the user could enter a name of a window and it would close that window: char Bt[80]; HWND khWnd; case 1006: GetDlgItemText(hWnd, 1003, Bt, 80); // 1003 is the name of the TextBox :S khWnd = FindWindow(NULL, Bt); SendMessage(kHwnd, WM_CLOSE, NULL, NULL); okay, so everything that should happen up to there happens, heres my problem: SetDlgItemText(hWnd, 1004, khWnd) // 1004 is the Label that should display the Handle to the window The problem, param 3 won't accept type HWND, only LPCTSTR, so how do I convert HWND to LPCTSTR, i have tried (LPCTSTR)khWNd, (char)khWnd, hell i even tried a reinterpred_cast, what ever the hell that is, when i try the conversion, the program compiles but nothing appears on the label. Could someone please help Me Thank You.

##### Share on other sites
The third argument to SetDlgItemText is the text that should be copied to the control. "LPCTSTR" is Hungarian for "C-style string". You can't cast from a HWND to a string because that makes no sense.

##### Share on other sites
The third parameter is supposed to be LPCTSTR, which is a pointer to a Cstring that you want it to be set to, which is what your Bt is.

##### Share on other sites
umm... okay, so how do i get it working?

##### Share on other sites
SetDlgItemText(hWnd, 1004, khWnd) // 1004 is the Label that should

instead of khWnd (third parameter), you could have something like Bt or another char array.

char newText[] = "Label caption";
SetDlgItemText(hWnd, 1004, newText); // I think something like this should work.

##### Share on other sites
SetDlgItemText() is used to set the contents of a text box item. the first parameter is the handle to the window, the second is the ID of the control, and the third is the text you want to change it to. so by calling:

SetDlgItemText(hWnd, 1004, "Foobar");

The text box, identified by the ID 1004, will now contain the text "Foobar".

When you say "display the handle to the window", what exactly do you mean?

##### Share on other sites
Unlike VB, C++ (and most lower-level languages) do not let you convert data between different types implicitly; this is known as static typing compared to VB's dynamic typing. While in VB you can turn an integer into a string magically, statically typed languages don't permit this.

In C++, to convert types, you use an intermediate object. Converting to a string is best done with an ostringstream object*:

// Top of your file#include <sstream>using std::ostringstream;// Conversion point in codeostringstream ConvertStream;ConvertStream << khWnd;SetDlgItemText(hWnd, 1004, ConvertStream.str().c_str());

This code basically puts the value of khWnd into the stream (with the << operator) and then converts the stream into a C++ string. The string is obtained by calling str() on a stringstream object. The string in turn has a function c_str() which will return a value that is essentially the same as an LPCTSTR.

On the off chance that you're using MFC, the MFC style solution is this:
CString text;text.Format("%u", khWnd);SetDlgItemText(hWnd, 1004, (LPCTSTR)text);

MFC's CString class has a function Format() that lets you use a C printf() style syntax to construct a string; see the documentation on printf() for some details on what it can do. The class also has a special conversion function that returns an LPCTSTR - which, for reference, stands for Long Pointer to Constant Terminated STRing - meaning that the pointer is 32 bits, the data at the pointer will not be modified (Constant), and the string ends with a null terminator (a byte with a value of 0).

*Disclaimer: I'm used to working with Visual C++ 6.0, which has a less-than-standard implementation of the C++ standard classes. Some of the classes and header files I mention may be different in standards-compliant compilers.