我正在尝试弄清楚如何使用 CreateProcess() 函数,而且我对 C++ 不太精通。我尝试了一些方法来尝试消除错误,但应用程序似乎没有按照我期望的那样执行此后的操作。
我想要做的是将“cmd.exe /c ipconfig > C: est.txt”传递给它并让它按预期执行。
我遇到的错误(在 dev C++ 中)是:
6 C:\Dev-Cpp\project est\Untitled1.cpp
int'main' must return
C:\Dev-Cpp\project est\Untitled1.cpp 在函数 `int main(...)' 中:
28 C:\Dev-Cpp\project est\Untitled1.cpp 返回语句没有值,在返回 'int' 的函数中
这是我正在使用的代码(取自微软的示例):
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
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( NULL, // No module name (use command line)
argv[1], // 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 );
}
改变
void _tmain( int argc, TCHAR *argv[] )
到
int _tmain( int argc, TCHAR *argv[] )
并在程序中返回退出代码,即更改
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
到
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}