上次分享了关于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;
}