2

My application is running in a job. I want to get a Handle to this Jobobject using OpenJobObject so i can later use this handle. The problem is, that i don't know the jobs name, and with passing NULL to the Job name it gives error 87 ( The parameter is incorrect ) back.

This is how i tried it:

HANDLE handle = OpenJobObject( JOB_OBJECT_QUERY, FALSE, NULL );
if ( !handle  ) printf( "\nError %d", GetLastError() );
else printf( "\nOK" );

I also found this on MSDN: An application cannot obtain a handle to the job object in which it is running unless it has the name of the job object. However, an application can call the QueryInformationJobObject function with NULL to obtain information about the job object.

So my question is, is it possible to get somehow a handle to the JobObject in which my application is running? Or get the name of the job my application is running in?

Thanks!

Update:
My code so far: http://pastebin.com/aJ7XMmci Right now, i'm getting Error 87 ( The parameter is incorrect ) from SetInformation :(

kampi
  • 2,362
  • 12
  • 52
  • 91
  • Unfortunately, runas does not appear to use a named job object, so OpenJobObject isn't going to help in your particular situation. – Harry Johnston Nov 21 '12 at 20:32

1 Answers1

0

OK, doesn't look like there's any supported method. That doesn't mean it can't be done! :-)

To enumerate all the handles in the system, see this question. The sample code here filters the handles and only looks for those belonging to a particular process, but that's easy to change. You might need to enable debug privilege first.

For each handle, duplicate it into your process, then call IsProcessInJob to find out whether it's the right handle or not.

Once you've got that working, check whether SYSTEM_HANDLE.ObjectTypeNumber is always the same for job objects. It probably is (on any given OS, at least) in which case you can drastically increase the efficiency of the code by only checking job object handles.

You could perhaps also filter to just the process running the Secondary Logon service, since this seems to be what creates the job objects for runas.

(If you do get this working, please post code - it could be very useful for future visitors.)

Community
  • 1
  • 1
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Thank you very much! This should be working...theoraticly :) I will definitely get this work, because it gave so much trouble for me, and others too. I have an other question, and someone recommended to use jobobjects. http://stackoverflow.com/questions/13399313/how-to-start-a-process-and-wait-for-its-child-process-to-finish Could you please see that too. Maybe you have another idee to solve my problem. But i will solve it this way too, and post the code when i am ready. – kampi Nov 21 '12 at 21:44
  • Yes, i did, but unfortunately it didn't work. It starts the setup.exe, but after setup.exe has started its two processes, setup.exe closes, and also your program exits too :( – kampi Nov 22 '12 at 07:26
  • In that case I think this is probably the only realistic user-mode solution. If it doesn't work for some reason, or if you can't use unsupported Win32 functions as a matter of policy, you'll need to go to kernel mode. – Harry Johnston Nov 22 '12 at 20:32
  • I did what you told. But right now, i'm getting another interesting error. I get error 87( The parameter is incorrect ) from `SetInformationJobObject`. I inserted a link to my code. Could you please check it? Maybe i'm doing something wrong. – kampi Nov 26 '12 at 11:21