If I lock with (BYTE)TRUE : works
If I unlock a locked device using (BYTE)FALSE : works
If I try to unlock a device (without locking it first) using (BYTE)FALSE : gives error #22, The device does not recognize command
I think this is expected behavior. Please guide me in right direction if I am doing it wrong.
I have a C++ program running on Windows XP compiled with Visual C++ 6.0 containing the following code
if (remove(NewName) != 0)
errmsg = strerror(errno);
printf("Removal Error: %s\n", errmsg);
and this fails, offering to send a message to Microsoft about the error of my ways.
The file I want to rename(FileName) does exist and the file I want to rename it to(NewName) does not and none of my error messages appear.
Please make suggestions on how I should fix this.
I have just run this code and it works fine, so I suspect there is some part that you have not shown that is actually failing. Can you post the full program (assuming it's not hundreds of lines)? Also please surround it with <pre></pre> tags to keep it formatted properly, use the code block button at the top of the edit window.
I am working on a project where I need to catch the windows shutdown event through a windows service running in system account, delay shutdown and execute some shutdown scripts and then once the scripts have been completed the shutdown continues.Kindly suggest me some ways to do this via a windows service developed by me in C++.
I have to run this in all operating systems namely Windows XP, Windows Server 2003& 2008, Windows Vista,Windows 7. I tried it presently on XP , but the application is meant to be supporting all flavours of Windows after NT
This means that you will need to conditionally delay the shutdown based on what operating system the service is running on. There have been some changes in how a shutdown works in Vista and above. These changes are documented here:
I would suggest creating a hidden window in each interactive logon session and returning FALSE when the window recieves a WM_QUERYENDSESSION message. On Vista and above you will need to call ShutdownBlockReasonCreate before returning FALSE to the WM_QUERYENDSESSION message.
Also to call Abort shutdown Method my service first needs to know that a shutdown signal was received. So kindly tell me how my service will come to know about that a shutdown signal was received and where should I place a call to abortshutdown ?
To catch the Shutdown-Event you have to handle SERVICE_CONTROL_SHUTDOWN or SERVICE_CONTROL_PRESHUTDOWN
event in your HandlerEx callback function you registered with RegisterServiceCtrlHandlerEx.
(To use this, you need to say the system that your service supports shutdown events.
An other possibility is to use the SetConsoleCtrlHandler function where you handle the CTRL_SHUTDOWN_EVENT event in your HandlerRoutine.
After you catched the shutdown-event you have to use the function AbortSystemShutdown or just
delay your service-shutdown for example with the status SERVICE_STOP_PENDING.
Its also a good reference about service shutdown.
"Services can also use the SetConsoleCtrlHandler function to receive shutdown notification. This notification is received when the running applications are shutting down, which occurs before services are shut down."
and abortshutdown does the following functionality
BOOL AbortShutdown(LPTSTR lpMachineName)
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
// TRACE("OpenProcessToken failed.\n");
// Get the LUID for shutdown privilege.
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
//TRACE("AdjustTokenPrivileges(setting) enable failed.\n");
// Prevent the system from shutting down.
fResult = AbortSystemShutdown(lpMachineName);
Does your service get the shutdown notification SERVICE_CONTROL_SHUTDOWN or does AbortSystemShutdown not abort the shutdown?
I would say its easier to test the version with SetConsoleCtrl..., after that you should try th service version.
In my first answer I said, that you have to say the system that your service supports SHUTDOWN events.
Now I found what i meant. You have to use SetServiceStatus with a SERVICE_STATUS where dwControlsAccepted is a least SERVICE_ACCEPT_SHUTDOWN.
modified on Thursday, October 22, 2009 12:02 PM
Last Visit: 31-Dec-99 19:00 Last Update: 7-Dec-23 9:53