유돌이

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

2010. 1. 11. 14:37 델파이

프로그램을 띄우거나 파일을 실행할경우 ShellAPI 함수인 ShellExecute() 를 사용합니다.
이 함수는 윈도우즈 탐색기에서 파일을 선택하고 더블클릭하는 기능과 동일한 동작을 합니다.
다음은 ShellExecute() 의 몇가지 사용예입니다.

(1) 파일과 연관(association)된 프로그램으로 파일을 엽니다
    ShellExecute(Handle, 'open', PChar('test.txt'), nil, nil, SW_SHOW);

(2) notepad.exe 에 파라미터로 config.sys 파일을 주어 메모장을 실행합니다
    ShellExecute(Handle, 'open', 'notepad', 'c:\config.sys', nil, SW_SHOW);

(3) PC에 설치된 기본 웝브라우저로 지정한 사이트를 엽니다.
    ShellExecute(Handle, 'open', 'www.howto.pe.kr', nil, nil, SW_SHOW);

(4) 특정 폴더를 시작 폴더로 하는 윈도우즈 탐색기를 엽니다
    ShellExecute(Handle, 'explore', PChar('c:\windows)', nil, nil, SW_SHOW);

(5) readme.doc 파일을 연결된 프로그램으로 인쇄하고 화면을 닫습니다
    ShellExecute(Handle, 'print', 'readme.doc', nil, nil, SW_SHOW);
   
(6) rMyDelphiFile.pas 파일을 wordpad 프로그램으로 인쇄하고 화면을 닫습니다
    ShellExecute(Handle, 'print', 'wordpad.wxe', 'MyDelphiFile.pas', nil, SW_SHOW);

(7) readme.doc 파일을 프린터를 선택하여 연결된 프로그램으로 인쇄하고 화면을 닫습니다
    var
      Device : array[0..255] of char;
      Driver : array[0..255] of char;
      Port   : array[0..255] of char;
      S: String;
      hDeviceMode: THandle;
    begin
      Printer.PrinterIndex := -1;  // 프린터 인덱스를 지정합니다. 여기서는 기본 프린터(-1) 선택
      Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
      S := Format('"%s" "%s" "%s"',[Device, Driver, Port]);
      ShellExecute(Handle, 'printto', 'readme.doc', Pchar(S), nil, SW_HIDE);

(8) 기본 메일 프로그램을 실행합니다.
    ShellExecute(Handle, nil, 'mailto:cozy@howto.pe.kr', nil, nil, SW_SHOW);

(9) DOS 명령어를 실행하고 화면을 닫습니다
    ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);

(10) DOS 명령어를 실행하고 화면을 닫지 않습니다
     ShellExecute(Handle, 'open', PChar('command.com'), PChar('/k dir'), nil, SW_SHOW);

(11) ShellExecute()의 리턴값은 실행된 프로그램의 핸들이거나 에러코드입니다
     리턴값이 32 이하이면 에러가 발생한것으로 각각은 아래와 같은 의미가 있습니다

  var
    code: Integer;
  begin
    code := ShellExecute(...);
    if code <= 32 then ShowMessage(ShowShellExecuteError(code));
  end;
    
  // ShellExecute()의 리턴코드에 대한 에러 메시지
  function ShowShellExecuteError(i: integer): String;
  begin
    case i of 0: result := 'The operating system is out of memory or resources.';
      ERROR_FILE_NOT_FOUND: result := 'The specified file was not found.';
      ERROR_PATH_NOT_FOUND: result := 'The specified path was not found.';
      ERROR_BAD_FORMAT: result := 'The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).';
      SE_ERR_ACCESSDENIED: result := 'The operating system denied access to the specified file.';
      SE_ERR_ASSOCINCOMPLETE: result := 'The filename association is incomplete or invalid.';
      SE_ERR_DDEBUSY: result := 'The DDE transaction could not be completed because other DDE transactions were being processed.';
      SE_ERR_DDEFAIL: result := 'The DDE transaction failed.';
      SE_ERR_DDETIMEOUT: result := 'The DDE transaction could not be completed because the request timed out.';
      SE_ERR_DLLNOTFOUND: result := 'The specified dynamic-link library was not found.';
      //SE_ERR_FNF          : result:='The specified file was not found.';
      SE_ERR_NOASSOC           : result:='Unbekannter Extender.';
      SE_ERR_OOM: result := 'There was not enough memory to complete the operation.';
      //SE_ERR_PNF          : result:='The specified path was not found.';
      SE_ERR_SHARE: result := 'A sharing violation occurred.';
    end;
  end;

(12) ShellExecuteEX()를 이용하여 notepad.exe 를 실행한 후 종료될때까지 기다립니다
  var
    SEInfo: TShellExecuteInfo;
    ExitCode: DWORD;
    ExecuteFile, ParamString, StartInString: string;
  begin
    ExecuteFile   := 'notepad.exe';   // 실행할 프로그램
    ParamString   := 'c:\winzip.log'; // 프로그램의 명령행 파라미터
    StartInString := 'c:\';           // 시작 위치
    FillChar(SEInfo, SizeOf(SEInfo), 0);
    SEInfo.cbSize := SizeOf(TShellExecuteInfo);

    with SEInfo do
    begin
      fMask        := SEE_MASK_NOCLOSEPROCESS;
      Wnd          := Application.Handle;
      lpFile       := PChar(ExecuteFile);
      lpParameters := PChar(ParamString);
      lpDirectory  := PChar(StartInString);
      nShow        := SW_SHOWNORMAL;
    end;
    if ShellExecuteEX(@SEInfo) then
    begin
      repeat
        Application.ProcessMessages;
        GetExitCodeProcess(SEInfo.hProcess, ExitCode);
      until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
      ShowMessage('프로그램이 종료되었습니다');
    end
    else ShowMessage('프로그램을 실행할 수 없습니다');

 

출처 : http://www.howto.pe.kr 작성자 : 김영대님!


posted by 유돌이