Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
KANSoftWare

Получить выходные данные DOS приложения

Delphi , ОС и Железо , DOS и Консоль

Получить выходные данные DOS приложения

Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch

{----------------------------CreateDOSProcessRedirected--------------------------- 
 Description    : executes a (DOS!) app defined in the CommandLine parameter redirected 
                  to take input from InputFile and give output to OutputFile 
 Result         : True on success 
 Parameters     : 
                  CommandLine : the command line for the app, including its full path 
                  InputFile   : the ascii file where from the app takes input 
                  OutputFile  : the ascii file to which the app's output is redirected 
                  ErrMsg      : additional error message string. Can be empty 
 Error checking : YES 
 Target         : Delphi 2, 3, 4 
 Author         : Theodoros Bebekis, email bebekis@otenet.gr 
 Notes          : 
 Example call   : CreateDOSProcessRedirected('C:\MyDOSApp.exe', 
                                             'C:\InputPut.txt', 
                                             'C:\OutPut.txt', 
                                             'Please, record this message') 
-----------------------------------------------------------------------------------}

 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;

   { check for InputFile existence }
   if not FileExists(InputFile) then
     raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
       'Input file * %s *' + #10 +
       'does not exist' + #10 + #10 +
       ErrMsg, [InputFile]);

   { save the cursor }
   OldCursor     := Screen.Cursor;
   Screen.Cursor := crHourglass;

   { copy the parameter Pascal strings to null terminated strings }
   StrPCopy(pCommandLine, CommandLine);
   StrPCopy(pInputFile, InputFile);
   StrPCopy(pOutPutFile, OutputFile);

   try

     { prepare SecAtrrs structure for the CreateFile calls 
      This SecAttrs structure is needed in this case because 
      we want the returned handle can be inherited by child process 
      This is true when running under WinNT. 
      As for Win95 the documentation is quite ambiguous }
     FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
     SecAtrrs.nLength        := SizeOf(SecAtrrs);
     SecAtrrs.lpSecurityDescriptor := nil;
     SecAtrrs.bInheritHandle := True;

     { create the appropriate handle for the input file }
     hInputFile := CreateFile(pInputFile,
       { pointer to name of the file }
       GENERIC_READ or GENERIC_WRITE,
       { access (read-write) mode }
       FILE_SHARE_READ or FILE_SHARE_WRITE,
       { share mode } @SecAtrrs,                             { pointer to security attributes }
       OPEN_ALWAYS,                           { how to create }
       FILE_ATTRIBUTE_TEMPORARY,              { file attributes }
       0);                                   { handle to file with attributes to copy }


     { is hInputFile a valid handle? }
     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]);

     { create the appropriate handle for the output file }
     hOutputFile := CreateFile(pOutPutFile,
       { pointer to name of the file }
       GENERIC_READ or GENERIC_WRITE,
       { access (read-write) mode }
       FILE_SHARE_READ or FILE_SHARE_WRITE,
       { share mode } @SecAtrrs,                             { pointer to security attributes }
       CREATE_ALWAYS,                         { how to create }
       FILE_ATTRIBUTE_TEMPORARY,              { file attributes }
       0);                                   { handle to file with attributes to copy }

     { is hOutputFile a valid handle? }
     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]);

     { prepare StartupInfo structure }
     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;

     { create the app }
     Result := CreateProcess(nil,                           { pointer to name of executable module }
       pCommandLine,
       { pointer to command line string }
       nil,                           { pointer to process security attributes }
       nil,                           { pointer to thread security attributes }
       True,                          { handle inheritance flag }
       CREATE_NEW_CONSOLE or
       REALTIME_PRIORITY_CLASS,       { creation flags }
       nil,                           { pointer to new environment block }
       nil,                           { pointer to current directory name }
       StartupInfo,                   { pointer to STARTUPINFO }
       ProcessInfo);                  { pointer to PROCESS_INF }

     { wait for the app to finish its job and take the handles to free them later }
     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
     { close the handles 
      Kernel objects, like the process and the files we created in this case, 
      are maintained by a usage count. 
      So, for cleaning up purposes we have to close the handles 
      to inform the system that we don't need the objects anymore }
     if hOutputFile <> 0 then CloseHandle(hOutputFile);
     if hInputFile <> 0 then CloseHandle(hInputFile);
     if hAppThread <> 0 then CloseHandle(hAppThread);
     if hAppProcess <> 0 then CloseHandle(hAppProcess);
     { restore the old cursor }
     Screen.Cursor := OldCursor;
   end;
 end;

Приведенный код - функция Delphi, называемая CreateDOSProcessRedirected, которая запускает DOS-приложение, перенаправляет его ввод из файла и вывод в другой файл, и ожидает завершения приложения.

Вот шаг за шагом, что функция делает:

  1. Она проверяет, существует ли файл ввода, и если нет, то бросает исключение.
  2. Она сохраняет текущую позицию курсора и изменяет ее на часовой курсор, пока процесс не будет запущен.
  3. Она копирует командную строку, имя файла ввода и имя файла вывода из строк Pascal в null-terminated строки.
  4. Она готовит структуру SecurityAttributes для использования с функцией CreateFile, чтобы создать handles для файлов ввода и вывода.
  5. Она создает handles для файлов ввода и вывода с помощью функции CreateFile.
  6. Она проверяет, являются ли handles валидными, и если нет, то бросает исключение.
  7. Она готовит структуру StartupInfo для использования с функцией CreateProcess, чтобы запустить процесс DOS-приложения.
  8. Она создает процесс DOS-приложения с помощью функции CreateProcess, передавая в нее командную строку, handle ввода, handle вывода и другие параметры.
  9. Если создание процесса успешно, она ожидает завершения процесса с помощью функции WaitForSingleObject.
  10. Наконец, она закрывает все handles, созданные во время выполнения функции.

Функция возвращает булевое значение, указывающее, была ли выполнена операция успешно или нет.

Чтобы получить код выхода DOS-приложения, можно использовать API-функцию GetExitCodeProcess, которая не показана в этом фрагменте кода.

Вот некоторые предложения по улучшению:

  • Рассмотрите использование исключений вместо сообщений об ошибках. Это позволит вам ловить и обрабатывать конкретные ошибки более эффективно.
  • Возможно, вы хотели бы использовать более надежный способ обработки файловой системы, например, с помощью блоков try-except или встроенной функциональности Delphi для работы с файлами.
  • Если функция предназначена для использования в многопоточной среде, вам нужно убедиться, что все handlesproperly закрыты и освобождены, когда они больше не нужны, чтобы избежать ресурсных утечек.

Замечание: код appears to be from an older version of Delphi (2, 3 or 4), which may require modifications to work with newer versions of the compiler.

Функция CreateDOSProcessRedirected позволяет запустить DOS-приложение,.redirecting его вывод в файл и ввод от файла.


Комментарии и вопросы

Получайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш Telegram-канал delphi_kansoftware и будьте в курсе последних тенденций в разработке под Linux, Windows, Android и iOS




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.


:: Главная :: DOS и Консоль ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-12-22 20:14:06
2025-07-09 10:21:31/0.00689697265625/0