博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC常用代码之创建进程
阅读量:2429 次
发布时间:2019-05-10

本文共 1858 字,大约阅读时间需要 6 分钟。

作者:朱金灿

来源:

 

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

#include 
#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)\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://cbimb.baihongyu.com/

你可能感兴趣的文章
他修过车、杀过鱼,最终进入阿里巴巴打造 9 个本地版支付宝!
查看>>
百面机器学习!算法工程师面试宝典!| 码书
查看>>
苹果无人驾驶拿 124 个工程师祭天!
查看>>
漫画 | 一个前端渣渣的成功逆袭
查看>>
与吴恩达并肩战斗,她是 AI 界的女超人!|人物志
查看>>
微信手机 WeOS 的可行性到底有多大?
查看>>
阿里面试,我挂在了第四轮……
查看>>
C++ 程序员到高级架构师,必须经历的三个阶段
查看>>
和 Java、C# 等语言对比后,Python 简直酷上天了!
查看>>
程序媛到最后,拼的到底是什么?
查看>>
笑死!996 程序员竟然做了这个梦!| 每日趣闻
查看>>
“再见,微软!”
查看>>
ARM 发布新一代 CPU 和 GPU,实现 20% 性能提升!
查看>>
技术引路:机器学习仍大有可为,但方向在哪里?
查看>>
漫画:如何给女朋友解释什么是编译与反编译
查看>>
刷屏了!这篇 Python 学习贴,90% 的程序员都用的上!
查看>>
漫画:如何给女朋友解释什么是适配器模式?
查看>>
程序员又迎来一个好消息! | 每日趣闻
查看>>
Mac 被曝存在恶意漏洞:黑客可随意调动摄像头,波及四百万用户!
查看>>
拒绝与其他码农一致!CSDN定制T让你成为最靓的仔
查看>>