如何利用BCB寫一個遠端關機的程式 |
答題得分者是:axsoft
|
GGL
資深會員 發表:104 回覆:600 積分:335 註冊:2006-11-05 發送簡訊給我 |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
引言: 因為之前有看到利用WOL的方式將電腦喚醒的方式,所以想試試看能不能利用傳送訊息的方式將遠端的電腦關機?找了很久都沒有看到類似的文章,而我也不知道如何著手,請知道的人教教我吧....謝謝GGL您好: 試試看這個合不合用 原始程式下載處:< href="http://www.matcode.com/rsd-con.c.txt">http://www.matcode.com/rsd-con.c.txt //----------------------------------------------------------- // 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); } }或者用Remote Shut Down Utility包在你的程式裡 下載:Remote Shutdown GUI application (freeware) 以上資訊皆歸Copyright © 2001, MATCODE · E-Mail: info@matcode.com. Delphi.Ktop風紀小隊---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2003/05/08 10:56:41 |
GGL
資深會員 發表:104 回覆:600 積分:335 註冊:2006-11-05 發送簡訊給我 |
|
GGL
資深會員 發表:104 回覆:600 積分:335 註冊:2006-11-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |