부모 프로세스에서 값을 던져주면 그것을 자식 프로세스에서 더해주는 코드..


부모프로세스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
 
#define DIR_LEN MAX_PATH+1
 
int _tmain(int avgc, TCHAR* avgs[]) {
 
    STARTUPINFO si = { 0, };
    PROCESS_INFORMATION pi;
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE;
    si.dwX = 100;
    si.dwY = 100;
    si.dwXSize = 200;
    si.dwYSize = 200;
    si.lpTitle = _T("practice!");
 
    TCHAR add[100=  _T("Systemprogram.exe");
    
    TCHAR num[10];
 
    printf("숫자 10개 입력\n");
    
    for (int i = 0; i < 10; i++) {
        _tscanf(_T("%s"), &num);
        _tcprintf(_T("%s"), num);
        _tcscat(add, _T(" "));
        _tcscat(add, num);
    }
 
    TCHAR command[100];
    _tcscpy(command, add);
    _tcprintf(_T("%s"), command);
 
    TCHAR cDir[DIR_LEN];
    BOOL state;
    GetCurrentDirectory(DIR_LEN, cDir);
    _fputts(cDir, stdout);
    _fputts(_T("\n"), stdout);
    SetCurrentDirectory(_T("C:\\WinSystem"));
    GetCurrentDirectory(DIR_LEN, cDir);
    _fputts(cDir, stdout);
    _fputts(_T("\n"), stdout);
 
    state = CreateProcess(NULL, command, NULLNULL, TRUE, CREATE_NEW_CONSOLE, NULLNULL&si, &pi);
    if (state != 0) {
        _fputts(_T("생성"),stdout);
    }
    else {
        _fputts(_T("생성불가"), stdout);
    }
//    CloseHandle(pi.hProcess);
    return 0;
}
 
cs


자식프로세스를 실행시키고 나면 만들어지는 자식프로세스이름.exe 파일을 WinSystem에 잠시 이동시켜놓고

부모프로세스로 WinSystem에 들어가서 이를 실행시켜주면서 매개변수를 넘겨준다.. 

 

자식프로세스.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
 
int _tmain(int argc,TCHAR* argv[]) {
 
    DWORD val[10];
    DWORD sum = 0;
    
    for (int i = 0; i < 10; i++) {
        val[i] = _ttoi(argv[i + 1]);
        sum += val[i];
    }
    
    
    _tprintf(_T("sum = %d , avg = %lf"), sum, double(sum/10));
 
    _gettchar();
 
    return 0;
}
cs


argv[0] 즉 매개변수 첫번 째 값은 실행파일의 문자열 값이 들어온다.  여기서 Systemprogram.exe 이다.

그 후 argv[1] 부터 부모프로세스에서 적어준 숫자 값이 들어가있기 때문에 여기서 부터 더해준다.

+ Recent posts