Browse Source

Bug fix: Copying extension command with ShowResultsInMsgBox flag to clipboard was not working

Source commit: 43a8f9a0981545744df4029dd4b65dc690033817
Martin Prikryl 8 years ago
parent
commit
d35141bbaf
1 changed files with 61 additions and 58 deletions
  1. 61 58
      source/windows/Tools.cpp

+ 61 - 58
source/windows/Tools.cpp

@@ -299,78 +299,81 @@ UnicodeString __fastcall StoreFormSize(TForm * Form)
 static void __fastcall ExecuteProcessAndReadOutput(const
   UnicodeString & Command, const UnicodeString & HelpKeyword, UnicodeString & Output)
 {
-  SECURITY_ATTRIBUTES SecurityAttributes;
-  ZeroMemory(&SecurityAttributes, sizeof(SecurityAttributes));
-  SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
-  SecurityAttributes.bInheritHandle = TRUE;
-  SecurityAttributes.lpSecurityDescriptor = NULL;
+  if (!CopyCommandToClipboard(Command))
+  {
+    SECURITY_ATTRIBUTES SecurityAttributes;
+    ZeroMemory(&SecurityAttributes, sizeof(SecurityAttributes));
+    SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
+    SecurityAttributes.bInheritHandle = TRUE;
+    SecurityAttributes.lpSecurityDescriptor = NULL;
 
-  HANDLE PipeRead = INVALID_HANDLE_VALUE;
-  HANDLE PipeWrite = INVALID_HANDLE_VALUE;
+    HANDLE PipeRead = INVALID_HANDLE_VALUE;
+    HANDLE PipeWrite = INVALID_HANDLE_VALUE;
 
-  if (!CreatePipe(&PipeRead, &PipeWrite, &SecurityAttributes, 0) ||
-      !SetHandleInformation(PipeRead, HANDLE_FLAG_INHERIT, 0))
-  {
-    throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Command)));
-  }
+    if (!CreatePipe(&PipeRead, &PipeWrite, &SecurityAttributes, 0) ||
+        !SetHandleInformation(PipeRead, HANDLE_FLAG_INHERIT, 0))
+    {
+      throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Command)));
+    }
 
-  PROCESS_INFORMATION ProcessInformation;
-  ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
+    PROCESS_INFORMATION ProcessInformation;
+    ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
 
-  try
-  {
     try
     {
-      STARTUPINFO StartupInfo;
-      ZeroMemory(&StartupInfo, sizeof(StartupInfo));
-      StartupInfo.cb = sizeof(STARTUPINFO);
-      StartupInfo.hStdError = PipeWrite;
-      StartupInfo.hStdOutput = PipeWrite;
-      StartupInfo.wShowWindow = SW_HIDE;
-      StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
-
-      if (!CreateProcess(NULL, Command.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation))
+      try
       {
-        throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Command)));
+        STARTUPINFO StartupInfo;
+        ZeroMemory(&StartupInfo, sizeof(StartupInfo));
+        StartupInfo.cb = sizeof(STARTUPINFO);
+        StartupInfo.hStdError = PipeWrite;
+        StartupInfo.hStdOutput = PipeWrite;
+        StartupInfo.wShowWindow = SW_HIDE;
+        StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
+
+        if (!CreateProcess(NULL, Command.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation))
+        {
+          throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Command)));
+        }
+      }
+      __finally
+      {
+        // If we do not close the handle here, the ReadFile below would get stuck once the app finishes writting,
+        // as it still sees that someone "can" write to the pipe.
+        CloseHandle(PipeWrite);
       }
-    }
-    __finally
-    {
-      // If we do not close the handle here, the ReadFile below would get stuck once the app finishes writting,
-      // as it still sees that someone "can" write to the pipe.
-      CloseHandle(PipeWrite);
-    }
-
-    char Buffer[4096];
-    DWORD BytesRead;
-    while (ReadFile(PipeRead, Buffer, sizeof(Buffer), &BytesRead, NULL))
-    {
-      Output += UnicodeString(UTF8String(Buffer, BytesRead));
-      // Same as in ExecuteShellCheckedAndWait
-      Sleep(200);
-      Application->ProcessMessages();
-    }
 
-    DWORD ExitCode;
-    if (DebugAlwaysTrue(GetExitCodeProcess(ProcessInformation.hProcess, &ExitCode)) &&
-        (ExitCode != 0))
-    {
-      UnicodeString Buf = Output;
-      UnicodeString Buf2;
-      if (ExtractMainInstructions(Buf, Buf2))
+      char Buffer[4096];
+      DWORD BytesRead;
+      while (ReadFile(PipeRead, Buffer, sizeof(Buffer), &BytesRead, NULL))
       {
-        throw ExtException(Output, UnicodeString(), HelpKeyword);
+        Output += UnicodeString(UTF8String(Buffer, BytesRead));
+        // Same as in ExecuteShellCheckedAndWait
+        Sleep(200);
+        Application->ProcessMessages();
       }
-      else
+
+      DWORD ExitCode;
+      if (DebugAlwaysTrue(GetExitCodeProcess(ProcessInformation.hProcess, &ExitCode)) &&
+          (ExitCode != 0))
       {
-        throw ExtException(MainInstructions(FMTLOAD(COMMAND_FAILED_CODEONLY, (static_cast<int>(ExitCode)))), Output, HelpKeyword);
+        UnicodeString Buf = Output;
+        UnicodeString Buf2;
+        if (ExtractMainInstructions(Buf, Buf2))
+        {
+          throw ExtException(Output, UnicodeString(), HelpKeyword);
+        }
+        else
+        {
+          throw ExtException(MainInstructions(FMTLOAD(COMMAND_FAILED_CODEONLY, (static_cast<int>(ExitCode)))), Output, HelpKeyword);
+        }
       }
     }
-  }
-  __finally
-  {
-    CloseHandle(ProcessInformation.hProcess);
-    CloseHandle(ProcessInformation.hThread);
+    __finally
+    {
+      CloseHandle(ProcessInformation.hProcess);
+      CloseHandle(ProcessInformation.hThread);
+    }
   }
 }
 //---------------------------------------------------------------------------