C++启动和结束adb service

上次分享了关于C#发送控制adb的命令,本次分享下c++启动和结束adb service的程序:

void CSingleDocApplication1View::OnAdb()

{

// TODO: Add your command handler code here

ExecuteADBShellCmd();

Sleep(200);

SendCommand("version");

Sleep(200);

KillADBServer();

}

bool CSingleDocApplication1View::ExecuteADBShellCmd()

{

int ADBTryCT = 0;

char buffer[4096] = { 0 };

bool readflag = true;

char Ttcommand[1024] = { 0 };

char CurrentPath[512] = "";

GetModuleFileNameA(NULL, CurrentPath, sizeof(CurrentPath));

*(strrchr(CurrentPath, '\\')) = '\0';

if (strlen(CurrentPath) == 0)

{

return false;

}

sprintf_s(Ttcommand, sizeof(Ttcommand), "%s\\adb.exe devices", CurrentPath);

HANDLE hRead = NULL, hWrite = NULL;

SECURITY_ATTRIBUTES sa;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.lpSecurityDescriptor = NULL;

sa.bInheritHandle = TRUE;

if (!CreatePipe(&hRead, &hWrite, &sa, 0))

{

::MessageBox(NULL, _T("CreatePipe Failed!!"), _T("Notify"), MB_OK);

return false;

}

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(STARTUPINFO));

si.cb = sizeof(STARTUPINFO);

GetStartupInfo(&si);

si.hStdError = hWrite;

si.hStdOutput = hWrite;

si.wShowWindow = SW_HIDE;

si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

USES_CONVERSION;

if (!::CreateProcess(NULL, A2W(Ttcommand), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))

{

::MessageBox(NULL, _T("Unable to open program: adb.exe!"), _T("Notify"), MB_OK);

return false;

}

ResumeThread(pi.hThread);

CloseHandle(pi.hThread);

DWORD dwRet = WaitForSingleObject(pi.hProcess, 60000);

if (dwRet == WAIT_TIMEOUT)

{

KillADBServer();

return false;

}

DWORD dwExitCode;

GetExitCodeProcess(pi.hProcess, &dwExitCode);

CloseHandle(pi.hProcess);

CloseHandle(hWrite);

CloseHandle(hRead);

return true;

}

bool CSingleDocApplication1View::KillADBServer()

{

HANDLE hRead = NULL, hWrite = NULL;

SECURITY_ATTRIBUTES sa;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.lpSecurityDescriptor = NULL;

sa.bInheritHandle = TRUE;

bool findAdbDevice = false;

char Ttcommand[512] = { 0 };

sprintf_s(Ttcommand, sizeof(Ttcommand), "%s", "adb kill-server");

if (!CreatePipe(&hRead, &hWrite, &sa, 0))

{

return false;

}

PROCESS_INFORMATION pi;

STARTUPINFO si;

si.cb = sizeof(STARTUPINFO);

GetStartupInfo(&si);

si.hStdError = hWrite;

si.hStdOutput = hWrite;

si.wShowWindow = SW_HIDE;

si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

USES_CONVERSION;

if (!::CreateProcess(NULL, A2W(Ttcommand), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))

{

return FALSE;

}

DWORD dwExitCode;

ResumeThread(pi.hThread);

CloseHandle(pi.hThread);

WaitForSingleObject(pi.hProcess, INFINITE);

GetExitCodeProcess(pi.hProcess, &dwExitCode);

CloseHandle(pi.hProcess);

return true;

}

int CSingleDocApplication1View::SendCommand(char* szCmd)

{

DWORD dwRet = 0u;

SECURITY_ATTRIBUTES sa;

HANDLE hRead, hWrite;

bool findAdbDevice = false;

char CurrentPath[512] = "";

GetModuleFileNameA(NULL, CurrentPath, sizeof(CurrentPath));

*(strrchr(CurrentPath, '\\')) = '\0';

if (strlen(CurrentPath) == 0)

{

return false;

}

char Ttcommand[512] = { 0 };

sprintf_s(Ttcommand, sizeof(Ttcommand), ("%s\\adb.exe %s"), CurrentPath, szCmd);

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.lpSecurityDescriptor = NULL;

sa.bInheritHandle = TRUE;

if (!CreatePipe(&hRead, &hWrite, &sa, 0))

{

return false;

}

PROCESS_INFORMATION pi;

STARTUPINFO si;

si.cb = sizeof(STARTUPINFO);

GetStartupInfo(&si);

si.hStdError = hWrite;

si.hStdOutput = hWrite;

si.wShowWindow = SW_HIDE;

si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

USES_CONVERSION;

if (!::CreateProcess(NULL, A2W(Ttcommand), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))

{

return false;

}

char buffer[100] = { 0 };

DWORD bytesRead;

CloseHandle(hWrite);

while (true) // 保证把返回的数据读取完

{

if (ReadFile(hRead, buffer, sizeof(buffer), &bytesRead, NULL) == NULL)

{

SecureZeroMemory(buffer, sizeof(buffer));

break;

}

::MessageBoxA(NULL, buffer, "Return Value", MB_OK); // 线损返回值

if ((strstr(buffer, "TTL") != NULL))

{

findAdbDevice = true;

SecureZeroMemory(buffer, sizeof(buffer));

break;

}

SecureZeroMemory(buffer, sizeof(buffer));

}

Sleep(100);

DWORD dwExitCode;

unsigned int uInt = 0;

ResumeThread(pi.hThread);

CloseHandle(pi.hThread);

GetExitCodeProcess(pi.hProcess, &dwExitCode);

TerminateProcess(pi.hProcess, uInt);

CloseHandle(pi.hProcess);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值