winExec.cpp

Engine/source/platformWin32/winExec.cpp

More...

Classes:

Public Functions

DefineEngineFunction(shellExecute , bool , (const char *executable, const char *args, const char *directory) , ("", "") , "(string executable, string args, string directory)" "@brief Launches an outside executable or batch <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n\n</a>" "@param executable Name of the executable or batch <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n</a>" "@param args Optional list of arguments, in string format, <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> pass <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">executable\n</a>" " @param directory Optional string containing path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> output or <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">shell\n</a>" " @return true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> executed, false <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">not\n</a>" " @ingroup Platform" )

Detailed Description

Public Functions

DefineEngineFunction(shellExecute , bool , (const char *executable, const char *args, const char *directory) , ("", "") , "(string executable, string args, string directory)" "@brief Launches an outside executable or batch <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n\n</a>" "@param executable Name of the executable or batch <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n</a>" "@param args Optional list of arguments, in string format, <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> pass <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">executable\n</a>" " @param directory Optional string containing path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> output or <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">shell\n</a>" " @return true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> executed, false <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">not\n</a>" " @ingroup Platform" )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platformWin32/platformWin32.h"
 25#include "console/console.h"
 26#include "console/engineAPI.h"
 27#include "console/simBase.h"
 28#include "core/strings/unicode.h"
 29#include "platform/threads/thread.h"
 30#include "platform/threads/mutex.h"
 31#include "core/util/safeDelete.h"
 32#include "util/tempAlloc.h"
 33
 34//-----------------------------------------------------------------------------
 35// Thread for executing in
 36//-----------------------------------------------------------------------------
 37
 38class ExecuteThread : public Thread
 39{
 40   // [tom, 12/14/2006] mProcess is only used in the constructor before the thread
 41   // is started and in the thread itself so we should be OK without a mutex.
 42   HANDLE mProcess;
 43
 44public:
 45   ExecuteThread(const char *executable, const char *args = NULL, const char *directory = NULL);
 46
 47   virtual void run(void *arg = 0);
 48};
 49
 50//-----------------------------------------------------------------------------
 51// Event for cleanup
 52//-----------------------------------------------------------------------------
 53
 54class ExecuteCleanupEvent : public SimEvent
 55{
 56   ExecuteThread *mThread;
 57   bool mOK;
 58
 59public:
 60   ExecuteCleanupEvent(ExecuteThread *thread, bool ok)
 61   {
 62      mThread = thread;
 63      mOK = ok;
 64   }
 65
 66   virtual void process(SimObject *object)
 67   {
 68      if( Con::isFunction( "onExecuteDone" ) )
 69         Con::executef( "onExecuteDone", Con::getIntArg( mOK ) );
 70      SAFE_DELETE(mThread);
 71   }
 72};
 73
 74//-----------------------------------------------------------------------------
 75
 76ExecuteThread::ExecuteThread(const char *executable, const char *args /* = NULL */, const char *directory /* = NULL */) : Thread(0, NULL, false)
 77{
 78   SHELLEXECUTEINFO shl;
 79   dMemset(&shl, 0, sizeof(shl));
 80
 81   shl.cbSize = sizeof(shl);
 82   shl.fMask = SEE_MASK_NOCLOSEPROCESS;
 83   
 84   char exeBuf[1024];
 85   Platform::makeFullPathName(executable, exeBuf, sizeof(exeBuf));
 86   
 87   TempAlloc< TCHAR> dirBuf( ( directory ? dStrlen( directory ) : 0 ) + 1 );
 88   dirBuf[ dirBuf.size - 1 ] = 0;
 89
 90#ifdef UNICODE
 91   WCHAR exe[ 1024 ];
 92   convertUTF8toUTF16( exeBuf, exe );
 93
 94   TempAlloc< WCHAR> argsBuf( ( args ? dStrlen( args ) : 0 ) + 1 );
 95   argsBuf[ argsBuf.size - 1 ] = 0;
 96
 97   if( args )
 98      convertUTF8toUTF16N( args, argsBuf, argsBuf.size );
 99   if( directory )
100      convertUTF8toUTF16N( directory, dirBuf, dirBuf.size );
101#else
102   char* exe = exeBuf;
103   char* argsBuf = args;
104   if( directory )
105      dStrpcy( dirBuf, directory );
106#endif
107
108   backslash( exe );
109   backslash( dirBuf );
110   
111   shl.lpVerb = TEXT( "open" );
112   shl.lpFile = exe;
113   shl.lpParameters = argsBuf;
114   shl.lpDirectory = dirBuf;
115
116   shl.nShow = SW_SHOWNORMAL;
117
118   if(ShellExecuteEx(&shl) && shl.hProcess)
119   {
120      mProcess = shl.hProcess;
121      start();
122   }
123}
124
125void ExecuteThread::run(void *arg /* = 0 */)
126{
127   if(mProcess == NULL)
128      return;
129
130   DWORD wait = WAIT_OBJECT_0 - 1; // i.e., not WAIT_OBJECT_0
131   while(! checkForStop() && (wait = WaitForSingleObject(mProcess, 200)) != WAIT_OBJECT_0) ;
132
133   Sim::postEvent(Sim::getRootGroup(), new ExecuteCleanupEvent(this, wait == WAIT_OBJECT_0), -1);
134}
135
136//-----------------------------------------------------------------------------
137// Console Functions
138//-----------------------------------------------------------------------------
139
140DefineEngineFunction( shellExecute, bool, (const char * executable, const char * args, const char * directory), ("", ""), "(string executable, string args, string directory)"
141            "@brief Launches an outside executable or batch file\n\n"
142            "@param executable Name of the executable or batch file\n"
143            "@param args Optional list of arguments, in string format, to pass to the executable\n"
144            "@param directory Optional string containing path to output or shell\n"
145            "@return true if executed, false if not\n"
146            "@ingroup Platform")
147{
148   ExecuteThread *et = new ExecuteThread( executable, args, directory );
149   if(! et->isAlive())
150   {
151      delete et;
152      return false;
153   }
154
155   return true;
156}
157
158#ifndef TORQUE_SDL
159
160void Platform::openFolder(const char* path )
161{
162   char filePath[1024];
163   Platform::makeFullPathName(path, filePath, sizeof(filePath));
164
165#ifdef UNICODE
166   WCHAR p[ 1024 ];
167   convertUTF8toUTF16( filePath, p );
168#else
169   char* p = filePath;
170#endif
171
172   backslash( p );
173
174   ::ShellExecute( NULL,TEXT("explore"),p, NULL, NULL, SW_SHOWNORMAL);
175}
176
177void Platform::openFile(const char* path )
178{
179   char filePath[1024];
180   Platform::makeFullPathName(path, filePath, sizeof(filePath));
181
182#ifdef UNICODE
183   WCHAR p[ 1024 ];
184   convertUTF8toUTF16( filePath, p );
185#else
186   char* p = filePath;
187#endif
188
189   backslash( p );
190
191   ::ShellExecute( NULL,TEXT("open"),p, NULL, NULL, SW_SHOWNORMAL);
192}
193
194#endif // !TORQUE_SDL
195
196