MFC
2009.09.01 10:27

도스 커맨드 실행하기

조회 수 49045 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
71 MFC CFileDialog 초기 폴더 지정 2010.08.11 57128 0
70 MFC CTree Ctrl 맴버 요약 2010.06.24 42024 0
69 MFC [Collection] CList 템플릿 클래스 사용하기 2010.06.24 46823 0
68 MFC [Collection] CMap 템플릿 클래스 사용하기 2010.06.23 50473 0
67 MFC MFC 클래스간 포인터 얻기 2010.06.01 46902 0
66 일반 배치파일(bat)에서 for루프 사용법 2010.04.15 47631 0
65 C# C# 레지스트리 사용하기 2010.03.15 40399 0
64 C# 디렉토리 경로 입력 받기 2010.03.15 38400 0
63 Python python을 이용한 프로그램 개발 및 배포 방안 2010.02.25 46045 0
62 MFC 폴더가 존재하는 지 확인하고, 없으면 만드는 함수 2010.02.01 58380 0
61 일반 ClearCase 팁 1 2009.11.26 38796 0
60 Python Python Embedding (MFC프로그램에서 파이썬 실행하기) 2009.11.14 47684 0
59 MFC 파일 읽기 전용 해제 및 설정 2009.09.11 54206 0
58 MFC MFC app 디버깅 출력을 위한 콘솔 생성하기 2009.09.01 42929 0
57 MFC 커맨드 창 속성 제어 2009.09.01 37898 0
목록
Board Pagination ‹ Prev 1 ... 25 26 27 28 29 30 31 32 33 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5