::: µ¨ÆÄÀÌ Tip&Trick :::

µ¨ÆÄÀÌ Tip&Trick ¼º°Ý¿¡ ¸ÂÁö ¾Ê´Â ±¤°í,ºñ¹æ,Áú¹®ÀÇ ±ÛÀº Áï½Ã »èÁ¦Çϸç
³»¿ëÀ» º¹»çÇÏ¿© »ç¿ëÇÒ °æ¿ì ¹Ýµå½Ã À̰÷(http://www.howto.pe.kr)À» Ãâó·Î ¸í½ÃÇÏ¿© ÁÖ¼¼¿ä


Category

  ±è¿µ´ë(2003-03-06 21:36:25, Hit : 4492, Vote : 1207
 Redirecting DOS Application Output

// A function to execute a DOS or Win32 consoloe mode application and wait for it to close before continuing.
// Input for the app can be directed from a file, and the output will be redirected to a file.

uses
  Controls, Windows, SysUtils, Forms;

{---------------------------CreateDOSProcessRedirected--------------------------
Description    : executes a (DOS!) app defined in the CommandLine parameter
                  redirected to take input from InputFile (optional) and give
                  output to OutputFile
Result         : True on success
Parameters     : CommandLine : the command line for app, including full path
                  InputFile   : the ascii file where from the app takes input,
                                 empty if no input needed/required.
                  OutputFile  : the ascii file to which the 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 (InputFile <> '') and (not FileExists(InputFile)) then
    raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
       'Input file * %s *' + #10 +
       'does not exist' + #10 + #10 +
       ErrMsg, [InputFile]);

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

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

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

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

    if InputFile <> '' then
    begin
      { 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_NORMAL
         or FILE_FLAG_WRITE_THROUGH,          { file attributes }
         0);                                 { handle to file with attrs 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]);
    end else
      { we aren't using an input file }
      hInputFile := 0;

    { 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_NORMAL
       or FILE_FLAG_WRITE_THROUGH,          { file attributes }
       0 );                                 { handle to file with attrs 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 }
       HIGH_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; { CreateDOSProcessRedirected }





611   [µ¥ÀÌÅͺ£À̽º] DB¿¡ ÀúÀåµÈ JPEG(JPG)¸¦ DBGrid¿¡ Ãâ·ÂÇÏ±â  ±è¿µ´ë 2003/03/06 5233 1252
610   [µ¥ÀÌÅͺ£À̽º] µ¿ÀûÀ¸·Î À妽º ¸¸µé±â  ±è¿µ´ë 2003/03/06 4455 1128
609   [µ¥ÀÌÅͺ£À̽º] Excel ODBC¸¦ »ç¿ëÇÏ¿© xls¸¦ Å×À̺í·Î »ç¿ëÇÏ±â  ±è¿µ´ë 2003/03/06 6844 1592
608   [µ¥ÀÌÅͺ£À̽º] DB aliasÁß ORACLE alias ¸í ±¸ÇÏ±â  ±è¿µ´ë 2003/03/06 4000 971
607   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] QuickReport¿¡¼­ ÇÁ¸°ÅÍ ¹Ù²Ù¾î¼­ Ãâ·ÂÇÏ±â  ±è¿µ´ë 2003/03/06 6215 1540
606   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] RichEdit ÀÇ ³»¿ëÀ» Bitmap À¸·Î ¸¸µé±â  ±è¿µ´ë 2003/03/06 3341 870
605   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] RichEdit ¿¡¼­ ¹®ÀÚ(¿­)¸¦ ã¾Æ ±ÛÀÚ¼Ó¼º ¹Ù²Ù±â  ±è¿µ´ë 2003/03/06 4727 1212
604   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] RichEdit ¿¡¼­ Ä¿¼­¸¦ óÀ½, ¸¶Áö¸·À¸·Î º¸³»±â  ±è¿µ´ë 2003/03/06 6868 1474
603   [À©µµ¿ìÁî API] ½ÇÇàÁßÀÎ ¸ðµç ÇÁ·Î±×·¥ Minimized ½ÃŰ±â  ±è¿µ´ë 2003/03/06 4647 1231
602   [½Ã½ºÅÛ] µ¨ÆÄÀÌ·Î DOS ÇÁ·Î±×·¥(Console application) ¸¸µé±â  ±è¿µ´ë 2003/03/06 17031 7521
601   [COM/OLE] Registering *.tlb files without Delphi  ±è¿µ´ë 2003/03/06 4981 1129
600   [³×Æ®¿÷/ÀÎÅͳÝ] How to bring a network down - "Win Nuke"  ±è¿µ´ë 2003/03/06 7286 1991
599   [À©µµ¿ìÁî API] Æú´õ³ª ÆÄÀÏÀÇ À©µµ¿ìÁî µî·ÏÁ¤º¸ dialog ¶ç¿ì±â  ±è¿µ´ë 2003/03/06 5085 1473
598   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] À̹ÌÁö¸¦ ¸¶¿ì½º·Î drag ÇØ¼­ zoom ÇÏ±â  ±è¿µ´ë 2003/03/06 3599 1023
597   [À©µµ¿ìÁî API] ¿ÜºÎ ÇÁ·Î±×·¥À» ÃÖ»óÀ§·Î ¼³Á¤ÇÏ±â  ±è¿µ´ë 2003/03/06 5213 1232
596   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] TObjectÀÇ ÇÁ·ÎÆÛƼ¸¦ ¹®ÀÚ¿­·Î ÂüÁ¶ÇÏ±â  ±è¿µ´ë 2003/03/06 5216 1591
595   [À©µµ¿ìÁî API] ÇÁ·Î±×·¥À¸·Î Screensaver µî·ÏÇÏ´Â µÎ°¡Áö ¹æ¹ý  ±è¿µ´ë 2003/03/06 4480 1267
594   [µ¥ÀÌÅͺ£À̽º] ƯÁ¤ ÆûÀÇ ÇöÀç ÆíÁýÁßÀÎ DB Field ±¸ÇÏ±â  ±è¿µ´ë 2003/03/06 4082 1110
593   [À©µµ¿ìÁî API] KeyDownÀÇ BeepÀ½À» ¾ø¾ÖÀÚ...  ±è¿µ´ë 2003/03/06 4614 1210
592   [½Ã½ºÅÛ] How do I use SetWindowsHookEx ?  ±è¿µ´ë 2003/03/06 6803 954
  [½Ã½ºÅÛ] Redirecting DOS Application Output  ±è¿µ´ë 2003/03/06 4492 1207
590   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] µÎ°³ÀÇ RichEdit »çÀÌ¿¡ ³»¿ë º¹»çÇÏ±â  ±è¿µ´ë 2003/03/06 5844 1364
589   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] ƯÁ¤ ColorÀÇ Invert Color ±¸ÇÏ±â  ±è¿µ´ë 2003/03/06 4309 1315
588   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] ¹®ÀÚ¿­ ¼ö½Ä¹®Àå(expression)ÀÇ °á°ú ±¸ÇÏ±â  ±è¿µ´ë 2003/03/06 3331 873
587   [½Ã½ºÅÛ] ¿Àµð¿À CDÀÇ º¼·ý Á¶ÀýÇÏ±â  ±è¿µ´ë 2003/03/06 3601 1025
586   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] À©µµ¿ìÁî "³¯Â¥/½Ã°£" ¼³Á¤È­¸é ¶ç¿ì±â  ±è¿µ´ë 2003/03/06 6194 1822
585   [½Ã½ºÅÛ] ¸¶ÀÌÅ© º¼·ý Á¶ÀýÇÏ±â  ±è¿µ´ë 2003/03/06 4630 1284
584   [¸ÖƼ¹Ìµð¾î] JPEG, WAVE ¸¦ resource ÆÄÀÏ¿¡ ³Ö°í Àоî¿À±â  ±è¿µ´ë 2003/03/06 5238 1223
583   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] desktop ¹è°æÈ­¸éÀ» ÆûÀÇ ¹è°æÈ­¸éÀ¸·Î ±×¸®±â  ±è¿µ´ë 2003/03/06 3668 1037
582   [À©µµ¿ìÁî API] ·¹Áö½ºÆ®¸®ÀÇ º¯°æ¿©ºÎ ¾Ë¸®´Â 2°¡Áö ¹æ¹ý  ±è¿µ´ë 2003/03/06 4750 1279
581   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] StringGrid ÀÇ ³»¿ëÀ» Ŭ¸³º¸µå·Î º¹»çÇÏ±â  ±è¿µ´ë 2003/03/06 4454 1060
580   [µ¥ÀÌÅͺ£À̽º] Save DBGrid To Excel  ±è¿µ´ë 2003/03/06 6964 1797
579   [COM/OLE] DelphiÀÇ OCX¸¦ InstallShield·Î ¹èÆ÷ÇÏ´Â ¹æ¹ý  ±è¿µ´ë 2003/03/06 6753 5616
578   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] ASCII printing  ±è¿µ´ë 2003/03/06 5271 1211
577   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] OEM conversion  ±è¿µ´ë 2003/03/06 4310 1262
576   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] MemoÀÇ ÇàÀÇ ¹®ÀÚ¼ö¸¦ Á¦ÇÑÇϰí WordWrap½ÃŰ±â  ±è¿µ´ë 2003/03/06 5218 1314
575   [À©µµ¿ìÁî API] DDE ¾²Áö ¾Ê°í IEÀÇ ÇöÀç URL °¡Á®¿À±â  ±è¿µ´ë 2003/03/06 5976 1673
574   [À©µµ¿ìÁî API] RichEdit¿¡ ÀÔ·ÂÇÑ ¹®ÀåÀÇ ½ÇÁ¦ ³ôÀÌ ±¸ÇÏ±â  ±è¿µ´ë 2003/03/06 5027 1426
573   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] StringGrid ÀÇ ¼±Åÿµ¿ª¸¸ Ŭ¸³º¸µå·Î º¹»çÇÏ±â  ±è¿µ´ë 2003/03/06 4840 1075
572   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] StringGrid ¿¡¼­ ÇÁ·Î±×·¥À¸·Î MultiSelect ½ÃŰ±â  ±è¿µ´ë 2003/03/06 5387 1187

[1][2][3][4][5][6][7][8][9] 10 ..[25] [´ÙÀ½ 10°³]
 

Copyright 1999-2022 Zeroboard / skin by zero