Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:11:17+00:00 2026-05-27T08:11:17+00:00

Years ago, I used to do some basic programming in C. Now I am

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:11:18+00:00Added an answer on May 27, 2026 at 8:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

10 years ago I did some game programming and used OpenGL for the 3d
Couple of years ago i used some tool to monitor what event messages were
Some years ago I used to program in VB6, I really liked it because
In code I have developed some years ago I have been using this a
Good afternoon, I inherited some C# code from years ago. I have refactored it
I remember years ago being able to download and install the forums software used
Some years ago I was told about a study into code reuse. Apparently it
Many years ago I remember a fellow programmer counselling this: new Some::Class; # bad!
Four years ago I wrote a Sudoku puzzle solver, and now I'm trying to
We're using some C library in our Java project. Several years ago some other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.