MFC
2009.09.01 10:27

도스 커맨드 실행하기

조회 수 49039 댓글 1
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

커맨드창을 띄워서 도스 커맨드를 실행하는 두가지 함수.
자꾸 잊어버리는구먼.

1.
system("cls");

2.
ShellExecute(NULL, "open", "iexplore.exe", "http://wiki.lge.com/lab3/autokeypad", NULL, SW_SHOWNORMAL);

3.
#include <shellapi.h>

void KShellExecute(HWND handle, LPCTSTR exe, LPCTSTR param, LPCTSTR dir)
{
    DWORD ExitCode;
    SHELLEXECUTEINFO SEInfo;

    memset( &SEInfo, 0, sizeof(SEInfo));
    SEInfo.cbSize = sizeof(SHELLEXECUTEINFO);

    SEInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    SEInfo.hwnd = handle;
    SEInfo.lpFile = exe;
    SEInfo.lpParameters = param;
    SEInfo.lpDirectory = dir;
    SEInfo.nShow = SW_HIDE; // SW_SHOWNORMAL;


    if (ShellExecuteEx(&SEInfo)==TRUE)
    {
        do
        {
            GetExitCodeProcess(SEInfo.hProcess, &ExitCode);
            Sleep(500);
        } while (ExitCode); // wait until the command is finished
    }
}

  • Dreamy 2009.11.27 17:02
    ShellExecute(Ex) 사용법 예제 12가지

    프로그램을 띄우거나 파일을 실행할경우 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('프로그램을 실행할 수 없습니다');

Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
16 MFC MFC app 디버깅 출력을 위한 콘솔 생성하기 2009.09.01 42927 0
15 MFC 커맨드 창 속성 제어 2009.09.01 37886 0
» MFC 도스 커맨드 실행하기 1 2009.09.01 49039 0
13 MFC Cstring methods 2009.04.21 54918 0
12 MFC 비트맵 비교 2009.03.23 40812 0
11 MFC 현재디렉토리의 파일리스트들을 알아오는 클래스 CFindFile 2008.05.07 62448 0
10 MFC 현재 실행된 어플리케이션의 디렉토리 적용하기 2008.05.07 39883 0
9 MFC 리사이징 다이얼로그(Resizing dialog) 2008.03.24 46616 0
8 MFC 시스템 출력 리디렉션 - 도스 커맨드 결과 받아오기 file 2007.08.14 52867 0
7 MFC 노트패드를 이용한 덤프 file 2006.05.19 33501 0
6 MFC 시작프로그램 레지스트리에 등록/해제 함수 2006.04.14 45687 0
5 MFC [C] Unicode 사용에 대하여 2006.04.14 47079 0
4 MFC API를 이용하는 유니코드와 ANSI 문자열간의 변환 방법 2006.04.14 63086 0
3 MFC CString을 유니코드로 변환 WCHAR에 저장하는 방법 1 2006.04.14 56713 0
2 MFC 다이얼로그 기반 APP에서 Edit에 엔터키 먹게 하기 2006.04.14 42091 0
목록
Board Pagination ‹ Prev 1 2 3 Next ›
/ 3

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5