If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

InitiateSystemShutdownEx hangs system

So using InitiateSystemShutdownEx causes a Win32 machine to go log off go to the shutdown screen and just sit there displaying shutting down with the working indicator just spinning. I left the machine sitting there for an hour and it never shut down. Not sure whats going on. This works fine on all the Win7 x64 machines we are using but the one 32 bit Win7 machine has this problem and it occurs no matter what parameters I pass.

Re: InitiateSystemShutdownEx hangs system

Don't remove the shutdown privilege after the function returns. As msdn says, the function is asynchronous so it may return before it needs to use the shutdown privilege (unlikely maybe?) Also check the return value from the function to see if that sheds any light on the problem.

Re: InitiateSystemShutdownEx hangs system

Not sure whats going on. This works fine on all the Win7 x64 machines we are using but the one 32 bit Win7 machine has this problem and it occurs no matter what parameters I pass.

In general, you should check all of the API functions for their respective return values. Just assuming they "work" because they work on some machines is not how to write general code that will be run on any machine.

For example LookupPrivilegeValue, AdjustTokenPrivileges, etc. all return values that you are not checking. So the first thing to do is write the code so that you're checking the return value (and in addition, calling GetLastError() if necessary).

And no, your call to GetLastError() after calling AdjustTokenPrivileges is not correct. You are always supposed to first get the return value of the function, and if it is FALSE, NULL, whatever, then you call GetLastError(). Calling GetLastError() just blindly like that will possibly give you false results.

Re: InitiateSystemShutdownEx hangs system

Windows error log has no errors at the time of shutdown, there are several from when it starts up but that is another problem. The return value has always been 1 or non zero and nothing reported from GetLastError()

Re: InitiateSystemShutdownEx hangs system

Calling GetLastError() just blindly like that will possibly give you false results.

If an API function succeeds then GetLastError() cannot be relied upon to return ERROR_SUCCESS. It's best to assume in the case of success that its value is undefined unless the API documentation specificially states the value of GetLastError upon success.

Also, even when the InitiateSystemShutdownEx function indicates success, you are still returning false. You don't provide details of the function definition which contains this section of code, but if InitiateSystemShutdownEx succeeds, shouldn't you be returning true?

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: InitiateSystemShutdownEx hangs system

Originally Posted by 2kaud

Also, even when the InitiateSystemShutdownEx function indicates success, you are still returning false. You don't provide details of the function definition which contains this section of code, but if InitiateSystemShutdownEx succeeds, shouldn't you be returning true?

Re: InitiateSystemShutdownEx hangs system

Originally Posted by 2kaud

If an API function succeeds then GetLastError() cannot be relied upon to return ERROR_SUCCESS. It's best to assume in the case of success that its value is undefined unless the API documentation specificially states the value of GetLastError upon success.

I'm assuming you're addressing the OP, which is exactly my point.

You can only use GetLastError() if

1) The function returns a failure code and
2) The function is documented that on failure, use GetLastError() to get more info and
3) The GetLastError() is called immediately after the failed function.

Any violation of those 3 items, then usage of GetLastError() is incorrect, such as calling GetLastError() on a success code.

And no the return value was being used to indicate success or failure and in the calling program it is not even checked, however as this is legacy code being updated from calling ExitWindows, which was failing due to applications running on another user I switched to InitiateSystemShutdownEx. Running this version still provides no errors yet the Win7 32 bit machine does not shutdown and just sits on the Shutting Down screen.

Re: InitiateSystemShutdownEx hangs system

1) The function returns a failure code and
2) The function is documented that on failure, use GetLastError() to get more info and
3) The GetLastError() is called immediately after the failed function.

Any violation of those 3 items, then usage of GetLastError() is incorrect, such as calling GetLastError() on a success code.

Regards,

Paul McKenzie

Maybe you should lookup the documentation for AdjustTokenPrivileges as it states:

If the function succeeds, the return value is nonzero. To determine whether the function adjusted all of the specified privileges, call GetLastError, which returns one of the following values when the function succeeds:

Re: InitiateSystemShutdownEx hangs system

Depends upon what the program calling this function does with the return value. From the MSDN documentation

'The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services, or even the system.'

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: InitiateSystemShutdownEx hangs system

Originally Posted by 2kaud

Depends upon what the program calling this function does with the return value. From the MSDN documentation

'The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services, or even the system.'

Ordinarily I would agree with you, but for this function the programmer shouldn't make any expectations about doing any work after this function has been called because there is no guarantee of how fast the rug is going to be pulled out.

Re: InitiateSystemShutdownEx hangs system

Originally Posted by farpetrad

Maybe you should lookup the documentation for AdjustTokenPrivileges as it states:

If the function succeeds, the return value is nonzero. To determine whether the function adjusted all of the specified privileges, call GetLastError, which returns one of the following values when the function succeeds:

ERROR_SUCCESS
ERROR_NOT_ALL_ASSIGNED

OK My mistake. Probably one of the few functions that uses "GetLastError" to indicate success.

However, as Arjay pointed out, the InitiateSystemShutdownEx function is asynchronous and on top of that, is only a request to the OS to shutdown.

You should write a tiny main() function that all it does is call that code. Then see if that works correctly. If it works, then it is the larger app that has an issue after that function is called.

Re: InitiateSystemShutdownEx hangs system

I was about to try that with a C# app I found that tests all the different shutdown api's, however this is being called from a server app that is sending a packet which in response we need to shut down, I have a feeling that one of the other apps running is not responding correctly as there is a second server app running on this machine, and only this machine, and it may not be shutting down and may be causing the problems. I will report back after I test this if I can get time on the lab system.

Re: InitiateSystemShutdownEx hangs system

Originally Posted by farpetrad

I was about to try that with a C# app I found that tests all the different shutdown api's, however this is being called from a server app that is sending a packet which in response we need to shut down, I have a feeling that one of the other apps running is not responding correctly as there is a second server app running on this machine, and only this machine, and it may not be shutting down and may be causing the problems. I will report back after I test this if I can get time on the lab system.

You've got a lot of layers in there before getting to the ISSEx call. At the very least modify your server code and spew some trace statements to a log file to make sure the shutdown code is getting called. I've used InitiateSystemShutdown[Ex] since NT4 on, and have yet to find a case where an app prevented the system from shutting down.

If you do find that IISEx is getting called try to add more diagnostics to the logging. Use the toolhelp apis to take a snapshot of the running process prior to the IISEx call and log them. Call IISEx and then take another snapshot in the destructor of the class that calls ISSEx. Btw, I'm not sure if the toolhelp code will actually execute (or if it does execute, will it show a difference between the previous process snapshot). Another consideration is the account this service is running in. Try running the service under a different account with admin permissions.