Click here to Skip to main content
15,910,009 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 3:39
Kushagra Tiwari22-Oct-09 3:39 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Randor 22-Oct-09 3:49
professional Randor 22-Oct-09 3:49 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 3:47
Kushagra Tiwari22-Oct-09 3:47 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Covean22-Oct-09 3:42
Covean22-Oct-09 3:42 
GeneralRe: Delaying Shutdown through windows service and executing scripts ? Pin
Kushagra Tiwari22-Oct-09 3:48
Kushagra Tiwari22-Oct-09 3:48 
QuestionRe: Delaying Shutdown through windows service and executing scripts ? Pin
David Crow22-Oct-09 4:01
David Crow22-Oct-09 4:01 
AnswerRe: Delaying Shutdown through windows service and executing scripts ? Pin
Covean22-Oct-09 4:08
Covean22-Oct-09 4:08 
QuestionRe: Delaying Shutdown through windows service and executing scripts ? [modified] Pin
Kushagra Tiwari22-Oct-09 5:11
Kushagra Tiwari22-Oct-09 5:11 
Hello Covean,

As per your suggesstion I did the folowing things:

In ServiceMain of my service I did the following code :


m_hServiceStatus = RegisterServiceCtrlHandler(m_szServiceName, _Handler);
if (m_hServiceStatus == NULL)
{
LogEvent(_T("Handler not installed"));
return;
}

and here is the code for _Handler function :

void WINAPI CServiceModule::_Handler(DWORD dwOpcode)
{
char chComputerName[512];
DWORD dwBuff = 512;
GetComputerName(chComputerName,&dwBuff);
if (dwOpcode == SERVICE_CONTROL_SHUTDOWN)
{

AbortShutdown(chComputerName);

}
_Module.Handler(dwOpcode);
}

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

// Get the LUID for shutdown privilege.
LookupPrivilegeValue(lpMachineName, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

// 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.
fResult = AbortSystemShutdown(lpMachineName);

if (!fResult)
{
//TRACE("AbortSystemShutdown failed.\n");
return false;
}
else
{
Cxcutter objexe;

}
//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;
}

But some how the when shutdown is done then the control never comes to this and windows shuts down normailly Frown | :(
Please suggest some thing If I am going wrong somewhere.

Regards,
Kushagra

modified on Thursday, October 22, 2009 11:35 AM

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 
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 

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.