今天早晨才看到这个文章,用delphi写了一下,果然好用。
现在把原作者的文章贴出来,大家共享。

解决方案 »

  1.   

    不可思议吗?不,这是WinNT系列的功能,WinNT提供了关闭或重启本地或远程计算机的函数InitiateSystemShutdown,应用这个函数可以轻松实现远程关机/重启。    当然,这是有前提的,就是必须以管理员身份登陆远程计算机,然后才有权限关机。实例程序如下。
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>//这个函数用于获得必要的权限。
    BOOL EnablePrivilege(LPCTSTR lpSystemName,LPCTSTR lpName)
    {
    HANDLE hToken;
    BOOL fOk=FALSE;
    if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
    {
    TOKEN_PRIVILEGES tp;
    tp.PrivilegeCount=1;
    if(!LookupPrivilegeValue(lpSystemName,lpName,&tp.Privileges[0].Luid))
    printf("Can't lookup privilege value.\n");
    tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
    printf("Can't adjust privilege value.\n");
    fOk=(GetLastError()==ERROR_SUCCESS);
    CloseHandle(hToken);
    }
    return fOk;
    }int main()
    {
    char msg[256],cname[256];
    int timeout;
    bool ret;
    printf("1----Shut down local computer.\n2----Reboot local computer.\n");
    printf("3----Shut down remote computer.\n4----Reboot remote computer.\n\n");switch(getch())
    {
    case '1':
    printf("Input the message(Max. 255 chars):");
    scanf("%s",msg);
    printf("Input the timeout value:");
    scanf("%d",&timeout);
    EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
    ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,FALSE);//五个参数分别是:1-计算机名,                                                        //                 2-提示消息,                                                        //                 3-从函数执行到关机的时间(单位是秒),                                                        //                 4-是否强制关闭应用程序,                                                        //                 5-关机还是重启
    break;
    case '2':
    printf("Input the message(Max. 255 chars):");
    scanf("%s",msg);
    printf("Input the timeout value:");
    scanf("%d",&timeout);
    EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
    ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,TRUE);
    break;
    case '3':
    printf("Input the remote computer name:");
    scanf("%s",cname);
    printf("Input the message(Max. 255 chars):");
    scanf("%s",msg);
    printf("Input the timeout value:");
    scanf("%d",&timeout);
    EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
    ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,FALSE);
    break;
    case '4':
    printf("Input the remote computer name:");
    scanf("%s",cname);
    printf("Input the message(Max. 255 chars):");
    scanf("%s",msg);
    printf("Input the timeout value:");
    scanf("%d",&timeout);
    EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
    ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,TRUE);
    break;
    default:
    break;
    }
    if(ret)
    printf("\nSuccess!\n");
    else
    printf("\nFail.\n");
    return ret;
    }
      

  2.   

    我有一疑问,就是:我没有提供用户名和密码,居然可以关闭远程计算机。请问这是为什么?InitiateSystemShutdown(NULL,msg,timeout,TRUE,FALSE);
    我只用了这一句,参数由edit中读取,居然给人家的计算机关了!!
      

  3.   

    还有,就是他的提升权限的代码,如何翻译成delphi?//这个函数用于获得必要的权限。
    BOOL EnablePrivilege(LPCTSTR lpSystemName,LPCTSTR lpName)
    {
    HANDLE hToken;
    BOOL fOk=FALSE;
    if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
    {
    TOKEN_PRIVILEGES tp;
    tp.PrivilegeCount=1;
    if(!LookupPrivilegeValue(lpSystemName,lpName,&tp.Privileges[0].Luid))
    printf("Can't lookup privilege value.\n");
    tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
    printf("Can't adjust privilege value.\n");
    fOk=(GetLastError()==ERROR_SUCCESS);
    CloseHandle(hToken);
    }
    return fOk;
    }
      

  4.   

    function EnableDebugPrivilege: Boolean;function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;varTP: TOKEN_PRIVILEGES;Dummy: Cardinal;beginTP.PrivilegeCount := 1;LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);if bEnable thenTP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLEDelse TP.Privileges[0].Attributes := 0;AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);Result := GetLastError = ERROR_SUCCESS;end;varhToken: Cardinal;beginOpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);if EnablePrivilege(hToken, 'SeDebugPrivilege', True) then ShowMessage('OK');CloseHandle(hToken);end;