winExec.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "console/console.h"
  24. #include "sim/simBase.h"
  25. #include "string/unicode.h"
  26. #include "platform/threads/thread.h"
  27. #include "platform/threads/mutex.h"
  28. #include "memory/safeDelete.h"
  29. //////////////////////////////////////////////////////////////////////////
  30. // Thread for executing in
  31. //////////////////////////////////////////////////////////////////////////
  32. class ExecuteThread : public Thread
  33. {
  34. // [tom, 12/14/2006] mProcess is only used in the constructor before the thread
  35. // is started and in the thread itself so we should be OK without a mutex.
  36. HANDLE mProcess;
  37. public:
  38. ExecuteThread(const char *executable, const char *args = NULL, const char *directory = NULL);
  39. virtual void run(void *arg = 0);
  40. };
  41. //////////////////////////////////////////////////////////////////////////
  42. // Event for cleanup
  43. //////////////////////////////////////////////////////////////////////////
  44. class ExecuteCleanupEvent : public SimEvent
  45. {
  46. ExecuteThread *mThread;
  47. bool mOK;
  48. public:
  49. ExecuteCleanupEvent(ExecuteThread *thread, bool ok)
  50. {
  51. mThread = thread;
  52. mOK = ok;
  53. }
  54. virtual void process(SimObject *object)
  55. {
  56. Con::executef(2, "onExecuteDone", Con::getIntArg(mOK));
  57. SAFE_DELETE(mThread);
  58. }
  59. };
  60. //////////////////////////////////////////////////////////////////////////
  61. ExecuteThread::ExecuteThread(const char *executable, const char *args /* = NULL */, const char *directory /* = NULL */) : Thread(0, NULL, false)
  62. {
  63. //#pragma message("Implement UNICODE support for ExecuteThread [12/14/2006 tom]" )
  64. SHELLEXECUTEINFOA shl;
  65. dMemset(&shl, 0, sizeof(shl));
  66. shl.cbSize = sizeof(shl);
  67. shl.fMask = SEE_MASK_NOCLOSEPROCESS;
  68. char exeBuf[1024];
  69. Platform::makeFullPathName(executable, exeBuf, sizeof(exeBuf));
  70. shl.lpVerb = "open";
  71. shl.lpFile = exeBuf;
  72. shl.lpParameters = args;
  73. shl.lpDirectory = directory;
  74. shl.nShow = SW_SHOWNORMAL;
  75. if(ShellExecuteExA(&shl) && shl.hProcess)
  76. {
  77. mProcess = shl.hProcess;
  78. start();
  79. }
  80. }
  81. void ExecuteThread::run(void *arg /* = 0 */)
  82. {
  83. if(mProcess == NULL)
  84. return;
  85. DWORD wait;
  86. while(! checkForStop() && (wait = WaitForSingleObject(mProcess, 200)) != WAIT_OBJECT_0) ;
  87. Sim::postEvent(Sim::getRootGroup(), new ExecuteCleanupEvent(this, wait == WAIT_OBJECT_0), -1);
  88. }
  89. //////////////////////////////////////////////////////////////////////////
  90. // Console Functions
  91. //////////////////////////////////////////////////////////////////////////
  92. ConsoleFunction(shellExecute, bool, 2, 4, "(executable, [args], [directory]) Executes a process"
  93. "@param executable The program to execute\n"
  94. "@param args Arguments to pass to the executable\n"
  95. "@param directory The directory in which the program is located\n"
  96. "@return Returns true on success, false otherwise")
  97. {
  98. ExecuteThread *et = new ExecuteThread(argv[1], argc > 2 ? argv[2] : NULL, argc > 3 ? argv[3] : NULL);
  99. if(! et->isAlive())
  100. {
  101. delete et;
  102. return false;
  103. }
  104. return true;
  105. }
  106. ConsoleFunction(shellExecuteBlocking, int, 2, 6, "(executable, [args], [directory])"
  107. "@param executable The program to execute\n"
  108. "@param args Arguments to pass to the executable\n"
  109. "@param directory The directory in which the program is located\n"
  110. "@return Returns true on success, false otherwise")
  111. {
  112. const char* executable = argv[1];
  113. const char* args = argc > 2 ? argv[2] : NULL;
  114. const char* directory = argc > 3 ? argv[3] : NULL;
  115. SHELLEXECUTEINFOA shl;
  116. dMemset(&shl, 0, sizeof(shl));
  117. shl.cbSize = sizeof(shl);
  118. shl.fMask = SEE_MASK_NOCLOSEPROCESS;
  119. char exeBuf[1024];
  120. Platform::makeFullPathName(executable, exeBuf, sizeof(exeBuf));
  121. shl.lpVerb = "open";
  122. shl.lpFile = exeBuf;
  123. shl.lpParameters = args;
  124. shl.lpDirectory = directory;
  125. shl.nShow = SW_HIDE;
  126. ShellExecuteExA(&shl);
  127. if ( shl.hProcess == NULL )
  128. return false;
  129. return ( WaitForSingleObject( shl.hProcess, INFINITE) == WAIT_OBJECT_0 );
  130. }
  131. void Platform::openFolder(const char* path )
  132. {
  133. char filePath[1024];
  134. Platform::makeFullPathName(path, filePath, sizeof(filePath));
  135. ::ShellExecuteA( NULL,"explore",filePath, NULL, NULL, SW_SHOWNORMAL);
  136. }