IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    CreateProcess+Pipe 交互运行命令

    天下发表于 2016-05-27 09:13:00
    love 0
    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, 0, sizeof(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;
    }


    天下 2016-05-27 17:13 发表评论


沪ICP备19023445号-2号
友情链接