| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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 | }
|
|---|