VC中如何为函数创建进程?
创建线程:做一个线程函数,将其设为主线程的一个线程。
我现在想创建一个进程,能不能像创建线程一样,做一个函数,然后将它变成进程呢?
需要用到什么函数?需要例子(核心代码+头文件),麻烦举个控制台下的例子
谢谢了~
(VC++6.0/控制台,WinXP)
[解决办法]
CreateProcess可以创建进程
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// 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 );
[解决办法]
CreateProcess可以创建进程
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// 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 );
[解决办法]
把函数的执行代码写好之后生成一个EXE,
你就可以CreateProcess的方式创建一个进程了
[解决办法]
http://blog.sina.com.cn/s/blog_4be9fda701000aa4.htmlcreateProcess函数十个参数的简介
http://blog.csdn.net/jtujtujtu/article/details/3495875
[解决办法]
我现在想创建一个进程,能不能像创建线程一样,做一个函数,然后将它变成进程呢?
============
不行,进程和线程的概念完全不同,进程是一个容器,你要创建进程必须先得有一个EXE文件。