Frerich Raabe Frerich Raabe - 10 months ago 41
C++ Question

How do I get the job object (if any) for my current process?

In the context of Windows Job Objects, how can I get the job object for the current process (in case it is in a job object)? The IsProcessInJob function lets me test whether a given process (e.g. the current one) is in a given (or any) job - but it doesn't yield the handle of the matching job.

Ben Ben
Answer Source

If you just want to find out what quotas/limits you are running under, or enumerate all the other processes in the job, you don't need to get the Job object for the current process.

You can call QueryInformationJobObject with NULL, which will be the Job object of the current process.

To answer the specific question, call IsProcessInJob find out if you are in a job.

You can find out everything about the Job by passing NULL to QueryInformationJobObject

Your child processes will inherit your job automatically, unless you pass CREATE_BREAKAWAY_FROM_JOB and the job has JOB_OBJECT_LIMIT_BREAKAWAY_OK or JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK is set. In these cases you can assign the process to a new job if you wish.

So without knowing the handle, you can find out all about your current Job, and assign child processes within the current job, or if you have permission, without the current job. I.e. you can do almost everything the handle would allow you to do.

The only exception is duplicate it to another sibling process. If you need to do that you will have to have the parent process communicate the handle value to you somehow.