home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue66 / Construc / Refactor / Source / DOSToMemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-11-12  |  3.9 KB  |  91 lines

  1. unit DOSToMemo;
  2. interface
  3. function CreateDOSProcessRedirected(const CommandLine,InputFile,OutputFile,
  4.                                     ErrMsg :string):boolean;
  5. { from a discussion at  http://www.experts-exchange.com, posted by jeurk}
  6. {I've made not changes and there are several warnings issued when this is compiled}
  7.  
  8. implementation
  9. uses Forms, Windows, sysutils, controls;
  10.  
  11. function CreateDOSProcessRedirected(const CommandLine,InputFile,OutputFile,
  12.                                     ErrMsg :string):boolean;
  13. const
  14.   ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
  15. var
  16.   OldCursor     : TCursor;
  17.   pCommandLine  : array[0..MAX_PATH] of char;
  18.   pInputFile,
  19.   pOutPutFile   : array[0..MAX_PATH] of char;
  20.   StartupInfo   : TStartupInfo;
  21.   ProcessInfo   : TProcessInformation;
  22.   SecAtrrs      : TSecurityAttributes;
  23.   hAppProcess,
  24.   hAppThread,
  25.   hInputFile,
  26.   hOutputFile   : THandle;
  27. begin
  28.   Result := False;
  29.   if not FileExists(InputFile) then
  30.     raise Exception.CreateFmt(ROUTINE_ID+#10+#10+'Input file * %s *'+#10+
  31.                               'does not exist'+#10+#10+ErrMsg,[InputFile]);
  32.   OldCursor     := Screen.Cursor;
  33.   Screen.Cursor := crHourglass; 
  34.   StrPCopy(pCommandLine, CommandLine); 
  35.   StrPCopy(pInputFile, InputFile); 
  36.   StrPCopy(pOutPutFile, OutputFile); 
  37.   try 
  38.     FillChar(SecAtrrs, SizeOf(SecAtrrs), #0); 
  39.     SecAtrrs.nLength              := SizeOf(SecAtrrs); 
  40.     SecAtrrs.lpSecurityDescriptor := nil; 
  41.     SecAtrrs.bInheritHandle       := True; 
  42.     hInputFile := CreateFile(pInputFile,GENERIC_READ or GENERIC_WRITE, 
  43.                              FILE_SHARE_READ or FILE_SHARE_WRITE,@SecAtrrs, 
  44.                              OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL 
  45.                              or FILE_FLAG_WRITE_THROUGH,0); 
  46.     if hInputFile = INVALID_HANDLE_VALUE then 
  47.       raise Exception.CreateFmt(ROUTINE_ID+#10+#10+
  48.                                 'WinApi function CreateFile returned an' + 
  49.                                 'invalid handle value'  + #10 + 
  50.                                 'for the input file * %s *' + #10 + #10 + 
  51.                                 ErrMsg, [InputFile]);
  52.     hOutputFile := CreateFile(pOutPutFile,GENERIC_READ or GENERIC_WRITE,
  53.                               FILE_SHARE_READ or FILE_SHARE_WRITE,@SecAtrrs,
  54.                               CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL 
  55.                               or FILE_FLAG_WRITE_THROUGH,0); 
  56.     if hOutputFile = INVALID_HANDLE_VALUE then 
  57.       raise Exception.CreateFmt(ROUTINE_ID+#10+#10+ 
  58.                                 'WinApi function CreateFile returned an' + 
  59.                                 'invalid handle value'  + #10 + 
  60.                                 'for the output file * %s *' + #10 + #10 + 
  61.                                 ErrMsg, [OutputFile]); 
  62.     FillChar(StartupInfo, SizeOf(StartupInfo), #0); 
  63.     StartupInfo.cb          := SizeOf(StartupInfo); 
  64.     StartupInfo.dwFlags     := STARTF_USESHOWWINDOW or 
  65.                                STARTF_USESTDHANDLES; 
  66.     StartupInfo.wShowWindow := SW_HIDE; 
  67.     StartupInfo.hStdOutput  := hOutputFile;
  68.     StartUpInfo.hStdError   := hOutputFile;
  69.     StartupInfo.hStdInput   := hInputFile; 
  70.     Result := CreateProcess(nil,pCommandLine,nil,nil,True,HIGH_PRIORITY_CLASS, 
  71.                             nil,nil,StartupInfo,ProcessInfo); 
  72.     if Result then 
  73.     begin 
  74.       WaitforSingleObject(ProcessInfo.hProcess, INFINITE); 
  75.       hAppProcess  := ProcessInfo.hProcess; 
  76.       hAppThread   := ProcessInfo.hThread; 
  77.     end 
  78.     else 
  79.       raise Exception.Create(ROUTINE_ID+#10+#10+ 
  80.                              'Function failure'+#10+#10+ErrMsg); 
  81.   finally 
  82.     if hOutputFile <> 0 then CloseHandle(hOutputFile); 
  83.     if hInputFile <> 0 then CloseHandle(hInputFile); 
  84.     if hAppThread <> 0 then CloseHandle(hAppThread); 
  85.     if hAppProcess <> 0 then CloseHandle(hAppProcess); 
  86.     Screen.Cursor:= OldCursor; 
  87.   end; 
  88. end;    { CreateDOSProcessRedirected } 
  89.  
  90. end.
  91.