| View previous topic :: View next topic |
| Author |
Message |
Rubes_sw Valued Contributor


Joined: 11 Jun 2001 Posts: 625 Location: Northern Ireland
|
Posted: Thu Mar 20, 2003 1:15 pm Post subject: Remote Shutdown Code ! |
|
|
I found this:
| Code: | //-----------------------------------------------------------
// Remote Shutdown v1.0 Console Mode
// Copyright (C) 2002, MATCODE Software
// http://www.matcode.com
// Author: Vitaly Evseenko
//-----------------------------------------------------------
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#pragma hdrstop
int RemoteShutdown(LPSTR lpMachineName, LPSTR lpMessage,
DWORD dwTimeout, BOOL bForceAppsClosed,
BOOL bRebootAfterShutdown )
{
HANDLE hToken;
TOKEN_PRIVILEGES TokenPrivileges;
OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ;
LookupPrivilegeValue( NULL, SE_REMOTE_SHUTDOWN_NAME, &(TokenPrivileges.Privileges[0].Luid));
TokenPrivileges.PrivilegeCount = 1;
TokenPrivileges.Privileges[0].Attributes = 2;
AdjustTokenPrivileges( hToken, FALSE, &TokenPrivileges,
sizeof(TOKEN_PRIVILEGES), NULL, NULL );
if(!InitiateSystemShutdown(
lpMachineName, // name of computer to shut down
lpMessage, // address of message to display
dwTimeout, // time to display dialog box
bForceAppsClosed, // force applications with unsaved changes flag
bRebootAfterShutdown ))
{
return GetLastError();
}
return 0;
}
void OutUsage(void)
{
printf("\nUsage: RSD-CON ComputerName [Message] [/tnn] [/f] [/s]\n");
printf("\tComputerName - remote computer name\n");
printf("\tMessage - specify message to display\n");
printf("\t/t - time to display message (nn seconds)\n");
printf("\t/f - do not force applications with unsaved changes flag\n");
printf("\t/s - the computer is to shut down.\n");
printf("Example: RSD-CON PC_LARRY This computer will be restarted now. /t20\n");
}
void main( int argc, char *argv[] )
{
char szMachineName[100];
char szMessage[200];
DWORD dwTimeout;
BOOL bForceAppsClosed;
BOOL bRebootAfterShutdown;
int i, Err;
printf("Remote Shutdown v1.0, Console\n");
printf("Copyright (C) 2002, MATCODE Software\n");
printf("http://www.matcode.com\n");
if (GetVersion() & 0x80000000) // Not Windows NT/2000/XP
{
printf("\n\tThis is a Windows NT/2000/XP application.\n"
"This program will not work on Windows 95/98/ME !\n");
return;
}
if(argc<2)
{
OutUsage();
return;
}
strcpy(szMachineName, argv[1]);
dwTimeout = 0;
bForceAppsClosed = TRUE;
bRebootAfterShutdown = TRUE;
szMessage[0] = '\0';
for( i = 2; i < argc; i++ )
{
// if not started with / then message ;-)
if( argv[i][0] != '/')
{
strcat(szMessage, argv[i]);
strcat(szMessage, " ");
continue;
}
// parse option type
if(argv[i][1]=='t' || argv[i][1]=='T')
{
dwTimeout = atol(&argv[i][2]);
}
else if(argv[i][1]=='f' || argv[i][1]=='F')
{
bForceAppsClosed = FALSE;
}
else if(argv[i][1]=='s' || argv[i][1]=='S')
{
bRebootAfterShutdown = FALSE;
}
}
if (dwTimeout == 0 && szMessage[0])
{
dwTimeout = 5;
}
Err = RemoteShutdown(szMachineName, szMessage,
dwTimeout, bForceAppsClosed,
bRebootAfterShutdown );
if(Err)
{
LPSTR lpstErr = "\0";
if(Err == 53)
{
lpstErr = "The network path was not found.\n"
"Invalid computer name or is not Windows NT/2000/XP machine.\n";
}
else if(Err == 5)
{
lpstErr = "Access is denied. You have no administrative rights on the specified computer.\n";
}
printf("\nUnable to shutdown computer %s, Error: %d.\n%s",
szMachineName, Err, lpstErr);
OutUsage();
}
else
{
printf("\nComputer %s is shut down.\n", szMachineName);
}
} |
I don't know what language it's in, and whether we need the authors permission : But can someone convert this to a DLL ?
Authors Details:
http://www.matcode.com/
Copyright © 2001, MATCODE · E-Mail: info@matcode.com.
Nathan |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Mar 20, 2003 1:22 pm Post subject: |
|
|
I've seen this code before but I don't think this is very useful... It looks
like it's written in C++. The problem with the code is that is doesn't
power off a computer but it only gets it to the point where "It is
now safe to power off" message. The same thing as this code can be
achieved with the DOS SHUTDOWN command.
| Quote: | Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]
No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without warning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer less than 256)
yy is the minor reason code (positive integer less than 65536) |
_________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
ShinobiSoft Professional Member


Joined: 06 Nov 2002 Posts: 790 Location: Knoxville, Tn
|
Posted: Thu Mar 20, 2003 1:40 pm Post subject: |
|
|
Actually it's a console mode program written in C/C++ and it will only run on
Windows NT/2000/XP. From the code I'm reading this will shutdown a
computer remotely and display a message to the user if specified in the
command line.  _________________ Bill Weckel
ShinobiSoft Software
"The way is known to all, but not all know it." |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Mar 20, 2003 1:45 pm Post subject: |
|
|
Yes, but will it power off the remote computer? With the DOS
shutdown command it will display a message etc. like you said.
Also, it is written in C or C++? I've got LCC-WIN32 sitting around here
and if it's C I might want to compile it.  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Mar 20, 2003 1:48 pm Post subject: |
|
|
And there's also this on www.matcode.com -- undocwin.h:
| Code: | /**********************************************************************/
/* Undocumented Windows Functions
/* Copyright (C) 2001, MATCODE Software
/* http://www.matcode.com
/* Author: Vitaly Evseenko
/**********************************************************************/
//
// WARNING! This file contained the functions for a different
// platforms (OS).
// To use these functions in your application you need to comment out
// undue functions.
//////////////////////////////////////////////////////////////
// Function SwitchToThisWindow
// Import: user32.dll
//
// Descryption: The SwitchToThisWindow function
// forced switch to the thread that created the specified
// window into the foreground, activates and focused the window.
// Keyboard input is directed to the window.
// Unlike SetFocus and SetForegroundWindow, SwitchToThisWindow
// forced switch anyway even if console application work in
// full-screen window mode!
//
// Arguments: hAppWnd - Identifies the main thread window that
// should be activated, focused and brought to the foreground.
// bSwitch - TRUE - switch to the window;
// FALSE - switch back.
// Result: If the function succeeds, the return value is nonzero.
// Platform: Windows 95/98/ME/NT/2000/XP
//
BOOL __stdcall SwitchToThisWindow(HWND hAppWnd, BOOL bSwitch);
//////////////////////////////////////////////////////////////
// Function NtShutdownSystem
// Import: ntdll.dll
//
// Descryption: The NtShutdownSystem function either shuts down,
// reboot, or shuts down and restarts the system.
// The NtShutdownSystem function always forces shutting down the
// system and does not wait for third level applications.
//
// Comment: The calling process must use the
// AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
// privilege.
// Arguments: dwShutDown - Specifies the type of shutdown.
// This parameter must one of the following values:
// 0 - FORCE SHUTDOWN WITHOUT SWITHING OFF THE COMPUTER
// 1 - FORCE REBOOT
// 2 - FORCE SHUTDOWN WITH SWITH OFF THE COMPUTER
//
// Result: If the function fail, the return value is NTSTATUS.
// Platform: Windows NT/2000/XP
DWORD __stdcall NtShutdownSystem( DWORD dwShutDown );
//////////////////////////////////////////////////////////////
// Function LdrLoadDll
// Import: ntdll.dll
//
// Descryption: The LdrLoadDll function maps the specified
// executable module into the address space of the calling
// process. Unlike LoadLibraryA(W) this function loads
// any modules for any others (i.e. loading DLL from another
// forein DLL in Win2000).
//
// Arguments: szcwPath - Points to a null-terminated
// wide character string that containes the "path"
// variable where the system will look for the module.
// szcwPath should have following format:
// L"Path1;Path2;Path3;....;PathN;".
// If szcwPath equal NULL, LdrLoadDll function
// will perform search as LoadLibrary.
//
// pdwLdrErr - could be NULL;
//
// pUniModuleName - Points to an UNICODE_STRING
// that names a Win32 executable module (either a .DLL
// or an .EXE file).
//
// pResultInstance - should point to a variable that receaves
// the Identifier of the module.
//
// Result: If the function succeeds, the return value is zero (STATUS_SUCCESS).
// Platform: Windows NT/2000/XP
DWORD __stdcall LdrLoadDll( PWSTR *szcwPath, // Optional
PDWORD pdwLdrErr, // Optional
PUNICODE_STRING pUniModuleName,
PHINSTANCE pResultInstance
);
//////////////////////////////////////////////////////////////
// Function LdrGetProcedureAddress
// Import: ntdll.dll
//
// Descryption: The LdrGetProcedureAddress function retrives
// the address of the specified exported function.
//
// Arguments: hModule - Identifies the module that contains
// the function.
//
// pAnsiFunctionName - Points to an ANSI_STRING
// containing the function name, or specifies
// the function's ordinal value.
//
// dwUnknown - Unknown should be zero.
//
// ProcAddress - should point to a variable that receaves
// the address of the function.
//
// Result: If the function succeeds, the return value is zero (STATUS_SUCCESS).
// Platform: Windows NT/2000/XP
DWORD __stdcall LdrGetProcedureAddress( HINSTANCE hModule,
PANSI_STRING pAnsiFunctionName,
DWORD dwUnknown,
PFARPROC ProcAddress
); |
Will that help power the remote computer off? _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
ShinobiSoft Professional Member


Joined: 06 Nov 2002 Posts: 790 Location: Knoxville, Tn
|
Posted: Thu Mar 20, 2003 1:48 pm Post subject: |
|
|
From my intrepretation of the code, and I program in C/C++ also, it will
shutdown a remote computer.
And yes it is written in C/C++. You can compile it either way.  _________________ Bill Weckel
ShinobiSoft Software
"The way is known to all, but not all know it." |
|
| Back to top |
|
 |
ShinobiSoft Professional Member


Joined: 06 Nov 2002 Posts: 790 Location: Knoxville, Tn
|
Posted: Thu Mar 20, 2003 1:51 pm Post subject: |
|
|
FF the file you just posted just has a couple of simple functions. Nothing to
do with remotely shutting down a computer in there.  _________________ Bill Weckel
ShinobiSoft Software
"The way is known to all, but not all know it." |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Mar 20, 2003 1:55 pm Post subject: |
|
|
As you can see I'm a complete beginner in C/C++/Delphi/etc. and I
have a LOT to learn about it. Someday I'd like to be able to make my
own DLLs though!  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
ShinobiSoft Professional Member


Joined: 06 Nov 2002 Posts: 790 Location: Knoxville, Tn
|
Posted: Thu Mar 20, 2003 1:58 pm Post subject: |
|
|
Don't let yourself get frustrated with it, and don't try to learn too much at
once or you'll drive yourself crazy, trust me.
BTW, any questions ya need answered in the C/C++ department I'll be
more than glad to try and answer for ya.  _________________ Bill Weckel
ShinobiSoft Software
"The way is known to all, but not all know it." |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Mar 20, 2003 2:26 pm Post subject: |
|
|
Thanks.  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Thu Mar 20, 2003 2:36 pm Post subject: |
|
|
Hi Rubes_sw,
I will make you a VDS DLL with just this function that you posted. It is no big deal  _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|