当前位置: 代码迷 >> Android >> 匿名端口方式,采用c++语言,建立PC机和Android设备通讯,遇到有关问题
  详细解决方案

匿名端口方式,采用c++语言,建立PC机和Android设备通讯,遇到有关问题

热度:29   发布时间:2016-05-01 14:49:14.0
匿名端口方式,采用c++语言,建立PC机和Android设备通讯,遇到问题。
我想实现一个功能,采用匿名端口的方式,通过PC机和Android设备建立通讯;visual studio c++ 2008
为什么会出现这种情况, 在cmd下操作情况如下:
C:\Users\Administrator>adb shell
[email protected]:/ $ su
su
Permission denied
[email protected]:/ $
用匿名端口方式,输出却是得不到正确结果:
1)输入:adb shell 输出:[email protected]:/ $(正确)
2)输入:su 输出:
循环输出却不是:
su
Permission denied(不正确)为啥不正确呢?
源代码如下,给指点指点,欢迎批评
#include "Stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

typedef struct TASK_ITEM_
{
std::string strCmdLine;
std::vector<string> results;
}AdbTask;

HANDLE g_hRead, g_hWrite;

int RunProgram( AdbTask* pTask);
bool ReadStream( vector<string> &lines, DWORD &dwFileSize);
DWORD WINAPI StartThreadGetDeviceInfo( LPVOID pParam)
{
AdbTask* pTask = new AdbTask;
pTask ->strCmdLine = "shell";
RunProgram( pTask);
return 0;
}

int main( int argc, char *argv[])
{
DWORD dwThread = 0;
CreateThread( NULL, 0, StartThreadGetDeviceInfo, 0, 0, &dwThread);

char command[128];
while (true)
{
memset( command, 0, 128);
scanf( "%s", command);
DWORD err = GetLastError();
WriteFile( g_hWrite, command, lstrlen(command), &err,NULL);
}
return 0;
}

bool ReadStream( vector<string> &lines, DWORD &dwFileSize)
{
lines.clear();

DWORD dwRead = 0;
while (true)
{
dwFileSize = GetFileSize(g_hRead, NULL);
if ( dwFileSize < 1)
{
Sleep(5*1000);
continue;
}
char *pData = new char[dwFileSize];
memset( pData, 0, dwFileSize);
if (!::ReadFile(g_hRead, pData, dwFileSize, &dwRead, NULL))
{
break;
}
else if (0 == dwRead)
break;
printf("%s\r\n", pData);
}
return true;
}

int RunProgram( AdbTask* pTask)
{
string theCommand("C:\\Windows\\adb.exe");
theCommand += " ";
theCommand += pTask->strCmdLine;
pTask->strCmdLine = theCommand;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&g_hRead, &g_hWrite, &sa, 0))
{
return 0;
}
STARTUPINFO startupInfo;
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; //Enable the wShowWindow
startupInfo.wShowWindow = SW_HIDE; //Hide the consol window
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.hStdOutput = g_hWrite;
//startupInfo.hStdError = g_hWrite;
startupInfo.hStdInput = g_hRead;

char strCommand[256];
memset( strCommand, 0 , 256);
strcpy( strCommand, theCommand.c_str());
PROCESS_INFORMATION processInfo; 
memset(&processInfo, 0, sizeof(processInfo));
if (!::CreateProcess(NULL,
strCommand,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&startupInfo,
&processInfo))
{
CloseHandle(g_hWrite);
return 0;
}
else
{
CloseHandle (processInfo.hProcess); // 关闭所返回的子进程句柄
CloseHandle (processInfo.hThread); // 关闭子进程中主线程句柄
}
if (processInfo.hProcess != NULL)
{
DWORD dwFileSize = 0;
if (!ReadStream( pTask->results, dwFileSize))
{
}
}
return 7; 
}

------解决方案--------------------
需要root
  相关解决方案