设为首页 加入收藏

TOP

VC常用代码之创建进程
2016-01-29 17:26:19 】 浏览:1044
Tags:常用 代码 创建 进程

创建进程是编程开发的常用操作。Windows中的创建进程采用API函数CreateProcess实现。下面是一个使用例子:

#include <windows.h>
#include 
  
   

int _tmain(int argc, _TCHAR* argv[])
{

	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	std::string strCmdLine = ping www.baidu.com;
 
	// Start the child process. 
	if( !CreateProcess( NULL,   // No module name (use command line)
		(LPSTR)strCmdLine.c_str(),        // 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)
, GetLastError() );
		return 1;
	}

	// Wait until child process exits.
	WaitForSingleObject( pi.hProcess, INFINITE );

	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );

   getchar();
   return 0;
}

  

使用上面的方式创建进程会出现一个控制台界面。要隐藏这个控制台界面,只需要将CreateProcess函数的第六个参数设为CREATE_NO_WINDOW,比如上面对应的代码应改为:

	if( !CreateProcess( NULL,   // No module name (use command line)
		(LPSTR)strCmdLine.c_str(),        // Command line
		NULL,           // Process handle not inheritable
		NULL,           // Thread handle not inheritable
		FALSE,          // Set handle inheritance to FALSE
		CREATE_NO_WINDOW,              // 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)
, GetLastError() );
		return 1;
	}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VS2015--win32工程配置的一些想法.. 下一篇VC获取并修改计算机屏幕分辨率

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目