Click here to Skip to main content
15,896,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? [modified] Pin
Covean22-Oct-09 5:50
Covean22-Oct-09 5:50 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 6:46
Kushagra Tiwari22-Oct-09 6:46 
QuestionRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 21:10
Kushagra Tiwari22-Oct-09 21:10 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 23:29
Kushagra Tiwari22-Oct-09 23:29 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Covean23-Oct-09 0:31
Covean23-Oct-09 0:31 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari23-Oct-09 0:39
Kushagra Tiwari23-Oct-09 0:39 
QuestionRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari23-Oct-09 2:12
Kushagra Tiwari23-Oct-09 2:12 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Covean23-Oct-09 2:40
Covean23-Oct-09 2:40 
What OS to you use? Cause on Vista I do not find any way to block the shutdown (ShutdownBlockReasonCreate needs a window...).

But the service itself works fine.
Here some eventlog-output:
SetServiceStatus Succeed
Service started
SetServiceStatus Succeed
SERVICE_CONTROL_SHUTDOWN (<- shutdown event caught)
AbortSystemShutdown failed! Error=1115 (<- means shutdown already in progress, very funny errorcode Smile | :) )


The only thing left is to stop the shutdown.
On WinXP your code should work. How do you test your service?
You can just catch this event if your app really runs as service
in debug mode you will never see this event.

Here some code-changes I made (but nothing very special):

1. The "problem" that your SetServiceStatus always "fails":

void ...SetServiceStatus(DWORD dwState)
{
    if (::SetServiceStatus(m_hServiceStatus, &m_status) == TRUE)
    {
        // ...
        LogEvent(TEXT("SetServiceStatus Failed"));
    }
    else
    {
        // ...
        LogEvent(TEXT("SetServiceStatus Failed"));
    }
}


---> copy & paste is not always your friend. Laugh | :laugh:


2. In "...::Handler(...)" I made some changes to get more eventlog entries:

switch (dwOpcode)
{
    case SERVICE_CONTROL_STOP:
        LogEvent(_T("SERVICE_CONTROL_STOP"));
        //...
        break;
    case SERVICE_CONTROL_PAUSE:
        LogEvent(_T("SERVICE_CONTROL_PAUSE"));
        break;
    case SERVICE_CONTROL_CONTINUE:
        LogEvent(_T("SERVICE_CONTROL_CONTINUE"));
        break;
    case SERVICE_CONTROL_INTERROGATE:
        LogEvent(_T("SERVICE_CONTROL_INTERROGATE"));
        break;
    case SERVICE_CONTROL_SHUTDOWN:
        LogEvent(_T("SERVICE_CONTROL_SHUTDOWN"));

        AbortShutdown(chComputerName);
        break;
    //...
}


3. AbortShutdown changes: (AbortShutdown is now part of your Module class, just for LogEvent)

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 
    // privilege. 
	if (!OpenProcessToken(GetCurrentProcess(), 
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    {
       // TRACE("OpenProcessToken failed.\n"); 
        LogEvent(TEXT("OpenProcessToken failed"));

		return false;
    }

 
    // Get the LUID for shutdown privilege. 
    if(!LookupPrivilegeValue(lpMachineName, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid))
    {
        LogEvent(TEXT("LookupPrivilegeValue failed"));
		return false;
    }


 
    tkp.PrivilegeCount = 1;  // one privilege to set    
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
    // Get shutdown privilege for this process. 
    if(!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
    {
        LogEvent(TEXT("AdjustTokenPrivileges failed"));
		return false;
    }
 
    // Cannot test the return value of AdjustTokenPrivileges. 
    /*if (GetLastError() != ERROR_SUCCESS) 
    {
        //TRACE("AdjustTokenPrivileges(setting) enable failed.\n"); 
		return false;
    }*/
 
    // Prevent the system from shutting down. 
    LogEvent(TEXT("AbortSystemShutdown"));
    fResult = AbortSystemShutdown(NULL); // <---- changed to NULL for local pc

 
    if (!fResult) 
    { 
        //TRACE("AbortSystemShutdown failed.\n"); 

        wchar_t test[512];
        swprintf((wchar_t*)&test, TEXT("AbortSystemShutdown failed! Error=%d"), (DWORD)GetLastError());
        LogEvent((LPCWSTR)&test);
		return false;
    }
	else
	{
		//Cxcutter objexe;
		//objexe.Write_W3C_Log ("ZDEVSERVICE" , "1.0" , "" , "AbortSystemShutdown Successful" , "In CtrlHandler" ,  "" , "");
        LogEvent(TEXT("AbortSystemShutdown succeed"));
	}
		//m_bIsShuttingDown = false; // Reset shut down flag
 

    // Disable shutdown privilege. 
    tkp.Privileges[0].Attributes = 0; 
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
        (PTOKEN_PRIVILEGES) NULL, 0); 
 
    if (GetLastError() != ERROR_SUCCESS) 
    {
       // TRACE("AdjustTokenPrivileges(re-setting) disable failed.\n"); 
		return false;
    }
	return true;
}


Like you see I never changed something special and your service gets the SHUTDOWN notification.
Now its just the question how to abort the shutdown? Maybe this code works on WinXP.

I would say, at first try out some more logging to the eventlog just to find out if your service
catches the event SERVICE_CONTROL_SHUTDOWN. If so then you have to find out where an error occurs.
And make sure every time you test your service really runs as service.

Let me you know what you find out, cause your code is fine.

Greetings Covean
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari23-Oct-09 3:01
Kushagra Tiwari23-Oct-09 3:01 
QuestionRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari26-Oct-09 1:28
Kushagra Tiwari26-Oct-09 1:28 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Covean26-Oct-09 4:05
Covean26-Oct-09 4:05 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari26-Oct-09 20:21
Kushagra Tiwari26-Oct-09 20:21 
QuestionSYBASE's Functions giving LOGIN CRASH problem in VC++ code Pin
Vetukuri Raju22-Oct-09 1:59
Vetukuri Raju22-Oct-09 1:59 
GeneralRe: SYBASE's Functions giving LOGIN CRASH problem in VC++ code Pin
CPallini22-Oct-09 2:33
mveCPallini22-Oct-09 2:33 
QuestionVisual C++ Express, No MFC? Does that matter? Pin
mtthw22-Oct-09 1:51
mtthw22-Oct-09 1:51 
AnswerRe: Visual C++ Express, No MFC? Does that matter? Pin
CPallini22-Oct-09 1:53
mveCPallini22-Oct-09 1:53 
GeneralRe: Visual C++ Express, No MFC? Does that matter? Pin
mtthw22-Oct-09 11:02
mtthw22-Oct-09 11:02 
AnswerRe: Visual C++ Express, No MFC? Does that matter? Pin
Maximilien22-Oct-09 2:45
Maximilien22-Oct-09 2:45 
GeneralRe: Visual C++ Express, No MFC? Does that matter? Pin
mtthw22-Oct-09 11:11
mtthw22-Oct-09 11:11 
AnswerRe: Visual C++ Express, No MFC? Does that matter? Pin
Richard MacCutchan22-Oct-09 2:47
mveRichard MacCutchan22-Oct-09 2:47 
GeneralRe: Visual C++ Express, No MFC? Does that matter? Pin
mtthw22-Oct-09 11:30
mtthw22-Oct-09 11:30 
GeneralRe: Visual C++ Express, No MFC? Does that matter? Pin
Richard MacCutchan22-Oct-09 23:20
mveRichard MacCutchan22-Oct-09 23:20 
AnswerRe: Visual C++ Express, No MFC? Does that matter? Pin
David Crow22-Oct-09 2:58
David Crow22-Oct-09 2:58 
AnswerRe: Visual C++ Express, No MFC? Does that matter? Pin
KarstenK22-Oct-09 21:38
mveKarstenK22-Oct-09 21:38 
JokeRe: Visual C++ Express, No MFC? Does that matter? Pin
Moak24-Oct-09 1:34
Moak24-Oct-09 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.