如何遠端關機

有關於C/C++的語法, 程式等
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

如何遠端關機

文章 tim »

原始程式下載處:http://www.matcode.com/rsd-con.c.txt

代碼: 選擇全部

     
    //----------------------------------------------------------- 
    // Remote Shutdown v1.0 Console Mode 
    // Copyright (C) 2002, MATCODE Software 
    // <a href=http://www.matcode.com target=_blank>http://www.matcode.com</a> 
    // 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 namen"); 
           printf("tMessage      - specify message to displayn"); 
           printf("t/t  - time to display message (nn seconds)n"); 
           printf("t/f  - do not force applications with unsaved changes flagn"); 
           printf("t/s  - the computer is to shut down.n"); 
           printf("Example: RSD-CON PC_LARRY This computer will be restarted now. /t20n"); 
    } 
     
    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, Consolen"); 
           printf("Copyright (C) 2002, MATCODE Softwaren"); 
           printf("<a href=http://www.matcode.comn"); target=_blank>http://www.matcode.comn");</a> 
     
           if (GetVersion() & 0x80000000)    // Not Windows NT/2000/XP 
           { 
                  printf("ntThis 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); 
           } 
    } 
多多留言, 整理文章, 把經驗累積下來.....
回覆文章