I'm trying to use the win32job API that is part of the PyWin32 package. I want to do this:
win32job.CreateJobObject(None, NULL)
I want to pass NULL as the second parameter as documented here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682409(v=vs.85).aspx
If lpName is NULL, the job is created without a name.
How do I pass in NULL?
Here's what I hoped would work, but doesn't:
win32job.CreateJobObject(None, None)
Error:
TypeError: None is not a valid string in this context
(Side question... if anyone knows how to view JobObjects associated with a process in Windows, that would be helpful.)
You can use win32job.CreateJobObject(None, "")
. Although it isn't specified on [MS.Docs]: CreateJobObjectA function, the empty string acts just like NULL.
According to the link above:
If the function succeeds, the return value is a handle to the job object. The handle has the JOB_OBJECT_ALL_ACCESS access right. If the object existed before the function call, the function returns a handle to the existing job object and GetLastError returns ERROR_ALREADY_EXISTS.
I wrote a small C program for demonstrating purposes:
#include <Windows.h>
#include <stdio.h>
#define EMPTY_TEXT ""
#define DUMMY0_TEXT "dummy0"
#define DUMMY1_TEXT "dummy1"
#define DIM 12
int main() {
char* names[DIM] = { NULL, NULL, EMPTY_TEXT, EMPTY_TEXT, DUMMY0_TEXT,
DUMMY0_TEXT, EMPTY_TEXT, DUMMY0_TEXT, DUMMY1_TEXT,
NULL, DUMMY0_TEXT, DUMMY1_TEXT };
HANDLE jobs[DIM] = { NULL };
for (int i = 0; i < DIM; i++) {
jobs[i] = CreateJobObjectA(NULL, names[i]);
printf("%02d [%6s] CreateJobObject: %08X - GetLastError: %d\n", i, names[i], (long)jobs[i], GetLastError());
}
for (int i = 0; i < DIM; i++)
CloseHandle(jobs[i]);
return 0;
}
Output (built and ran with VStudio 2015 Community Edition):
00 [(null)] CreateJobObject: 000000D8 - GetLastError: 0 01 [(null)] CreateJobObject: 000000E0 - GetLastError: 0 02 [ ] CreateJobObject: 00000088 - GetLastError: 0 03 [ ] CreateJobObject: 000000F0 - GetLastError: 0 04 [dummy0] CreateJobObject: 000000F4 - GetLastError: 0 05 [dummy0] CreateJobObject: 000000F8 - GetLastError: 183 06 [ ] CreateJobObject: 000000E8 - GetLastError: 0 07 [dummy0] CreateJobObject: 000000FC - GetLastError: 183 08 [dummy1] CreateJobObject: 00000100 - GetLastError: 0 09 [(null)] CreateJobObject: 000000DC - GetLastError: 0 10 [dummy0] CreateJobObject: 000000E4 - GetLastError: 183 11 [dummy1] CreateJobObject: 00000104 - GetLastError: 183
You can see that for "dummy0" and "dummy", every time (excepting the 1st) when creating a new object, the function does return a new HANDLE, but fails (GetLastError returns 183 (which is ERROR_ALREADY_EXISTS)). This doesn't happen for NULL and the empty string names (from here I understand that a new object is created with every call instead of incrementing an existing object's reference).
Python "translation" (code.py):
#!/usr/bin/env python3
import win32job
import win32api
DUMMY0 = "dummy00"
DUMMY1 = "dummy11"
JOB_NAMES = ["", "", DUMMY0, DUMMY0, "", DUMMY1, DUMMY0, "", DUMMY1]
if __name__ == "__main__":
handles = list()
for i, name in enumerate(JOB_NAMES):
h = win32job.CreateJobObject(None, name)
print("{} [{:10}] {} - GetLastError: {}".format(i, name, h, win32api.GetLastError()))
handles.append(h)
for h in handles:
win32api.CloseHandle(h)
Output (same result as in C's case - which is natural since Python functions only wrap the lower level C ones):
(py35x64_test) e:\Work\Dev\StackOverflow\q046800142>"c:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py 0 [ ] <PyHANDLE:300> - GetLastError: 0 1 [ ] <PyHANDLE:308> - GetLastError: 0 2 [dummy00 ] <PyHANDLE:580> - GetLastError: 0 3 [dummy00 ] <PyHANDLE:584> - GetLastError: 183 4 [ ] <PyHANDLE:588> - GetLastError: 0 5 [dummy11 ] <PyHANDLE:592> - GetLastError: 0 6 [dummy00 ] <PyHANDLE:596> - GetLastError: 183 7 [ ] <PyHANDLE:600> - GetLastError: 0 8 [dummy11 ] <PyHANDLE:604> - GetLastError: 183
Regarding the "side" question: unfortunately, I'm not familiar with that topic.