MFC
2009.09.01 10:27

도스 커맨드 실행하기

조회 수 49169 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
476 일반 PROMPT Example secret 2023.05.22 0 0
475 Android Flutter 문법 정리 secret 2023.09.24 0 0
474 Android 원격 linux 서버에서 local device로 adb 접속하기 secret 2015.11.05 1 0
473 Pi 회로부품 메모 1 secret 2019.08.09 1 0
472 Android [GIT 사용법] Dreamy의 요약 2 secret 2012.04.20 158 0
471 Android 대화상자 만들기 메모 2023.11.01 707 0
470 PHP [MySQL] 뷰 생성하기(VIEW 생성하기) 2022.10.12 1091 0
469 HTML5 TypeScript 문법 정리 2023.01.13 1167 0
468 PHP sQL:: JOIN, UNION 사용법 2022.09.05 1178 0
467 HTML5 [TypeORM] TypeORM CreateQueryBuilder 2023.01.13 1363 0
466 JAVA [JAVA] 람다식 기본 예제 1 (map, filter, reduce, collect) 2022.11.04 1364 0
465 HTML5 typescript 기본문법 정리 2023.01.13 1365 0
464 PHP SQL :: EXIST , NOT EXIST 2022.09.05 1485 0
463 업무 Back-End 관련 정보 2022.05.17 1615 0
462 일반 3D프린트 가이드 모음 2022.04.02 2332 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5