home *** CD-ROM | disk | FTP | other *** search
- unit DOSToMemo;
- interface
- function CreateDOSProcessRedirected(const CommandLine,InputFile,OutputFile,
- ErrMsg :string):boolean;
- { from a discussion at http://www.experts-exchange.com, posted by jeurk}
- {I've made not changes and there are several warnings issued when this is compiled}
-
- implementation
- uses Forms, Windows, sysutils, controls;
-
- function CreateDOSProcessRedirected(const CommandLine,InputFile,OutputFile,
- ErrMsg :string):boolean;
- const
- ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
- var
- OldCursor : TCursor;
- pCommandLine : array[0..MAX_PATH] of char;
- pInputFile,
- pOutPutFile : array[0..MAX_PATH] of char;
- StartupInfo : TStartupInfo;
- ProcessInfo : TProcessInformation;
- SecAtrrs : TSecurityAttributes;
- hAppProcess,
- hAppThread,
- hInputFile,
- hOutputFile : THandle;
- begin
- Result := False;
- if not FileExists(InputFile) then
- raise Exception.CreateFmt(ROUTINE_ID+#10+#10+'Input file * %s *'+#10+
- 'does not exist'+#10+#10+ErrMsg,[InputFile]);
- OldCursor := Screen.Cursor;
- Screen.Cursor := crHourglass;
- StrPCopy(pCommandLine, CommandLine);
- StrPCopy(pInputFile, InputFile);
- StrPCopy(pOutPutFile, OutputFile);
- try
- FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
- SecAtrrs.nLength := SizeOf(SecAtrrs);
- SecAtrrs.lpSecurityDescriptor := nil;
- SecAtrrs.bInheritHandle := True;
- hInputFile := CreateFile(pInputFile,GENERIC_READ or GENERIC_WRITE,
- FILE_SHARE_READ or FILE_SHARE_WRITE,@SecAtrrs,
- OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL
- or FILE_FLAG_WRITE_THROUGH,0);
- if hInputFile = INVALID_HANDLE_VALUE then
- raise Exception.CreateFmt(ROUTINE_ID+#10+#10+
- 'WinApi function CreateFile returned an' +
- 'invalid handle value' + #10 +
- 'for the input file * %s *' + #10 + #10 +
- ErrMsg, [InputFile]);
- hOutputFile := CreateFile(pOutPutFile,GENERIC_READ or GENERIC_WRITE,
- FILE_SHARE_READ or FILE_SHARE_WRITE,@SecAtrrs,
- CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL
- or FILE_FLAG_WRITE_THROUGH,0);
- if hOutputFile = INVALID_HANDLE_VALUE then
- raise Exception.CreateFmt(ROUTINE_ID+#10+#10+
- 'WinApi function CreateFile returned an' +
- 'invalid handle value' + #10 +
- 'for the output file * %s *' + #10 + #10 +
- ErrMsg, [OutputFile]);
- FillChar(StartupInfo, SizeOf(StartupInfo), #0);
- StartupInfo.cb := SizeOf(StartupInfo);
- StartupInfo.dwFlags := STARTF_USESHOWWINDOW or
- STARTF_USESTDHANDLES;
- StartupInfo.wShowWindow := SW_HIDE;
- StartupInfo.hStdOutput := hOutputFile;
- StartUpInfo.hStdError := hOutputFile;
- StartupInfo.hStdInput := hInputFile;
- Result := CreateProcess(nil,pCommandLine,nil,nil,True,HIGH_PRIORITY_CLASS,
- nil,nil,StartupInfo,ProcessInfo);
- if Result then
- begin
- WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
- hAppProcess := ProcessInfo.hProcess;
- hAppThread := ProcessInfo.hThread;
- end
- else
- raise Exception.Create(ROUTINE_ID+#10+#10+
- 'Function failure'+#10+#10+ErrMsg);
- finally
- if hOutputFile <> 0 then CloseHandle(hOutputFile);
- if hInputFile <> 0 then CloseHandle(hInputFile);
- if hAppThread <> 0 then CloseHandle(hAppThread);
- if hAppProcess <> 0 then CloseHandle(hAppProcess);
- Screen.Cursor:= OldCursor;
- end;
- end; { CreateDOSProcessRedirected }
-
- end.
-