Originally Posted by
abuckau907
I'm probably wrong, but I think it's something along the lines of....
how does each process get it's pid? well..from looking at the task manager, they generally range 0-5000 or whatever..anyway, it's a relatively low range. Why not use the whole Int32 and go up to 2 million or whatever? well...I think it's because Windows(R) keeps track of the handles very careful (scratch that. I know it does), so it's the one who assignes pID when a process starts. <---The OpenProcess() handle is different, because OpenProcess() is basically saying "Windows(R) may I please have ANOTHER handle which is connected to this process" -- the fact that the ID is 4 apart..not really sure. Either coincidence, or something to do with how Windows manages/recycles pid's.
How do you know the process.id varies? I not quite sure what you mean. Like...on start-up you find the pid, then a few minutes later it's different? I don't think* that's possible (unless you re-start the app ofc) so..are you just keeping a reference to said process like...
Process _targetProc = ******
and later when you check _targetProc.Id, the value changes?!?
or do you mean, on startup, the id is different each time? Because this is normal as described in the Windows(R) part above.
I just looked at my task manager, never realized --> most of the pID are like.. at least* 30-40 apart from eachother..
Why is there this space? why doesn't Windows(R) just hand them out in sequential order?
The Answer: It's something to do with how windows(R) itself manages processes. I think it makes them 40 apart (or whatever) to save space for in-case the Handle gets duplicated...apparently windows decided to keep their values close, not sure the technical reason tho. Something to do with how "windows manages processes/pid and copying it". Windows internals.
edit: If you wanted a technical answer, sorry. But because you didn't know OpenProcess() was *supposed* to return a different handle, I assume mine is close enough to help?
apparently OpenProcess() (ie. windows) is designed to keep 'related' pid into a small group (ie. range of values)..not sure of the technical reason tho, I'm sure there are several. -stuff to do with creating/copying/pausing/stopping processes probably, it's a complex subject. Keeping related pid in a specific range apparently makes it easier for windows to manage processes. or it's a coincidence, and you just need to run a bunch of apps *after* starting the target process but *before* calling OpenProcess() to get a new handle. <-- test this maybe, see if it's still 4 apart? if so, it's by Windows' doing.
Q How are you keeping track of pId and when do you know it's changed?
Like...
do you store a reference to the process. Or just declare as Int32 and save it there or ?
Maybe post a snippet of code and I can try to comment more.
edit* hypothetically I've never started enough new processes to get to a pid of 2 million :P i probably keep it under 30..5 of which have gui, so, maybe windows pId does go higher than 5000 or whatever, but, obviously windows(r) has to manage them in *some* fashion.
final-edit: if the original "space between pId" is only 30-40, at 4 bytes each that's only 10 copies of the handle? so..if more than 10 copies are made, the pID's will either be in 2 distinct ranges, or, you can't have more than 10 copies?!? There is a flaw in my logic, someone with more knowledge care to comment?
final-edit#2 lol: just started a bunch of apps --> some of their pId were much less than 30 apart, so, I think the fact that it was 4 apart is just coincidence, and the handle returned by OpenProcess() is in no way guaranteed to be close to the original process handle. previous statements retracted. ?