VC常用代码之创建进程
2015-10-31 23:53
393 查看
作者:朱金灿
来源:http://blog.csdn.net/clever101
创建进程是编程开发的常用操作。Windows中的创建进程采用API函数CreateProcess实现。下面是一个使用例子:
#include <Windows.h>
#include <string>
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)\n", 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)\n", GetLastError() );
return 1;
}
来源:http://blog.csdn.net/clever101
创建进程是编程开发的常用操作。Windows中的创建进程采用API函数CreateProcess实现。下面是一个使用例子:
#include <Windows.h>
#include <string>
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)\n", 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)\n", GetLastError() );
return 1;
}
相关文章推荐
- VC常用代码之创建进程
- MyBatis入门(六)---mybatis与spring的整合
- [Django与表单]写一个简单的Django form表单处理
- Leap Motion C++环境的配置
- spring security使用数据库获取资源、角色和权限保护web应用
- 第四章:Django 的模板系统(转)
- spring security 下web应用安全的关键Filter:FilterSecurityInterceptor
- Python中文全攻略
- django 简易博客开发 1 安装、创建、配置、admin使用(转)
- eclipse javaweb 项目异常
- spring security 标准Filter及其在filter chain的顺序
- Spring MVC 异常处理
- eclipse新建项目出现两个红点
- 冒泡排序(与改进)
- VC++中文件类型小结
- python
- 疯狂JAVA讲义4.1--截取中文字符串
- Python 3 之 Chrom 截获的Headers转成dict格式
- hdoj4722Good Numbers【找规律】
- java代理模式