Article View: borland.public.cppbuilder.nativeapi
Article #6554InitiateSystemShutdown
From: Heinrich Wolf
Date: Sun, 01 May 2005 20:30
Date: Sun, 01 May 2005 20:30
175 lines
5832 bytes
5832 bytes
Hello,
I created a console application RemoteShutdown with these API calls:
LookupPrivilegeValue(Nil, SE_SHUTDOWN_NAME, NewState.Privileges[0].Luid);
LookupPrivilegeValue(Nil, SE_REMOTE_SHUTDOWN_NAME,
NewState.Privileges[1].Luid);
AdjustTokenPrivileges();
InitiateSystemShutdown(MachineName, Message, Timeout, ForceAppsClosed,
RebootAfterShutdown);
The command line arguments are the same as of InitiateSystemShutdown.
Running on Tower,
RemoteShutdown Tower "" 0 1 0
works, but
RemoteShutdown Notebook "" 0 1 0
tells me network path not found.
What is wrong?
Tower is 192.168.1.1
Notebook is 192.168.1.2
ping succeeds
TIA
Heiner
Here is the full code:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
void PrintError(char *ContextFormat, char *ContextParam)
{
char Buffer[0x100];
printf(ContextFormat, ContextParam);
fputs(": ", stdout);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), LANG_NEUTRAL | (SUBLANG_DEFAULT <<
10),
Buffer, sizeof(Buffer), NULL);
puts(Buffer);
}
void main(int AnzArgs, char *Arg[])
{
char *p,
fName[_MAX_FNAME];
HANDLE TokenHandle;
TOKEN_PRIVILEGES *NewState = NULL,
*PreviousState = NULL,
*TempState = NULL;
DWORD NewSize,
PreviousSize,
TempSize,
dwTimeout;
LPTSTR lpMachineName,
lpMessage;
BOOL ok = TRUE,
bForceAppsClosed,
bRebootAfterShutdown;
if (AnzArgs != 6)
{
puts("Too few arguments");
ok = FALSE;
}
else
{
lpMachineName = Arg[1];
if (strlen(Arg[2]))
lpMessage = Arg[2];
else
lpMessage = NULL;
dwTimeout = strtol(Arg[3], &p, 0);
if (*p)
{
printf("invalid Timeout %s\n", Arg[3]);
ok = FALSE;
}
bForceAppsClosed = strtol(Arg[4], &p, 0);
if (*p || bForceAppsClosed > 1)
{
printf("invalid Force %s\n", Arg[4]);
ok = FALSE;
}
bRebootAfterShutdown = strtol(Arg[5], &p, 0);
if (*p || bRebootAfterShutdown > 1)
{
printf("invalid Reboot %s\n", Arg[5]);
ok = FALSE;
}
NewSize = sizeof(*NewState) - sizeof(NewState->Privileges)
+ 2 * sizeof(NewState->Privileges[0]);
PreviousSize = NewSize;
TempSize = NewSize;
NewState = (TOKEN_PRIVILEGES *) malloc(NewSize);
PreviousState = (TOKEN_PRIVILEGES *) malloc(PreviousSize);
TempState = (TOKEN_PRIVILEGES *) malloc(TempSize);
if (NewState)
memset(NewState, 0, NewSize);
else
{
puts("cannot allocate new privileges");
ok = FALSE;
}
if (PreviousState)
memset(PreviousState, 0, PreviousSize);
else
{
puts("cannot allocate previous privileges");
ok = FALSE;
}
if (TempState)
memset(TempState, 0, TempSize);
else
{
puts("cannot allocate temporary privileges");
ok = FALSE;
}
}
if (! ok)
{
_splitpath(Arg[0], NULL, NULL, fName, NULL);
printf("%s <Host name> <Message> <Timeout sec.> <Bool Force> <Bool
Reboot>\n",
fName);
}
else
{
ok = FALSE;
if (! OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&TokenHandle))
PrintError("OpenProcessToken", "");
else
{
NewState->PrivilegeCount = 2;
NewState->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
NewState->Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
if (! LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&NewState->Privileges[0].Luid))
PrintError("LookupPrivilegeValue(%s)", SE_SHUTDOWN_NAME);
else
if (! LookupPrivilegeValue(NULL, SE_REMOTE_SHUTDOWN_NAME,
&NewState->Privileges[1].Luid))
PrintError("LookupPrivilegeValue(%s)", SE_REMOTE_SHUTDOWN_NAME);
else
if (! AdjustTokenPrivileges(
TokenHandle, FALSE, NewState, PreviousSize,
PreviousState, &PreviousSize))
PrintError("AdjustTokenPrivileges(%s)", "enable");
else
ok = TRUE;
}
}
if (ok)
{
if (InitiateSystemShutdown(
lpMachineName, lpMessage, dwTimeout, bForceAppsClosed,
bRebootAfterShutdown))
puts("InitiateSystemShutdown succeeded");
else
PrintError("InitiateSystemShutdown", "");
if (! AdjustTokenPrivileges(
TokenHandle, FALSE, PreviousState, TempSize,
TempState, &TempSize))
PrintError("AdjustTokenPrivileges(%s)", "disable");
}
if (NewState)
free(NewState);
if (PreviousState)
free(PreviousState);
if (TempState)
free(TempState);
}
Technical Details
Message-ID:
<3dksi9F6tfu9pU1@individual.net>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ab26fb2e2becedff,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Heinrich Wolf" <invalid@invalid.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: InitiateSystemShutdown Date: Sun, 1 May 2005 22:30:03 +0200 Lines: 178 Message-ID: <3dksi9F6tfu9pU1@individual.net> X-Trace: individual.net 99wFkmudtR8Q3uCY0xrFKg3rMauGaHo2sra/xcjxfr+f1UcasZ X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2527 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527