windows C++ call ADB command
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了windows C++ call ADB command,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4221字,纯文字阅读大概需要7分钟。
内容图文
提供两种方式:
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全部内容,希望文章能够帮你解决windows C++ call ADB command所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。