Help With Jobs and Processes?

Mar 19, 2014 at 9:33pm
So, I know what a job is: a bunch of processes (right?)
And I have a little bit of a clue of what a process is, thanks to my addiction to the task manager and stuff.

Anyway, I know what they are, but how do I use them? I know CreateProcess() makes a process, but I don't know how to use the process I created.

And CreateJobObject() makes a job (right?) but I don't know how to use it.

Anyway, any help I can get will be greatly appreciated.
Mar 20, 2014 at 6:21pm
Yes, Job Objects are groups of processes that are managed together. When you would use something like this is largely up to you. There are only a few times I've considered using them for a task and I always seem to find a better or more flexible way of accomplishing whatever it was I was trying to do.
Mar 20, 2014 at 9:04pm
Thank you for clearing up jobs, but about processes? I have (like I said) only so much as a clue about processes, and I have NO idea how to use them.
Mar 21, 2014 at 2:01pm
When an instance of a program is loaded into memory, it is referred to as a "process". Internet Explorer is an example of a process and so is Notepad. You would use "CreateProcess()" to launch other programs from your own.

An example of this that I often use is for a custom in house installer service I wrote. It uses the "CreateProcess()" function to launch 'msiexec.exe' with the name of the installer package, the '-i' and the '-quiet' parameters as arguments. This way I can use an already available tool to accomplish the task of installing an application.
Last edited on Mar 21, 2014 at 2:02pm
Mar 21, 2014 at 6:59pm
Oh, thanks for summing that up for me. Anyway, how would I use it though? Can you give like, a basic example? Just so I can understand how to use it better?
Mar 21, 2014 at 8:04pm
This is about as simple as I can think to get:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>

#define _WIN32_WINNT 0x05010000

#include <windows.h>
#include <winbase.h>

void pause()
{
    std::cin.sync();
    std::cout << "\nPAUSED\n";
    std::cin.ignore();
}

int main()
{
    TCHAR ProcName[] = "Notepad.exe";
    TCHAR ProcName2[] = "calc.exe";

    PROCESS_INFORMATION pi;
        ZeroMemory(&pi, sizeof(pi));

    PROCESS_INFORMATION pi2;
        ZeroMemory(&pi2, sizeof(pi));

    STARTUPINFO si;
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);

    STARTUPINFO si2;
        ZeroMemory(&si2, sizeof(si2));
        si2.cb = sizeof(si2);

    HANDLE Jobject = CreateJobObject(NULL, "Job");

    JOBOBJECT_EXTENDED_LIMIT_INFORMATION JBLI;

    try
    {
        if(Jobject == NULL)
        {
            throw "COULD NOT CREATE JOB OBJECT:";
        }

        if(!AssignProcessToJobObject(Jobject, OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId())))
        {
            throw "COULD NOT ASSIGN SELF TO JOB OBJECT!:";
        }

        if(!QueryInformationJobObject(Jobject, JobObjectExtendedLimitInformation, &JBLI, sizeof(JBLI), NULL))
        {
            throw "COULD NOT QUERY JOB OBJECT INFORMATION CLASS!:";
        }

        JBLI.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_BREAKAWAY_OK;

        if(!SetInformationJobObject(Jobject, JobObjectExtendedLimitInformation, &JBLI, sizeof(JBLI)))
        {
            throw "COULD NOT SET JOB OBJECT INFORMATION CLASS!:";
        }

        CreateProcess(NULL, ProcName, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

        if(!CreateProcess(NULL, ProcName2, NULL, NULL, FALSE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si2, &pi2))
        {
            throw "COULD NOT CREATE CHILD PROCESS!:";
        }

        std::cout << "Child Process ID: " << pi.dwProcessId << "\tThread ID: " << pi.dwThreadId << "\tHandle: " << pi.hProcess << "\n";
    }
    catch(char* Message)
    {
        std::cout << Message << " " << GetLastError() << "\n";
    }

    pause();

    TerminateProcess(pi.hProcess, 1816);
    TerminateProcess(pi2.hProcess, 1816);

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    CloseHandle(pi2.hThread);
    CloseHandle(pi2.hProcess);

    CloseHandle(Jobject);

    return 0;
}


If you have Process Explorer or something similiar you can see that the "Notepad" process is part of the parents job, but "calc" is not.
Mar 21, 2014 at 8:34pm
Okay, okay, thank you. I didn't really know how to make a process before or even what it was, but after running the code through I get it know. Anyway, thanks for the help.
Last edited on Mar 21, 2014 at 8:34pm
Topic archived. No new replies allowed.