logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

[Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드

by digipine posted Nov 02, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
Win32에서 도스 명령어를 실행하고 결과 값을 UniCode 스트링으로 가져오는 샘플 코드입니다.
아래 소스 코드를 참조하세요.
 
LPWSTR CHARtoWCHAR(LPSTR str, UINT codePage) {
    size_t len = strlen(str) + 1;
    int size_needed = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
    LPWSTR wstr = (LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR) * size_needed);
    MultiByteToWideChar(codePage, 0, str, len, wstr, size_needed);
    return wstr;
}
 
LPSTR UNICODEtoANSI(CString strData)
{
    LPWSTR  lpData = strData.GetBuffer(0);
    LPSTR  lpRtt = new char[(wcslen(lpData) + 1) * 2];
 
    WideCharToMultiByte(CP_ACP, 0, lpData, -1, lpRtt, (wcslen(lpData) + 1) * 2,
 
        NULL, NULL);
 
    return lpRtt;
}
 
LPWSTR OEMtoUNICODE(LPSTR lpStr)
{
    int len = (strlen(lpStr) + 1) * 2;
    LPWSTR lpwRtt = new TCHAR[len];
    memset(lpwRtt, 0x00, strlen(lpStr) / 2 + 1);
 
    MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpwRtt, (strlen(lpStr) + 1) * 2);
 
    return lpwRtt;
}
 
/* Execute a DOS command.
 
If the function succeeds, the return value is a non-NULL pointer to the output of the invoked command.
Command will produce a 8-bit characters stream using OEM code-page.
 
As charset depends on OS config (ex: CP437 [OEM-US/latin-US], CP850 [OEM 850/latin-1]),
before being returned, output is converted to a wide-char string with function OEMtoUNICODE.
 
Resulting buffer is allocated with LocalAlloc.
It is the caller's responsibility to free the memory used by the argument list when it is no longer needed.
To free the memory, use a single call to LocalFree function.
*/
LPWSTR DosExec(CString command) {
    // Allocate 1Mo to store the output (final buffer will be sized to actual output)
    // If output exceeds that size, it will be truncated
    const SIZE_T RESULT_SIZE = sizeof(char) * 1024 * 1024;
    char* output = (char*)LocalAlloc(LPTR, RESULT_SIZE);
    memset(output, 0, RESULT_SIZE);
 
    HANDLE readPipe, writePipe;
    SECURITY_ATTRIBUTES security;
    STARTUPINFOA start;
    PROCESS_INFORMATION processInfo;
 
    security.nLength = sizeof(SECURITY_ATTRIBUTES);
    security.bInheritHandle = true;
    security.lpSecurityDescriptor = NULL;
 
    if (CreatePipe(
        &readPipe, // address of variable for read handle
        &writePipe, // address of variable for write handle
        &security, // pointer to security attributes
        0 // number of bytes reserved for pipe
    )) {
 
        GetStartupInfoA(&start);
        start.hStdOutput = writePipe;
        start.hStdError = writePipe;
        start.hStdInput = readPipe;
        start.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
        start.wShowWindow = SW_HIDE;
 
        char *cmd = UNICODEtoANSI(command);
        // We have to start the DOS app the same way cmd.exe does (using the current Win32 ANSI code-page).
        // So, we use the "ANSI" version of createProcess, to be able to pass a LPSTR (single/multi-byte character string) 
        // instead of a LPWSTR (wide-character string) and we use the UNICODEtoANSI function to convert the given command 
        if (CreateProcessA(NULL, // pointer to name of executable module
            cmd, // pointer to command line string
            &security, // pointer to process security attributes
            &security, // pointer to thread security attributes
            TRUE, // handle inheritance flag
            NORMAL_PRIORITY_CLASS, // creation flags
            NULL, // pointer to new environment block
            NULL, // pointer to current directory name
            &start, // pointer to STARTUPINFO
            &processInfo // pointer to PROCESS_INFORMATION
        )) {
            OutputDebugStringA("run :");
            OutputDebugStringA(cmd);
            OutputDebugStringA("\n");
            // wait for the child process to start
            for (UINT state = WAIT_TIMEOUT; state == WAIT_TIMEOUT; state = WaitForSingleObject(processInfo.hProcess, 100));
 
            DWORD bytesRead = 0, count = 0;
            const int BUFF_SIZE = 1024;
            char* buffer = (char*)malloc(sizeof(char)*BUFF_SIZE + 1);
            //strncpy(output, "", 1);
            StringCbCopyA(output, 1, "");
            do {
                DWORD dwAvail = 0;
                if (!PeekNamedPipe(readPipe, NULL, 0, NULL, &dwAvail, NULL)) {
                    // error, the child process might have ended
                    break;
                }
                if (!dwAvail) {
                    // no data available in the pipe
                    break;
                }
                ReadFile(readPipe, buffer, BUFF_SIZE, &bytesRead, NULL);
                buffer[bytesRead] = '\0';
                if ((count + bytesRead) > RESULT_SIZE) break;
                //strncat(output, buffer, bytesRead);
                OutputDebugStringA(buffer);
                StringCbCatA(output, bytesRead, buffer);
                count += bytesRead;
            } while (bytesRead >= BUFF_SIZE);
            free(buffer);
        }
 
    }
 
    CloseHandle(processInfo.hThread);
    CloseHandle(processInfo.hProcess);
    CloseHandle(writePipe);
    CloseHandle(readPipe);
 
    // convert result buffer to a wide-character string
    LPWSTR result = OEMtoUNICODE(output);
    LocalFree(output);
    return result;
}
TAG •

List of Articles
No. Subject Author Date Views
45 [C#] UI Update from Thread, Thread에서 UI 업데이트 하기 샘플 코드 lizard2019 2019.01.23 1040
44 [C#] 프로그램 종료 방법 lizard2019 2019.01.23 6762
43 [C#] 코드 실행 시간 측정 및 DateTime 스트링으로 변환 포맷 lizard2019 2019.01.23 22947
42 [Win32] HBITMAP Contrast 조절하는 코드 - RGB 이미지 보정 엉뚱도마뱀 2018.05.04 734
» [Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드 digipine 2017.11.02 2687
40 VC++ UTF8 변환 관련 매크로 digipine 2017.11.02 8778
39 C# 으로 구현한 화면 캡춰 클래스 1 digipine 2017.11.02 33962
38 C# - 한글로된 폰트명 처리 방법 개선 (Font Name Localization) digipine 2017.11.02 1388
37 [C#] StreamReader 에서의 한글 Encoding 문제 digipine 2017.10.29 999
36 [API Hooking] Dll Injection 하는 방법 digipine 2017.10.29 5210
35 [WIN32] 파일 핸들로 파일 명 구하기 digipine 2017.10.29 1060
34 [WIN32] Process ID로 HWND 구하기 digipine 2017.10.29 5012
33 MS의 Hot Fix API의 유형 연구 digipine 2017.10.29 311
32 [WIN32] 실행 중인 프로세스를 외부에서 강제로 종료, 안전한 TerminateProcess digipine 2017.10.29 3455
31 [WIN32] API Hook 정리 문서 digipine 2017.10.29 1963
30 [WIN32, WINCE] 디스크 용량 구하는 방법 API GetDiskFreeSpaceEx digipine 2017.10.29 1564
29 [WINCE] MulDiv 함수 구현 digipine 2017.10.29 699
28 [DirectShow] 화면 원본 비율유지 digipine 2017.10.29 956
27 [VC++, WInAPI] 폴더를 통채로 지우기, 서브 폴더 포함, DeleteAllFiles digipine 2017.10.29 1761
26 [Win API]프로세스 아이디와 윈도우 핸들을 이용 파일명 구하기 digipine 2017.10.29 1390
Board Pagination Prev 1 2 3 Next
/ 3