Search code examples
c#.netwinapipinvokejobs

Unusual ERROR_BAD_LENGTH while setting job memory limits in C#


I'm currently attempting to expand this to allow me to access more of the possibilities with managing job objects. However, I'm running into issues whenever I try to set the JobMemoryLimit defined in the Limit objects.

I made a function that duplicates the set information logic in the JobObject constructor with some additional steps. First I set the additional flag

info.LimitFlags |= (uint)JobObjectLimitFlags.JOB_OBJECT_LIMIT_JOB_MEMORY;

And that alone is enough to break my code. When I call SetInformationJobObject it returns false and the job is not modified. My call to Marshal.GetLastWin32Error() returns the code for ERROR_BAD_LENGTH. Setting extendedInfo.JobMemoryLimit makes no difference.

I should note that in this example I am attempting to modify an existing Job, but I've tried it both ways. Plugging the above line into the JobObject constructor still results in a return of false from SetInformationJobObject, but now the error code is 0.

What does an ERROR_BAD_LENGTH mean in this case, and why is it triggered by changing my flags?


Solution

  • As it turns out, the object structure in the given example isn't correct for my system. I don't know if this is simply an issue with the example that isn't found by the path its author takes, or if something has changed about the architecture.

    All of the working code I've found and made uses 4 bytes for LimitFlags when running in 32bit mode, and 8 bytes when running in 64bit mode.