天下

记录修行的印记

CreateProcess+Pipe 交互运行命令

BOOL CNeowayAndroidCheckToolView::PipeSendCmd(const HANDLE& handle,LPCTSTR cmd)
{
    CString _cmd(cmd);
    BOOL bRet;
    DWORD dwWrited;

    _cmd.Append(
"\r\n");    
    
return WriteFile(handle,_cmd,_cmd.GetLength(),&dwWrited,NULL);
}
BOOL CNeowayAndroidCheckToolView::PipeRead(
const HANDLE& handle,CString& result,DWORD dwMilliseconds)
{
    BOOL bRet 
= FALSE;
    CHAR buffer[
4100];
    DWORD dwReaded;

    result.Empty();
    
while(1)
    {
        Sleep(dwMilliseconds);
        bRet 
= PeekNamedPipe(handle,buffer,1,&dwReaded,NULL,NULL);
        
if (!bRet) goto _flag_exit;
        
if (dwReaded>0)
        {
            bRet 
= ReadFile(handle, buffer,4096&dwReaded, NULL);
            
if (!bRet) goto _flag_exit;
            buffer[dwReaded] 
= '\0';
            result 
+= buffer;
        }
else {
            
break;
        }
    }
_flag_exit:
    
return bRet;
}
BOOL CNeowayAndroidCheckToolView::TestGpio()
{
    CString init_cmd 
= Path::Combine( Path::GetAppDirectory(),"adb.exe shell");
    
//CString init_cmd = "cmd.exe";
    CString cmd;
    CString result;
    DWORD exit_code 
= -1;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    HANDLE hStdOutRead
=NULL, hStdOutPipe=NULL;
    HANDLE hStdInWrite
=NULL, hStdInPipe=NULL;
    SECURITY_ATTRIBUTES saRead,saWrite;
    BOOL bRet;
    BOOL bSucceed;    
    DWORD dwReaded;
    std::vector
<CString> lines;

    saRead.nLength 
= sizeof(SECURITY_ATTRIBUTES);
    saRead.bInheritHandle 
= TRUE;
    saRead.lpSecurityDescriptor 
= NULL;
    saWrite 
= saRead;

    bRet 
= CreatePipe(&hStdOutRead, &hStdOutPipe, &saRead, 4096);
    
if (!bRet)
        
goto _flag_exit;
    bRet 
= CreatePipe(&hStdInPipe, &hStdInWrite, &saWrite, 4096);
    
if (!bRet)
        
goto _flag_exit;

    memset(
&si, 0sizeof(si));
    si.cb 
= sizeof(si);
    si.dwFlags 
= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow 
= SW_HIDE;
    si.hStdOutput 
= hStdOutPipe;
    si.hStdInput  
= hStdInPipe;

    bRet 
= CreateProcess(NULL, (LPSTR)(LPCTSTR)init_cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    
if (!bRet) goto _flag_exit;
        

    CloseHandle(hStdOutPipe);hStdOutPipe 
= NULL;
    CloseHandle(hStdInPipe);hStdInPipe 
= NULL;

    bRet 
= PipeRead(hStdOutRead,result,50);
    
if (!bRet) goto _flag_exit;

    PipeSendCmd(hStdInWrite,
"cd sys/gpio_test");
    
if (!bRet) goto _exit_adb;

    bRet 
= PipeRead(hStdOutRead,result,50);
    
if (!bRet) goto _exit_adb;

    PipeSendCmd(hStdInWrite,
"echo \"1\" > start");
    
if (!bRet) goto _exit_adb;

    bRet 
= PipeRead(hStdOutRead,result,50);
    
if (!bRet) goto _exit_adb;

    PipeSendCmd(hStdInWrite,
"cat result");
    
if (!bRet) goto _exit_adb;

    bRet 
= PipeRead(hStdOutRead,result,50);
    
if (!bRet) goto _exit_adb;

    bRet 
= (result.Find("OK")>=0);
_exit_adb:
    
if (!PipeSendCmd(hStdInWrite,"exit")) {
        bRet 
= FALSE; goto _flag_exit;
    }

    WaitForSingleObject( pi.hProcess, 
5000);

    
if (!GetExitCodeProcess(pi.hProcess, &exit_code)) {
        bRet 
= FALSE; goto _flag_exit;
    }
    bRet 
= bRet && (exit_code==0);
_flag_exit:
    
if  (hStdOutRead!=NULL)
        CloseHandle(hStdOutRead);
    
if  (hStdOutPipe!=NULL)
        CloseHandle(hStdOutPipe);
    
if  (hStdInWrite!=NULL)
        CloseHandle(hStdInWrite);
    
if  (hStdInPipe!=NULL)
        CloseHandle(hStdInPipe);
    
return bRet;
}

posted on 2016-05-27 17:13 天下 阅读(1605) 评论(0)  编辑 收藏 引用 所属分类: Win32


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2016年5月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论