设为首页 加入收藏

TOP

VC CreateProcess详解(一)
2014-11-23 17:44:53 】 浏览:250
Tags:CreateProcess 详解

CreatProcess(LPCSTR lpApplicationName,LPSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcesInformation);

lpApplicationName是应用程序的名称
lpCommandLine是命令行参数
lpProcessAttributes是进程的属性
lpThreadAttributes是线程的属性
bInheritHandles 是否继承父进程的属性
dwCreationFlags 是创建标志
lpEnvironment 是环境变量
lpCurrentDirectory 是当前目录
lpStartupInfo是传给新进程的信息
lpProcessInformation是进程返回的信息

调用函数的例子如下:
//创建进程
void TestCreateProcess(void)
{
//清空结构
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESHOWWINDOW;
sInfo.wShowWindow=SW_SHOWNORMAL;

ZeroMemory(&pInfo,sizeof(pInfo));

//创建一个进程
if(!::CreateProcess(_T("WinCpp.exe"),NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&sInfo,
&pInfo))
{
//输出出错信息
const int nBufSize=512;
TCHAR chBuf[nBufSize];
ZeroMemory(chBuf,nBufSize);

wsprintf(chBuf,_T("CreateProcess failed(%d).\n"),GetLastError());
::OutputDebugString(chBuf);
return;
}

//等进程关闭
WaitForSingleObject(pInof.hProcess,INFINITE);

//关闭进程和线程的句柄
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
}

我们用CreateProcess执行一个外部程序时,怎样才能得到这个程序的输入输出呢?

CreateProcess已经替我们准备好了,在CreateProcess的STARTUPINFO参数里有这样几个

hStdInput,hStdOutput,hStdError东东,用来为创建的进程指定输入输出,例如用

CreateFile创建一个文件,接着把得到的文件句柄指定给hStdOutput,并且把dwFlags的值

设置为USESTDHANDLES,这样外部程序的输出就会输到这个文件里。注意,CreateFile的

SECURITY_ATTRIBUTES.bInheritHandle参数要设为TRUE。

在Create系列函数中通常都会有一个叫SECURITY_ATTRIBUTES的参数
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor=NULL;
sa.bInheritHandle=TRUE;
如果把bInheritHandle的值设为TRUE,意思就是它所创建出来的东西是可以被其他的子进程

使用的。例如用CreatePipe创建的管道可以用在CreateProcess创建的进程中。

用CreateProcess创建子进程时通过lpCurrentDirectory参数指定子进程运行的路径。

#include <windows.h>

#include

#include

void _tmain( int argc, TCHAR *argv[] )

{

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 );

}

利用Cr

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇 VC++6.0调试工具使用初步 下一篇在批处理中使用ping命令,一个小..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目