#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
char cmdbuffer[1024];
HANDLE hReadPipe;
HANDLE hReadPipe2;
HANDLE hWritePipe;
HANDLE hWritePipe2;
DWORD __stdcall ThreadFun(void *pVoid)
{
SECURITY_ATTRIBUTES sat;
STARTUPINFO startupinfo;
PROCESS_INFORMATION pinfo;
BYTE buffer[1024];
DWORD byteRead;
sat.nLength=sizeof(SECURITY_ATTRIBUTES);
sat.bInheritHandle=true;
sat.lpSecurityDescriptor=NULL;
if(!CreatePipe(&hReadPipe,&hWritePipe,&sat,NULL))
{
//MessageBox(NULL, "Create Pipe Error! ", "error! ",MB_OK);
return 0;
}
if(!CreatePipe(&hReadPipe2,&hWritePipe2,&sat,NULL))
{
//MessageBox(NULL, "Create Pipe2 Error! ", "error! ",MB_OK);
return 0;
}
startupinfo.cb=sizeof(STARTUPINFO);
GetStartupInfo(&startupinfo);
startupinfo.hStdError=hWritePipe;
startupinfo.hStdOutput=hWritePipe;
startupinfo.hStdInput=hReadPipe2;
startupinfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupinfo.wShowWindow=SW_HIDE;
if(!CreateProcess(NULL, "C:\\Windows\\adb.exe shell",NULL, NULL, TRUE, NULL, NULL, NULL,&startupinfo,&pinfo))
{
DWORD dwError = GetLastError();
//MessageBox(NULL, "create process error! ", "Error! ",MB_OK);
return 0;
}
CloseHandle(hWritePipe);
CloseHandle(hReadPipe2);
CloseHandle(pinfo.hThread);
CloseHandle(pinfo.hProcess);
while(true)
{
RtlZeroMemory(buffer,1024);
if(ReadFile(hReadPipe,buffer,1023,&byteRead,NULL)==NULL)
break;
cout<<buffer<<endl;
}
CloseHandle(hReadPipe);
CloseHandle(hWritePipe2);
return 0;
}
int main()
{
//创建线程:
HANDLE hThread = NULL;
DWORD dwThread = 0;
hThread = CreateThread( NULL, 0, ThreadFun, 0, 0, &dwThread);//这个参数你根据你的需要决定要不要
if (hThread != NULL)
{
//MessageBox( "CreateThread failed. ", "main ", MB_OK );
}
else
{
CloseHandle( hThread );
}
char command[128] = {0};
while ( true)
{
memset( command, 0, 128);
gets_s(command);
strcat_s(command, " \r\n");//一定要有回车换行
//往adb程序里写命令:
DWORD byteRead = 0;
WriteFile(hWritePipe2,command,strlen(command),&byteRead,NULL);
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页