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

时间:2014-03-23 00:03:40

标签: 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;
    }

我该怎么做?

4 个答案:

答案 0 :(得分:22)

只需使用c_str的{​​{1}}功能。

见这里:

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

std::w/string

答案 1 :(得分:9)

LPCTSTR是一个古老的遗物。它是一个混合类型定义,如果您使用多字节字符串,则定义char*;如果使用Unicode,则定义wchar_t*。在Visual Studio中,可以在&#34;字符集&#34;下的一般项目设置中更改。

如果您使用的是Unicode,那么:

std::wstring somePath(L"....\\bin\\javaw.exe");
LPCTSTR str = somePath.c_str();                 // i.e. std::wstring to wchar_t*

如果您使用的是多字节,请使用此帮助程序:

// wide char to multi byte:
std::string ws2s(const std::wstring& wstr)
{
    int size_needed = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), 0, 0, 0, 0); 
    std::string strTo(size_needed, 0);
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), &strTo[0], size_needed, 0, 0); 
    return strTo;
}

即。 std::wstringstd::string将包含多字节字符串,然后再包含char*

LPCTSTR str = ws2s(somePath).c_str();

答案 2 :(得分:0)

最后决定使用CreateProcessW作为paulm提到一些修正 - 需要输入值(否则我会收到错误):

STARTUPINFOW si;
    memset(&si, 0, sizeof (STARTUPINFOW));
    si.cb = sizeof (STARTUPINFOW);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = FALSE;

    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof (PROCESS_INFORMATION));

    std::wstring cmdline(L" -jar install.jar");

    if (!CreateProcessW((LPCWSTR)strKeyValue.c_str(),
            (LPWSTR)cmdline.c_str(), // 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;
    }

答案 3 :(得分:0)

从stdlib类与TCHAR交互时最安全的方法是使用std::basic_string<TCHAR>并使用TEXT()宏包围原始字符串(因为TCHAR可以是窄而宽的取决于项目设置。)

std::basic_string<TCHAR> somePath(TEXT("....\\bin\\javaw.exe"));

由于你没有赢得风格竞赛这样做......另一种正确的方法是明确使用WinAPI函数的窄版或宽版。例如。在那个特例中:

  • std::string使用CreateProcessA(使用LPCSTRchar*的typedef
  • 使用std::u16stringstd::wstring使用CreateProcessW(使用LPCWSTR这是wchar_t*的typedef,在Windows中为16位)

在C ++ 17中,您可以这样做:

std::filesystem::path app = "my/path/myprogram.exe";
std::string commandcall = app.filename.string() + " -myAwesomeParams";
// define si, pi
CreateProcessA(
    const_cast<LPCSTR>(app.string().c_str()),
    const_cast<LPSTR>(commandcall.c_str()),
    nullptr, nullptr, false, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr,
    &si, &pi)