zoukankan      html  css  js  c++  java
  • VC常用代码之创建进程

    作者:朱金灿

    来源: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)
    ", 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;
    	}
    
  • 相关阅读:
    jQuery右键菜单contextMenu使用实例
    如何调动员工的积极性 -引用LTP.Net知识库
    Linux 目录管理的相关命令
    linux bash基础特性
    Linux 文件系统简介(FHS:Filesystem Hierarchy Standard)
    Linux 基础命令
    docker 限制容器能够使用的资源
    docker 私有registry harbor安装
    docker Dockerfile里使用的命令说明
    docker 存储卷 Volumes
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470021.html
Copyright © 2011-2022 走看看