0

The following code works:

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    char * writable = new char[myString.size() + 1];
    std::copy(myString.begin(), myString.end(), writable);
    writable[myString.size()] = '\0'; // don't forget the terminating 0 "delete[] writable;"

    int msgboxID = MessageBox(
        NULL,
        writable,
        "Notice",
        MB_OK
    );
    delete[] writable;
}

To clean up automatically I used info from : How to convert a std::string to const char* or char*?.

The following code throws an error:

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    std::vector<char> writable(myString.begin(), myString.end());
    writable.push_back('\0');

    int msgboxID = MessageBox(
        NULL,
        writable,
        "Notice",
        MB_OK
    );
}

I'm getting this error: 'MessageBoxA' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'LPCSTR'

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mickey D
  • 347
  • 2
  • 12

4 Answers4

4

You cannot pass a vector as LPCSTR, you must. Use:

&writable[0]

or:

writable.data()

instead. Or simply use myString.c_str()

marcinj
  • 48,511
  • 9
  • 79
  • 100
2

MessageBox takes a const char*. You don't need to copy the string first for that. Simply use c_str:

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        myString.c_str(),
        "Notice",
        MB_OK
    );
}

Note that I think your API is poor: you are modifying the value of the string passed in. Usually the caller wouldn't be expecting that. I think your function should look like this instead:

void CMyPlugin8::myMessageBox(const std::string& myString)
{
    std::string message = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        message.c_str(),
        "Notice",
        MB_OK
    );
}
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Tried the last option you posted above I get an error: | 'void CMyPlugin8::myMessageBox(const std::string &)' : overloaded member function not found in 'CMyPlugin8'| – Mickey D Feb 02 '17 at 17:25
  • You didn't fix the header file. The parameter is now `const` to ensure that it cannot be modified unintentionally. – Dark Falcon Feb 02 '17 at 17:27
  • Jackpot thanks! I thought i missed something there. That works – Mickey D Feb 02 '17 at 17:28
0
void CMyPlugin8::myMessageBox(const std::string& myString)
{
    std::string message = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        message.c_str(),
        "Notice",
        MB_OK
    );
}

Thanks everyone & @Falcon

Mickey D
  • 347
  • 2
  • 12
  • Actually modifying passed in variable is not good idea, I would recoment changing it to `const std::string& myString`, and inside function do `std::string tmp = "received ........`. You dont need vector here. Also if you ever will change to UNICODE build then you will find that all your char should be now wchar_t. – marcinj Feb 02 '17 at 17:28
0

If you still face a problem, after changing it myString.c_str(). Try this, Go to the Properties for your Project and under Configuration Properties/Advanced, change the Character Set to "Not Set". This way, the compiler will not assume that you want Unicode characters, which are selected by default:

enter image description here

Ranjeet R Patil
  • 453
  • 6
  • 10