我有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;
}
我该怎么做?
答案 0 :(得分:22)
答案 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::wstring
到std::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
(使用LPCSTR
,char*
的typedef std::u16string
或std::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)