我有一段代碼中,我需要使用與:: ifstream的打開和CreateProcess的兩個字符串,像使用與ifstream的寬字符串::打開或字節字符串與CreateProcess的
//in another file
const char* FILENAME = "C:\\...blah blah\\filename.bat";
// in main app
std::ifstream is;
is.open(FILENAME);
// ...do some writing
is.close();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
std::string cmdLine = "/c " + FILENAME;
if(!CreateProcess("c:\\Windows\\system32\\cmd.exe",
cmdLine.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
return GetLastError();
}
CreateProcess需要一個LPCWSTR,所以要在CreateProcess中使用該字符串我需要聲明文件名和'cmdLine'爲std :: wstring,但ifstream :: open不需要寬字符串......我可以'想想辦法解決這個問題。我似乎總是遇到unicode與多字節字符串的問題。
任何想法? 謝謝。
您可以使用例如['mbstowcs'](http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs)從'char *'轉換爲'wchar_t *'。 – 2012-04-19 12:08:18