提供两种方式:

1.Windows API

2.Windows _popen

// ADBHelper.cpp : This file contains the 'main' function. Program execution begins and ends there.
// #include "pch.h"
#include <iostream>
#include "windows.h"
#include <deque>
#include <string>
#include <thread> std::wstring StringToWString(const std::string& str);
std::string WStringToString(const std::wstring &wstr);
std::string ExeCmd(std::wstring CommandLine); int main()
{
for (int i = 0; i < 10; i++)
{
//std::string searchDevices = "adb devices";
//std::string out_content = ExeCmd(StringToWString(searchDevices));
//std::cout << out_content.c_str() << std::endl;
//Sleep(50); FILE *fp = NULL;
char debug_buf[1024] = { 0 };
if ((fp = _popen("adb devices", "r")) != NULL)
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);//hide console window
while (fgets(debug_buf, 255, fp) != NULL)
{ if (std::string(debug_buf) != "\n")
{
printf("read buffer = %s\n", debug_buf);
}
Sleep(50); }
_pclose(fp);
fp = NULL;
}
}
std::cout << "Hello World!\n";
} std::string ExeCmd(std::wstring CommandLine)
{
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
HANDLE /*g_hChildStd_IN_Rd,*/ g_hChildStd_OUT_Wr, g_hChildStd_OUT_Rd/*, g_hChildStd_IN_Wr*/; //pipe handles
char buf[1024]; //i/o buffer sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL; //if (CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &sa, 0)) //create stdin pipe
//{
if (CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0)) //create stdout pipe
{ //set startupinfo for the spawned process
/*The dwFlags member tells CreateProcess how to make the process.
STARTF_USESTDHANDLES: validates the hStd* members.
STARTF_USESHOWWINDOW: validates the wShowWindow member*/
GetStartupInfo(&si); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
//set the new handles for the child process
si.hStdOutput = g_hChildStd_OUT_Wr;
si.hStdError = g_hChildStd_OUT_Wr;
//si.hStdInput = g_hChildStd_IN_Rd; //spawn the child process
if (CreateProcess(NULL, (LPWSTR)CommandLine.c_str(), NULL, NULL, TRUE, NULL/*CREATE_NEW_CONSOLE*/,
NULL, NULL, &si, &pi))
{
unsigned long bread; //bytes read
unsigned long avail; //bytes available
memset(buf, 0, sizeof(buf)); for (;;)
{
PeekNamedPipe(g_hChildStd_OUT_Rd, buf, 1023, &bread, &avail, NULL);
//check to see if there is any data to read from stdout
if (bread != 0)
{
if (ReadFile(g_hChildStd_OUT_Rd, buf, 1023, &bread, NULL))
{
break;
} }
} //clean up all handles
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
//CloseHandle(g_hChildStd_IN_Rd);
CloseHandle(g_hChildStd_OUT_Wr);
CloseHandle(g_hChildStd_OUT_Rd);
//CloseHandle(g_hChildStd_IN_Wr);
return std::string(buf);
}
else
{
//CloseHandle(g_hChildStd_IN_Rd);
CloseHandle(g_hChildStd_OUT_Wr);
CloseHandle(g_hChildStd_OUT_Rd);
//CloseHandle(g_hChildStd_IN_Wr);
return std::string("create child process fail, error code: 2");
}
}
else
{
//CloseHandle(g_hChildStd_IN_Rd);
//CloseHandle(g_hChildStd_IN_Wr);
return std::string("create stdout pipe fail, error code: 1");
}
//}
//return std::string("create stdin pipe fail, error code: 0");
} std::wstring StringToWString(const std::string& str)
{
int num = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t *wide = new wchar_t[num];
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wide, num);
std::wstring w_str(wide);
delete[] wide;
return w_str;
} std::string WStringToString(const std::wstring &wstr)
{
std::string str;
int nLen = (int)wstr.length();
str.resize(nLen, ' ');
int nResult = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), nLen, (LPSTR)str.c_str(), nLen, NULL, NULL);
if (nResult == 0)
{
return "";
}
return str;
} // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

最新文章

  1. ubuntu系统虚拟机下共享文件夹
  2. Azure PowerShell (2) 修改Azure订阅名称
  3. 关于学习angularJS 的一些心得
  4. KnockoutJS 3.X API 第四章 数据绑定(4) 控制流with绑定
  5. CISA 信息系统审计知识点 [第二章. IT治理和管理 ]
  6. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)
  7. Web 应用性能提升 10 倍的 10 个建议
  8. 关于json-lib中日期类型转换的分析与问题解决
  9. 【融云分析】 IM 即时通讯之链路保活
  10. dijkstra(最短路)和Prim(最小生成树)下的堆优化
  11. ASP.NET MVC 自定义处理JSON ActionResult类
  12. 【剑指offer】字符串的排列
  13. 机器学习实战-ch2-有标签的聚类算法
  14. asp.net mvc源码分析-ModelValidatorProviders 客户端的验证
  15. linux下 redis 启动
  16. 用压测模拟并发、并发处理(synchronized,redis分布式锁)
  17. Linux安装jdk、删除Open jdk
  18. back propogation 的线代描述
  19. Windows+QT+Eclipse+MinGW搭建QT开发环境详细教程
  20. BZOJ 3632 外太空旅行(最大团)

热门文章

  1. B+树索引页大小是如何确定的?
  2. Windows环境中Hadoop配置
  3. redis学习之数据类型
  4. C++封装静态链接库和使用
  5. Python基础之字符串和编码
  6. Redis 14 发布订阅
  7. 快速掌握 Base 64 | 学 Java 密码系列
  8. [数据结构1.2-线性表] 动态数组ArrayList(.NET源码学习)
  9. 操作系统学习笔记5 | 用户级线程 &amp;&amp; 内核级线程
  10. SpringBoot集成Thymeleaf发送Html邮件报错