MFC
2009.09.01 10:27

도스 커맨드 실행하기

조회 수 48997 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
31 MFC CString 에서 형변환 함수들 총정리 2010.11.29 102653 0
30 MFC API를 이용하는 유니코드와 ANSI 문자열간의 변환 방법 2006.04.14 63028 0
29 MFC 현재디렉토리의 파일리스트들을 알아오는 클래스 CFindFile 2008.05.07 62374 0
28 MFC MFC에서 커맨드 인자(cmd argument) 사용하기 2010.12.13 61478 0
27 MFC 폴더가 존재하는 지 확인하고, 없으면 만드는 함수 2010.02.01 58303 0
26 MFC CFileDialog 초기 폴더 지정 2010.08.11 57071 0
25 MFC CString을 유니코드로 변환 WCHAR에 저장하는 방법 1 2006.04.14 56673 0
24 MFC Cstring methods 2009.04.21 54866 0
23 MFC 파일 읽기 전용 해제 및 설정 2009.09.11 54157 0
22 MFC 시스템 출력 리디렉션 - 도스 커맨드 결과 받아오기 file 2007.08.14 52783 0
21 MFC [Collection] CMap 템플릿 클래스 사용하기 2010.06.23 50418 0
» MFC 도스 커맨드 실행하기 1 2009.09.01 48997 0
19 MFC [C] Unicode 사용에 대하여 2006.04.14 47035 0
18 MFC MFC에서 인자(argument)처리 2005.10.28 46966 0
17 MFC MFC 클래스간 포인터 얻기 2010.06.01 46847 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