반응형
    
    
    
  윤성우 저자께서 작성하신 "뇌를 자극하는 윈도우 운영체제"의 실습 2번입니다.
C언어로 만들어져 있으며 다음과 같은 요구사항을 말하고 있습니다.
1. start 입력시 자식프로세스 생성.
2. start echo "문자열" 입력시, 자식프로세스 생성 후 해당 문자열 출력.
저는 C++을 사용해 구현했으며, Boost의 tokenizer를 사용했습니다.
만든 visualstudio 는 2019 버전을 사용했으며, 다른 버전 사용 시 echo_console.exe 파일 경로가 다를 수 있습니다.
소스코드는 아래와 같습니다.
main.cpp
#include <iostream>
#include <tchar.h>
#include <Windows.h>
#include <string>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef basic_string<TCHAR> TSTRING;
#define CMD_TOKEN_NUM 10
#define BUFFER 1000
TSTRING ERROR_CMD
= _T("is not execution code. \n");
bool CmdProcessing (void);
TSTRING StrLower(TSTRING);
void Make_Process(TSTRING& , TSTRING);
int _tmain(int agrc, TCHAR * argv[]) {
    _tsetlocale(LC_ALL, _T("Korean"));
    bool isExit = true;
    while (1) {
        isExit = CmdProcessing();
        if (isExit == true) {
            _fputts(_T("command end..... \n"), stdout);
            break;
        }
    }
    return 0;
}
bool CmdProcessing(void) {
    TSTRING cmdString;
    TSTRING cmdTokenList[CMD_TOKEN_NUM];
    _fputts(_T("Best command prompt>> "), stdout);
    getline(cin, cmdString);
    int index = 0;
    //boost::escaped_list_separator<char>
    boost::tokenizer<> tok(cmdString);
    for_each(tok.begin(), tok.end(), [&](auto& s) {
        cmdTokenList[index++] = [&](TSTRING s) -> TSTRING{    //Uppercase -> lowercase
            for (int i = 0; i < s.size(); i++)
                s[i] = tolower(s[i]);
            return s;
        }(s);
    });
    
    if (cmdTokenList[0] == _T("exit"))
        return true;
    else if (cmdTokenList[0] == _T("start")) {
        Make_Process(cmdTokenList[1], cmdString);
        return false;
    }
    else {
        cout << cmdTokenList[0] << ERROR_CMD << endl;
        return false;
    }
    return -1;
}
void Make_Process(TSTRING& cmd, TSTRING echo) {
    TCHAR command[] = _T("..\\..\\echo_console\\x64\\Debug\\echo_console.exe");
    //TCHAR command[] = _T("cmd.exe");
    STARTUPINFO si = { 0, };
    PROCESS_INFORMATION pi;
    si.cb = sizeof(si);
    TCHAR title[] = "echo_console.exe";
    si.lpTitle = title;
    TCHAR c_echo[BUFFER];
    TCHAR sum_echo[BUFFER];
    int len;
    if (cmd == _T("echo")) {
        echo.erase(0, 6);
        len = echo.size();
        echo.copy(c_echo, len);
        c_echo[len] = '\0';
        _stprintf_s(sum_echo, _T("%s %s"), command, c_echo);
    }
    else if (cmd == _T("")) {
        _stprintf_s(sum_echo, _T("%s"), command);
    }
    cout << sum_echo << endl;
    int ischeck = CreateProcess(NULL, sum_echo, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    if (!ischeck)
        cout << "ProcessCreate Error!!" << endl;
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}
echo_console.cpp
#include <cstdio>
#include <tchar.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int _tmain(int args, TCHAR * argv[]) {
    cout << " echo_console.exe" << endl;
    int i = 1;
    for(int i = 2; i < args; i++)
        cout << argv[i] << " ";
    cout << endl;
    cin >> i;
    return 0;
}
github : https://github.com/dnfwlq8054/win_ProcessCreate_simplecode.git
반응형
    
    
    
  '프로그래밍 > 운영체제' 카테고리의 다른 글
| C/C++ IPC인 mailslot에 대한 이해 (0) | 2020.01.16 | 
|---|---|
| C/C++ 프로세스 생성하기. (0) | 2019.12.29 | 
| c/c++ TCHAR 정리. (1) | 2019.12.29 | 
| 유니코드와 아스키코드(SBCS, MBCS, WBCS) (0) | 2019.12.11 | 
댓글