调用C函数获取电脑信息及执行cmd命令时隐藏窗口

GetPCInfo.h

#include  
#include 
using namespace std;

//
wstring Str2Wstr(string str);
string ExecCmd(const char *cmd);							// 执行 cmd 命令
void GetResultFromCmd(string & str);						// 得到结果
//

void getNetworkInfo(char *local_ip, char *local_mac);		// 获取IP、MAC

string GetPCInfo();		// 获取电脑相关信息

string GetCurrentDir();

//
// 获取两个日期的时间差
time_t convert(int year,int month,int day);
int get_days(const char* from, const char* to);
//

GetPCInfo.cpp

#include  
#include 
#include  // include must before window.h
#include 
#include   
#include   
#include   
 
#pragma comment(lib, "iphlpapi.lib")

using namespace std;

// ---- get harddisk info ---- //
wstring Str2Wstr(string str)
{
	unsigned len = str.size() * 2;// 预留字节数
	setlocale(LC_CTYPE, "");     //必须调用此函数
	wchar_t *p = new wchar_t[len];// 申请一段内存存放转换后的字符串
	mbstowcs(p, str.c_str(), len);// 转换
	std::wstring str1(p);
	delete[] p;// 释放申请的内存
	return str1;
}

string ExecCmd(string pszCmd)
{
	wstring pszCmd_w = Str2Wstr(pszCmd);
	wcout << "pszCmd_w is " << pszCmd_w << endl;

	// 创建匿名管道,write->read;
	SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
	HANDLE hRead, hWrite;
	if (!CreatePipe(&hRead, &hWrite, &sa, 0))
	{
		cout << "@ CreatePipe failed!" << endl;
		return (" ");
	}
	cout << "@0" << endl;

	// 设置命令行进程启动信息(以隐藏方式启动命令并定位其输出到hWrite
	STARTUPINFO si = { sizeof(STARTUPINFO) }; // Pointer to STARTUPINFO structure;
	GetStartupInfo(&si);
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
	//si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_HIDE; //隐藏窗口;
	si.hStdError = hWrite;
	si.hStdError = hWrite;
	si.hStdOutput = hWrite; //管道的输入端口连接命令行的输出;

	// 启动命令行
	PROCESS_INFORMATION pi;// Pointer to PROCESS_INFORMATIO

你可能感兴趣的:(C/C++,c语言,c++,开发语言)