gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:01:25 27 4
gpt4 key购买 nike

我有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com