提供两种方式:

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

windows C++ call ADB command的更多相关文章

  1. 解决Mac上adb: command not found问题

    使用mac进行开发的时候,有时候需要使用adb指令来进行一些操作,但是如果没有配置过Android环境变量,可能会出现adb: command not found的问题,查了一些资料,这里记录一下ma ...

  2. Windows bat with adb

    /********************************************************************* * Windows bat with adb * 说明: ...

  3. How to install IIS 7.5 on Windows 7 using the Command Line

    原文 How to install IIS 7.5 on Windows 7 using the Command Line On Windows Vista, to install IIS 7.0 f ...

  4. 解决adb command not found以及sdk环境配置

    解决adb command not found以及sdk环境配置 分类: mark 2013-10-02 09:41 2975人阅读 评论(0) 收藏 举报 原文地址:http://www.cnblo ...

  5. mac上adb command not found

    第一种报错(使用的自带mac命令行) bash: adb: command not found 1.vim ~/.bash_profile ,如果.bash_profile不存在,先touch ~/. ...

  6. ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137

    错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...

  7. 在Windows下使用adb logcat grep

    在Windows下使用adb logcat  grep 会提示 因为grep 为Linux命令,所以不能使用.怎么办呢? 这时候可以用到babun 下载地址:http://babun.github.i ...

  8. 如何通过adb command 完成自动SD卡升级?

    如何通过adb command 完成自动SD卡升级? 原创 2014年09月09日 10:50:57 2746 通过adb 命令的方式,免去了按powerkey+volumeup进入menu sele ...

  9. 解决mac中adb: command not found

    在Mac系统中,很多时候第一次在Android SDK中使用adb的时候.无法使用.会提示-bash: abd: command not found. 造成此类现象的原因是:未配置Android的环境 ...

随机推荐

  1. python常用功能模块

    路径相关:os.pathlib Windows注册表相关:winreg 系统cpu.内存.线程相关:psutil 文件.文件夹处理:shutil 解析和生成ini文件:ConfigParser:(co ...

  2. B+树索引页大小是如何确定的?

    B+树简介 在正式介绍本文的主题前,需要对 B+ 树有一定的了解,B+树是一种磁盘上数据的索引结构,大概长这个样子. B+树的叶子节点是所有的数据,非叶子节点称为索引页,索引页里有若干个索引项,本例中 ...

  3. 云存储?不依赖三方服务自己也可以搞,利用Docker来搭建分布式文件系统FastDfs

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_78 对于文件存储来说,一般情况下简单的处理就是在Django配置文件中配置存储目录,按照规则对文件进行上传或者下载. 实际上,当文 ...

  4. Vue 监视数据总结 && 表单控件使用总结

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...

  5. Vue 计算属性 && 监视属性

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...

  6. MySQL 常见索引类型介绍

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL 主要索引类型有如下几种: 1.主键索引 2.唯一索引 3.普通索引 4.空间索引 5.全文索引 假设有如下一张 ...

  7. RGBColor类定义

    这个类主要是颜色操作,操作详细原理如下图: 类声明: class RGBColor { public: RGBColor(); ~RGBColor(); RGBColor(ldouble a); RG ...

  8. 在Go中如何正确重试请求

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/677 我们平时在开发中肯定避不开的一个问题是如何在不可靠的网络服务中 ...

  9. 常见docker命令(二)-容器生命周期相关

    docker run 命令主要参数-d 后台运行,返回容器id-i 以交互模式运行,通常与-t连用-t 为容器重新分配一个伪输入终端,通常与-i连用-P(大写) 随机端口映射,容器内部端口随机映射到主 ...

  10. 使用three.js(webgl)搭建智慧楼宇、设备检测、数字孪生——第十三课

    老子云:有道无术,术尚可求,有术无道,止于术. 咱开篇引用老子的话术,也没其它意思,只是最近学习中忽有感悟,索性就写了上来. 这句话用现代辩证思维理解,这里的"道" 大抵是指方法论 ...