tools/qtestlib/wince/remotelib/commands.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "commands.h"
       
    42 
       
    43 #define CLEAN_FAIL(a) {delete appName; \
       
    44     delete arguments; \
       
    45     return (a); }
       
    46 
       
    47 int qRemoteLaunch(DWORD, BYTE*, DWORD*, BYTE**, IRAPIStream* stream)
       
    48 {
       
    49     if (!stream)
       
    50         return -1;
       
    51 
       
    52     DWORD bytesRead;
       
    53     int appLength;
       
    54     wchar_t* appName = 0;
       
    55     int argumentsLength;
       
    56     wchar_t* arguments = 0;
       
    57     int timeout = -1;
       
    58     int returnValue = -2;
       
    59     DWORD error = 0;
       
    60 
       
    61     if (S_OK != stream->Read(&appLength, sizeof(appLength), &bytesRead))
       
    62         CLEAN_FAIL(-2);
       
    63     appName = (wchar_t*) malloc(sizeof(wchar_t)*(appLength + 1));
       
    64     if (S_OK != stream->Read(appName, sizeof(wchar_t)*appLength, &bytesRead))
       
    65         CLEAN_FAIL(-2);
       
    66     appName[appLength] = '\0';
       
    67 
       
    68     if (S_OK != stream->Read(&argumentsLength, sizeof(argumentsLength), &bytesRead))
       
    69         CLEAN_FAIL(-2);
       
    70     arguments = (wchar_t*) malloc(sizeof(wchar_t)*(argumentsLength + 1));
       
    71     if (S_OK != stream->Read(arguments, sizeof(wchar_t)*argumentsLength, &bytesRead))
       
    72         CLEAN_FAIL(-2);
       
    73     arguments[argumentsLength] = '\0';
       
    74 
       
    75     if (S_OK != stream->Read(&timeout, sizeof(timeout), &bytesRead))
       
    76         CLEAN_FAIL(-2);
       
    77 
       
    78     bool result = qRemoteExecute(appName, arguments, &returnValue, &error, timeout);
       
    79 
       
    80     if (timeout != 0) {
       
    81         if (S_OK != stream->Write(&returnValue, sizeof(returnValue), &bytesRead))
       
    82             CLEAN_FAIL(-4);
       
    83         if (S_OK != stream->Write(&error, sizeof(error), &bytesRead))
       
    84             CLEAN_FAIL(-5);
       
    85     }
       
    86     delete appName;
       
    87     delete arguments;
       
    88     // We need to fail here for the execute, otherwise the calling application will wait
       
    89     // forever for the returnValue.
       
    90     if (!result)
       
    91         return -3;
       
    92     return S_OK;
       
    93 }
       
    94 
       
    95 
       
    96 bool qRemoteExecute(const wchar_t* program, const wchar_t* arguments, int *returnValue, DWORD* error, int timeout)
       
    97 {
       
    98     *error = 0;
       
    99 
       
   100     if (!program)
       
   101         return false;
       
   102 
       
   103     PROCESS_INFORMATION pid;
       
   104     if (!CreateProcess(program, arguments, NULL, NULL, false, 0, NULL, NULL, NULL, &pid)) {
       
   105         *error = GetLastError();
       
   106         wprintf(L"Could not launch: %s\n", program);
       
   107         return false;
       
   108     }
       
   109 
       
   110     // Timeout is in seconds
       
   111     DWORD waitingTime = (timeout == -1) ? INFINITE : timeout * 1000;
       
   112 
       
   113     if (waitingTime != 0) {
       
   114         DWORD waitStatus = WaitForSingleObject(pid.hProcess, waitingTime);
       
   115         if (waitStatus == WAIT_TIMEOUT) {
       
   116             TerminateProcess(pid.hProcess, 2);
       
   117             return false;
       
   118         } else if (waitStatus == WAIT_OBJECT_0 && returnValue) {
       
   119             *returnValue = 0;
       
   120             DWORD exitCode;
       
   121             if (GetExitCodeProcess(pid.hProcess, &exitCode))
       
   122                 *returnValue = exitCode;
       
   123         }
       
   124     }
       
   125     return true;
       
   126 }