windows C++ call ADB command
提供两种方式:
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的更多相关文章
- 解决Mac上adb: command not found问题
使用mac进行开发的时候,有时候需要使用adb指令来进行一些操作,但是如果没有配置过Android环境变量,可能会出现adb: command not found的问题,查了一些资料,这里记录一下ma ...
- Windows bat with adb
/********************************************************************* * Windows bat with adb * 说明: ...
- 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 ...
- 解决adb command not found以及sdk环境配置
解决adb command not found以及sdk环境配置 分类: mark 2013-10-02 09:41 2975人阅读 评论(0) 收藏 举报 原文地址:http://www.cnblo ...
- mac上adb command not found
第一种报错(使用的自带mac命令行) bash: adb: command not found 1.vim ~/.bash_profile ,如果.bash_profile不存在,先touch ~/. ...
- 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 ...
- 在Windows下使用adb logcat grep
在Windows下使用adb logcat grep 会提示 因为grep 为Linux命令,所以不能使用.怎么办呢? 这时候可以用到babun 下载地址:http://babun.github.i ...
- 如何通过adb command 完成自动SD卡升级?
如何通过adb command 完成自动SD卡升级? 原创 2014年09月09日 10:50:57 2746 通过adb 命令的方式,免去了按powerkey+volumeup进入menu sele ...
- 解决mac中adb: command not found
在Mac系统中,很多时候第一次在Android SDK中使用adb的时候.无法使用.会提示-bash: abd: command not found. 造成此类现象的原因是:未配置Android的环境 ...
随机推荐
- websocket、socket、http对比
简介 在之前的理解中,讲述了socket.websocket等相关的理解,本文就socket.websocket.http理解一下其对应的联系和区别. HTTP 协议 http 为短连接:客户端发送请 ...
- 2521-Shiro系列-基本使用
在springboot中使用SHiro作为安全框架,非常简单,灵活性也比较高 在springboot中使用Shiro不用像官网教程那样,官网是将配置文件写在INI文件和XML配置中 springboo ...
- mysql查询版本
系统环境下 :mysql -V; mysql内:select version();
- PLC中增益和偏移
y=kx+b这个直线方程,那么增益就是指k这个斜率,而偏移就是指b. 模拟量转换时一般是不需要设置这两个参数的,只有当外部信号与模块接收的信号在值上有偏差的情况下才会去调整这个参数. 如果的模块信号是 ...
- typora的第一天
一级标题 二级标题 三级标题 ..... 表格 java spring mybatis 代码 java代码 public void Hello(){ } 字体 hello word! hello wo ...
- 给你的博客加个aplayer
1.在 layout.ejs 中 body 标签内粘贴入以下 <!--音乐--> <link rel="stylesheet" href="https: ...
- 用GitHub Actions自动部署Hexo
什么是 GitHub Actions ? GitHub Actions 是一个 CI/CD(持续集成/持续部署)工具,GitHub 于 2018 年 10 月推出,正式版于 2019 年 11 月正式 ...
- 对DDD使用的一些建议
群里经常看到类似于"看了DDD之后就不会写代码了"的情况,趁最近学车的间隙,写写我的看法. 关于这个事儿,我是觉得:当没有DDD的时候,如果你知道怎么做,那就那么做好了,不要考虑D ...
- 学军中学第三届“图灵杯”趣味网络邀请赛——中级T4.欧拉回路 (图论,哈希)
题面 补题链接 7 5 6 7 1 2 3 3 13 5 30 50 10 30 70 8 题解 存在欧拉回路的条件是:1. 每个点的度数都是偶数.2. 有边的连通块最多一个. 数据范围是允许我们 n ...
- Linux之firewalld防火墙规则
一, 什么是防火墙规则? 允许哪些服务端口被放行,怎么放行,及哪些服务端口被阻拦,如何阻拦的一组网络安全规则.支持ipv4和ipv6,且分为直接规则和富规则两种. 二, 如何管理firewalld 1 ...