我正在尝试从我的.cpp文件中打开一个应用程序。我做了一些研究,发现使用CreateProcess()是最好的选择。这样做会产生以下代码:
//Below has the purpose of starting up the server: -----------------------------------------------
LPWSTR command = (LPWSTR)"C:\\Users\\CCRT\\Documents\\UPDATED\\FINALSERVER\\FINALSERVER\\Debug\\FINALSERVER.exe";
// Start the child process.
LPSTARTUPINFOA si;
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
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 (%d).\n", GetLastError());
}
else {
std::cout << "Command success\n";
}但是,当我试图构建解决方案时,会出现以下错误:
cannot convert argument 9 from 'LPSTARTUPINFOA *' to 'LPSTARTUPINFOW' 错误发生在CreateProcess()函数上。
我想知道是否有人能向我解释这个错误,并告诉我如何纠正它,因为我非常困惑到底是怎么回事。
发布于 2022-09-21 19:37:49
无法将参数9从“LPSTARTUPINFOA*”转换为“LPSTARTUPINFOW”
您为函数提供了一个指向STARTUPINFOA 指针的指针,但该函数需要一个STARTUPINFOW指针(这就是LPSTARTUPINFOW所包含的STARTUPINFOW指针)。
因此,正确的方法是像这样定义si:
STARTUPINFOW si{sizeof(STARTUPINFOW)}; // not LPSTARTUPINFOWsizeof(STARTUPINFOW)部件将si的cb成员设置为结构的大小。
另外:LPWSTR command = (LPWSTR)"C:\\U...是错的。它应该是
wchar_t command[] = L"C:\\U...";
// ^L使字符串文本为const wchar_t[N] (其中N是字符串+1的长度),但由于command可能被CreateProcessW更改,所以需要将其放入可变的wchar_t数组中。
这也是最好的一致。显式地使用"wide“函数,比如CreateProcessW,除非您也计划为Ansi构建,然后保持一致性,使用STARTUPINFO (没有A或W),并使用TEXT宏来定义字符串。
https://stackoverflow.com/questions/73806073
复制相似问题