0

I tried this in c++:

std::string teststring = "hello";
MessageBox(NULL,teststring,NULL, NULL);

error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'std::string' to 'LPCSTR'

qwerty
  • 2,065
  • 2
  • 28
  • 39
user10056
  • 63
  • 4
  • 12

3 Answers3

4

First, it looks like Visual C++ so tag it properly.

You can get the inside buffer using c_str() method on a std::string, so your code becomes:

std::string teststring = "hello";
MessageBox(NULL,teststring.c_str(),NULL, NULL);
Aamir
  • 14,882
  • 6
  • 45
  • 69
  • why visual C++? you could use any C++ compiler here. However answer is correct ) – Andrey Sep 19 '13 at 12:04
  • 1
    Yes, but normally in Windows environment when you are using a Windows API (MessageBox), most probably you are on Visual C++. That's why I said, 'it looks like' – Aamir Sep 19 '13 at 12:05
1

MessageBox's second and third parameter expect a C string.

To get a C string from a std::string you call c_str(), therefor the correct way to call it is:

std::string teststring = "hello";
MessageBox(NULL, teststring.c_str(), NULL, NULL);
user1233963
  • 1,450
  • 15
  • 41
0

what about try this?

std::string teststring = "hello";
LPCSTR tmp = teststring .c_str()
MessageBox(NULL,tmp ,NULL, NULL);
dhein
  • 6,431
  • 4
  • 42
  • 74