winExec.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "winExec_ScriptBinding.h"
  43. //////////////////////////////////////////////////////////////////////////
  44. // Event for cleanup
  45. //////////////////////////////////////////////////////////////////////////
  46. class ExecuteCleanupEvent : public SimEvent
  47. {
  48. ExecuteThread *mThread;
  49. bool mOK;
  50. public:
  51. ExecuteCleanupEvent(ExecuteThread *thread, bool ok)
  52. {
  53. mThread = thread;
  54. mOK = ok;
  55. }
  56. virtual void process(SimObject *object)
  57. {
  58. Con::executef(2, "onExecuteDone", Con::getIntArg(mOK));
  59. SAFE_DELETE(mThread);
  60. }
  61. };
  62. //////////////////////////////////////////////////////////////////////////
  63. ExecuteThread::ExecuteThread(const char *executable, const char *args /* = NULL */, const char *directory /* = NULL */) : Thread(0, NULL, false)
  64. {
  65. //#pragma message("Implement UNICODE support for ExecuteThread [12/14/2006 tom]" )
  66. SHELLEXECUTEINFOA shl;
  67. dMemset(&shl, 0, sizeof(shl));
  68. shl.cbSize = sizeof(shl);
  69. shl.fMask = SEE_MASK_NOCLOSEPROCESS;
  70. char exeBuf[1024];
  71. Platform::makeFullPathName(executable, exeBuf, sizeof(exeBuf));
  72. shl.lpVerb = "open";
  73. shl.lpFile = exeBuf;
  74. shl.lpParameters = args;
  75. shl.lpDirectory = directory;
  76. shl.nShow = SW_SHOWNORMAL;
  77. if(ShellExecuteExA(&shl) && shl.hProcess)
  78. {
  79. mProcess = shl.hProcess;
  80. start();
  81. }
  82. }
  83. void ExecuteThread::run(void *arg /* = 0 */)
  84. {
  85. if(mProcess == NULL)
  86. return;
  87. DWORD wait;
  88. while(! checkForStop() && (wait = WaitForSingleObject(mProcess, 200)) != WAIT_OBJECT_0) ;
  89. Sim::postEvent(Sim::getRootGroup(), new ExecuteCleanupEvent(this, wait == WAIT_OBJECT_0), -1);
  90. }
  91. void Platform::openFolder(const char* path )
  92. {
  93. char filePath[1024];
  94. Platform::makeFullPathName(path, filePath, sizeof(filePath));
  95. ::ShellExecuteA( NULL,"explore",filePath, NULL, NULL, SW_SHOWNORMAL);
  96. }