以下代码有效:
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;
}
为了自动清理,我使用了以下信息:如何将 std::string 转换为 const char* 或 char*?.
以下代码引发错误:
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
);
}
我收到此错误: “MessageBoxA”:无法将参数 2 从“std::vector<_Ty>”转换为“LPCSTR”