1

Years ago, I used to do some basic programming in C. Now I am attempting to relearn what I have forgotten as well as learn Visual C++. I am confused though by all the string options and now the extra layer of trying to make my programs Unicode compatible. I have been reading Beginning Visual C++ 2010 as well as online reading to learn this information.

As an exercise I am writing a very basic program that asks a user to input some text and then display that text in the form of a messagebox. The program works, but my way of getting it to work was more through guesswork and looking at other examples than truly understanding why I need to convert the various strings into different types.

The code is:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Windows.h"

using std::wcin;
using std::wcout;
using std::wstring;

int _tmain(int argc, _TCHAR* argv[])
{
    wstring myInput;

    wcout << "Enter a string: ";

    getline(wcin, myInput);

    MessageBoxW(NULL, myInput.c_str(), _T("Test MessageBox"), 64);

    return 0;
}

The MessageBox syntax is:

int WINAPI MessageBox(
  __in_opt  HWND hWnd,
  __in_opt  LPCTSTR lpText,
  __in_opt  LPCTSTR lpCaption,
  __in      UINT uType
);

On the other hand, if I just use the command line argument as the text of the messagebox, I do not need to convert the string at all and I am not sure why.

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Windows.h"

using std::wcout;

int _tmain(int argc, _TCHAR* argv[])
{

    MessageBoxW(NULL, argv[1], _T("Test MessageBox"), 64);

    return 0;
}

My confusion is:

  1. Why do I need to use the c_str() for argument 2 to MessageBoxW and why do I need to use the _T() macro (?) in argument 3?

  2. Why did the program work in the second code example without doing some sort of conversion?

  3. What exactly does LPCTSTR mean? I see another variant in MSDN functions called LPTSTR.

Thanks!

John Tangale
  • 325
  • 2
  • 17

2 Answers2

1

1) .c_str() is a standard C++ method to convert from C++ strings to C strings. _tmain, _T('x'), _T("text") and _TCHAR are (somewhat ugly) Microsoft macros that make your program compile either in unicode or non-unicode mode. There's a global setting in the project options that set some macros to configure your project in one of these two modes.

If you are in non-unicode mode (referred to as ANSI mode in MS's documentation) the macros expand to:

main, 'x', "text", char

If you are in unicode mode, the macros expand to

wmain, L'x', L"text", wchar_t

2) and 3) Windows headers are full of typedefs and macros like that. Sometimes they make code more obscure thant it needs to be. In general, LP means pointer (long pointer, i guess, but it's been a while since we needed to distinguish between near and far pointers), C means "const", T means that it will be either char or wchar_t depending on project settings and STR is obviously "string". After all, it's a plain C type, that's why you can pass C strings to them without conversion.

marcus
  • 5,041
  • 3
  • 30
  • 36
0
  1. The MessageBoxW function is expecting a C-style wide-character string (WCHAR ). The macro _L() alters your string so that it's Unicode compatible (WCHAR instead of char*).

  2. argv[] doesn't do objects, so you're already getting a WCHAR pointer out of it.

  3. LPCTSTR is basically a WINAPI typedef for const char * or const WCHAR*, depending on whether you are building as UNICODE. Also see this post: LPCSTR, LPCTSTR and LPTSTR

In short, your main function is being passed WCHAR* strings and MessageBoxW expects WCHAR* strings.

Community
  • 1
  • 1
Sanjamal
  • 346
  • 2
  • 6
  • But by creating a variable of wstring, is that already a wide string? Why would I then need to use the c_str() function? – John Tangale Dec 14 '11 at 02:30