c++ - 如何在 C++ 中将 std::wstring 转换为 LPCTSTR?

标签 c++ string type-conversion wstring

我有 wstring 格式的 Windows 注册表项值。现在我想将它传递给这段代码(第一个参数 - javaw.exe 的路径):

std::wstring somePath(L"....\\bin\\javaw.exe");

    if (!CreateProcess("C:\\Program Files\\Java\\jre7\\bin\\javaw.exe", <--- here should be LPCTSTR, but I have a somePath in wstring format..
            cmdline, // Command line.
            NULL, // Process handle not inheritable.
            NULL, // Thread handle not inheritable.
            0, // Set handle inheritance to FALSE.
            CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
            NULL, // Use parent's environment block.
            NULL, // Use parent's starting directory.
            &si, // Pointer to STARTUPINFO structure.
            &pi)) // Pointer to PROCESS_INFORMATION structure.
    {
        printf("CreateProcess failed\n");
        return 0;
    }

我该怎么做?

最佳答案

只需使用std::w/stringc_str函数即可。

看这里:

http://www.cplusplus.com/reference/string/string/c_str/

std::wstring somePath(L"....\\bin\\javaw.exe");

    if (!CreateProcess(somePath.c_str(),
            cmdline, // Command line.
            NULL, // Process handle not inheritable.
            NULL, // Thread handle not inheritable.
            0, // Set handle inheritance to FALSE.
            CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
            NULL, // Use parent's environment block.
            NULL, // Use parent's starting directory.
            &si, // Pointer to STARTUPINFO structure.
            &pi)) // Pointer to PROCESS_INFORMATION structure.
    {
        printf("CreateProcess failed\n");
        return 0;
    }

关于c++ - 如何在 C++ 中将 std::wstring 转换为 LPCTSTR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22585326/

相关文章:

c++ - 将特定字符移动到字符串末尾的递归 C++ 函数

c++ - 我应该将 Windows 8.1 SDK 还是 Windows 10 SDK 与 Visual Studio 2017 一起用于标准 C++?

c++ - 如何正确读取 DirectX 的 FBX 2014 索引?

c++ - MongoDB C Driver Windows 无法解析的外部符号

php - 在 PHP 中从数据库中删除 TinyMCE 格式

python - Python 中 float 到分数的转换

linux - 如何在 sed 中将方括号内的字母和数字替换为相同的数字和括号外的字母?

java - 从文件中读取 JSON 数据以在 StringEntity 中使用

将 volatile 数组转换为非 volatile 数组

java - 将 Java int[][] 的神秘 bytea 表示形式转换回数组(Hibernate)