Dos Console test...........(1)

11 篇文章 0 订阅
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <PROCESS.H>
#include <windows.h>
#include <fstream>
#include <SHELLAPI.H>

using namespace std;



#define   EXECDOSCMD   "ipconfig"   //可以换成你的命令 

VOID Thread_print (PVOID pvoid)
{
	while(1)
	{
		printf("Hello world\n\n");
		Sleep(1000);
	}
}


bool executePackage(LPCWSTR fileName, LPCWSTR args, LPCWSTR baseDir, bool wait)  
{  
    SHELLEXECUTEINFOW sei = { sizeof(SHELLEXECUTEINFOW) };  
	
    sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;  
	
    sei.lpFile = fileName;  
    sei.lpParameters = args;  
    sei.lpDirectory = baseDir;  
	
    if (!ShellExecuteExW(&sei)) {  
        return false;  
    }  
	
    if (wait) {  
        HANDLE hProcess = sei.hProcess;  
        if (hProcess != 0) {  
            WaitForSingleObject(hProcess, INFINITE);  
            CloseHandle(hProcess);  
        }  
    }  
	
    return true;  
}  


void EXEC ()
{ 
	SECURITY_ATTRIBUTES   sa; 
	HANDLE   hRead,hWrite; 
	
	sa.nLength   =   sizeof(SECURITY_ATTRIBUTES); 
	sa.lpSecurityDescriptor   =   NULL; 
	sa.bInheritHandle   =   TRUE; 
	if   (!CreatePipe(&hRead,&hWrite,&sa,0))   
	{ 
		return; 
	}   
	
	STARTUPINFO   si; 
	PROCESS_INFORMATION   pi;   
	si.cb   =   sizeof(STARTUPINFO); 
	GetStartupInfo(&si);   
	si.hStdError   =   hWrite; 
	si.hStdOutput  =   hWrite;
	si.hStdInput   =   hRead;
	si.wShowWindow =   SW_HIDE; 
	si.dwFlags   =   STARTF_USESHOWWINDOW   |   STARTF_USESTDHANDLES; 
	//关键步骤,CreateProcess函数参数意义请查阅MSDN 
	if   (!CreateProcess(NULL,   EXECDOSCMD 
		,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))   
	{ 
		return; 
	} 
	
	CloseHandle(hWrite); 
	
	char   buffer[4096]   =   {0}; 
	DWORD   bytesRead;   
	ofstream outfile("log.txt");

	_beginthread(Thread_print, 0, NULL);

	while   (true)   
	{ 
		if   (ReadFile(hRead,buffer,4095,&bytesRead,NULL)   ==   NULL) 
		{
			break; 
		}
		
		printf("printing.....\n");
		//buffer中就是执行的结果,可以保存到文本,也可以直接输出 
		//printf(buffer); 
		outfile << buffer;
		Sleep(200);   
	}

	outfile.close();

	return;
}



int main()
{
	 
	//EXEC();
	if(executePackage(LPCWSTR("mspaint"),LPCWSTR(""),LPCWSTR("C:\\Windows"),false))
	{
		printf("exec successfully...\n");
	}
	else
	{
		printf("exec failed...\n");
	}
	//system("ping www.baidu.com");

	//system("ping 127.0.0.1");
	// system("ipconfig");
	getch();
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值