Sayfayı Yazdır | Pencereyi Kapat


Dos Programını Çalıştırıp, input ve output'unu Yonlendirmek

Kategori: DELPHI - 7
Eklenme Tarihi: 27 Haziran 2005
Zaman: 18 Mayıs 2012 22:41
Samanalevi Netkafe - http://www.samanalevi.com/bilgisayaroku/117.Dos_Programini_CaliStirip_input_ve_outputunu_Yonlendirmek.html

Dos Programini calistirip, input ve output'unu yonlendirmek ve bitene kadar beklemek
{InputFile '' ise Dos programi klavyeden okuma yapar}

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 (InputFile <> '') and (not FileExists(InputFile)) then
    raise Exception.CreateFmt(ROUTINE_ID + #10#10 +
       '* %s *' + #10 + 'does not exist' + #10#10 +
       ErrMsg, [InputFile]);

hAppProcess := 0;
hAppThread := 0;
hInputFile := 0;
hOutputFile := 0;

OldCursor     := Screen.Cursor;
Screen.Cursor := crHourglass;

try
    StrPCopy(pCommandLine, CommandLine);
    StrPCopy(pInputFile, InputFile);
    StrPCopy(pOutPutFile, OutputFile);

    FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
    SecAtrrs.nLength              := SizeOf(SecAtrrs);
    SecAtrrs.lpSecurityDescriptor := nil;
    SecAtrrs.bInheritHandle       := TRUE;

    if InputFile <> '' then
    begin
       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 returned an invalid handle value' +
        #10 +'for the input file * %s *' + #10 + #10 +
        ErrMsg, [InputFile]);
    end else
      hInputFile := 0;

    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 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.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;

// Kullanimi:
procedure TForm1.Button2Click(Sender: TObject);
begin
CreateDOSProcessRedirected('chkdsk c:\', '', 'C:\sil.txt', '');
Memo1.Lines.LoadFromFile('C:\sil.txt');
end;

DELPHI - 7


Sayfayı Yazdır | Pencereyi Kapat 

http://www.samanalevi.com