2016年11月9日 星期三

[C++]taccess, CreatePipe, ZeroMemory, CreateProcess, WaitForSingleObject, ReadFile, CloseHandle

 

bool CLicense::runCheckViva()

{

        bool b_wait_fastboot = false;

        int nLimitTime = 0 ;

        CString sPath;

        char output[4096] = {0};

        char ErrorCode[4096] = {0};

 

//     Sleep(m_msWaitTime);

 

        // wait for adb

        sPath.Format(" shell ps com.qisda.viva");

        while ( !b_wait_fastboot){

                //if (!exeADB(" shell ps com.qisda.viva","com.qisda.viva"))

                if ( !(ExecADB( sPath, output , ErrorCode, "com.qisda.viva") ))

                {

                        TraceLog(MSG_INFO, _T("runRebootWaitADB get com.qisda.viva  fail "));

                        Sleep(1000);

                }

                else

                {

                        b_wait_fastboot = true;

                        TraceLog(MSG_INFO, _T("runRebootWaitADB get com.qisda.viva  OK"));

                }

 

                nLimitTime ++;

                if ( nLimitTime > m_nConnectRetry ) break; //time out

        }

 

       

        if ( b_wait_fastboot)       {

                m_strResult = "PASS";

                FactoryLog();

                return true;

        }

        else

        {

                m_strResult = "FAIL";

                FactoryLog();

                return false; //can't find Viva

        }

}

 

 

bool CLicense::ExecADB( CString csCmd, char* output, char* ErrorCode, std::string sFinds)

{

        bool isOk = false;

        int nAdbTimeOut = 10 * 1000; // 10s

        CString sCmd;

 

        DWORD nPipeSize = 1024 * 1024; //1M pipeline 

        CString pathExe = pthToolDir + _T("\\adb.exe");

 

        if (_taccess(pathExe, 0) == -1)

        {

                strcpy(ErrorCode, "CLicense  ERROR: No adb.exe exist!");

                return false;

        }

 

        HANDLE hRead, hWrite;

        SECURITY_ATTRIBUTES sa;

        sa.nLength = sizeof(SECURITY_ATTRIBUTES);

        sa.lpSecurityDescriptor = NULL;

        sa.bInheritHandle = TRUE;

        if (!CreatePipe(&hRead, &hWrite, &sa, nPipeSize))

        {

                strcpy(ErrorCode, "CLicense ERROR: CreatePipe fail!");

                return false;

        }

 

        //HANDLE hProcess = NULL;

        PROCESS_INFORMATION processInfo;

        STARTUPINFO startupInfo;

        ::ZeroMemory(&startupInfo, sizeof(startupInfo));

        startupInfo.cb = sizeof(startupInfo);

        startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

        startupInfo.wShowWindow     = SW_HIDE;

        startupInfo.hStdError = hWrite;

        startupInfo.hStdOutput = hWrite;

 

        //csCmd.Format(_T("%s pull %s %s"), pathExe, cmd3.c_str() , csPcLogPath );

        sCmd.Format("%s %s", pathExe, csCmd);

        TRACE(_T("Cmd: %s\n"), sCmd);

        if (::CreateProcess(NULL, sCmd.GetBuffer(0), NULL, NULL, TRUE, 0, NULL, NULL, &startupInfo, &processInfo))

        {

                DWORD TimeOutSignal = WaitForSingleObject(processInfo.hProcess, nAdbTimeOut); // timeout in 10 seconds

 

                CloseHandle(hWrite);

                hWrite = NULL;

                //if timeout then exit the process

                if (TimeOutSignal == WAIT_TIMEOUT)

                {

                        isOk = false;

                        TerminateProcess(processInfo.hProcess, 0);

                        strcpy(ErrorCode, "ERROR: ExecADB timeout");

                        AfxMessageBox("PULL FILE TIME OUT");

                }

                else

                {

                        DWORD bytesRead;

                        std::string        std_out = "";

                        //std::string std_find_string = "pass";

                        char* message = new char[nPipeSize];

                        memset(message, 0, sizeof(message));

                        ::ReadFile(hRead, message, nPipeSize, &bytesRead, NULL);

                        message[bytesRead] = '\0';

 

                        std_out = message;

                        if( ( std_out.find( sFinds )  == string::npos) && (sFinds.find( "NULL" )  == string::npos))

                        {

                                //m_strMessage = "ERROR: ExecADB fail! ";

                                csCmd.Format("ExecADB fail!, %s",csCmd);

                                m_strMessage = csCmd;

 

                                TraceLog(MSG_ERROR, m_strMessage);     

 

                                strcpy(ErrorCode, "ERROR: ExecADB fail!");

                        }

                        else

                        {

                                isOk = true;

                                strncpy(output, message, 4096);

                                strcpy(ErrorCode, "ExecADB command ok");

                                csCmd.Format("ADB OK, %s",csCmd);

                                m_strMessage = csCmd;

                                TraceLog(MSG_ERROR, m_strMessage);     

                        }

                        delete [] message;

 

                }

        }

        else

        {

                isOk = false;

                strcpy(ErrorCode, "ERROR: Execute ExecADB fail!");

                m_strMessage = "ERROR: Execute ExecADB fail! ";

                TraceLog(MSG_ERROR, m_strMessage);     

        }

        sCmd.ReleaseBuffer();

        CloseHandle(hRead);

        if (hWrite)CloseHandle(hWrite);

        //CloseHandle(hProcess);

 

        CloseHandle(processInfo.hProcess);

        CloseHandle(processInfo.hThread);

        //hProcess = NULL;

 

        return isOk;

}

**********************Confidentiality Notice************************
The opinions and views expressed in this e-mail are solely those of the author and do not necessarily represent our company and its affiliates. If this e-mail is not originally intended for you, or received by you in error, do not disclose its content to anyone and delete it immediately. This e-mail may contain information that is legally privileged, confidential or exempt from disclosure.

沒有留言:

張貼留言