Winapi 创建进程时未找到错误文件
尝试使用Winapi 创建进程时未找到错误文件,winapi,visual-c++,createprocess,Winapi,Visual C++,Createprocess,尝试使用CreateProcess运行记事本: void _tmain( int argc, TCHAR *argv[] ) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
CreateProcess
运行记事本:
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( "notepad.exe", // No module name (use command line)
NULL, // 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() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
输出中有错误
2
。这意味着未找到错误\u文件
。我应该从哪里开始寻找问题?这是否意味着它找不到notepad.exe
?参数必须提供应用程序的完整路径,而不仅仅是其名称
更确切地说:
字符串可以指定要创建的模块的完整路径和文件名
执行,也可以指定部分名称。在部分
名称,函数使用当前驱动器和当前目录
完成规格说明。函数将不使用搜索路径。
此参数必须包含文件扩展名;无违约
扩展是假定的
但是你应该不在当前的notepad.exe
文件夹中。因此,如果只想使用名称,请在第二个参数中设置它,lpCommandLine
。请注意,此字符串不是常量,因为它是这样定义为LPTSTR
参数的:
BOOL WINAPI CreateProcess(_在_opt_uctstrlpapplicationname中,
_Inout_opt_LPTSTR lpCommandLine,[…] 因此,您有两种变体:
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
WCHAR CommandLine[] = L"notepad.exe";
if( CreateProcess( 0, // No module name (use command line)
CommandLine, // 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
)
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
或
lpApplicationName
参数必须提供应用程序的完整路径,而不仅仅是其名称
更确切地说:
字符串可以指定要创建的模块的完整路径和文件名
执行或它可以指定部分名称。如果是部分名称
名称,函数使用当前驱动器和当前目录
完成规范。函数将不使用搜索路径。
此参数必须包括文件扩展名;无默认值
扩展是假定的
但是您可能不在当前文件夹的notepad.exe
中。因此,如果您只想使用名称,请在第二个参数lpCommandLine
中设置它。请注意,此字符串不是常量,因为它被定义为LPTSTR
参数:
BOOL WINAPI CreateProcess(_在_opt_uctstrlpapplicationname中,
_Inout_opt_LPTSTR lpCommandLine,[…] 因此,您有两种变体:
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
WCHAR CommandLine[] = L"notepad.exe";
if( CreateProcess( 0, // No module name (use command line)
CommandLine, // 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
)
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
或
并非所有人都将Windows安装到
C:\Windows
,因此不应硬编码Windows文件夹的路径。请使用GetWindowsDirectory()
取而代之。或者,看看是否注册了记事本的完整路径。@RemyLebeau-当然你对记事本路径的看法是正确的。但我的目标是在CreateProcessW
中显示第一个和第二个参数的用法,这不是最终代码,但不是每个人都将Windows安装到C:\Windows
,所以你不应该这样做对Windows文件夹的路径进行硬编码。请改用GetWindowsDirectory()
。或者,查看记事本的完整路径是否已注册。@RemyLebeau-当然,关于记事本的路径,您是对的。但我的目标是在CreateProcessW
中显示第一个和第二个参数的用法,这不是最终代码,只是演示