远程关机WinNT/Win2000/WinXP 今天早晨才看到这个文章,用delphi写了一下,果然好用。现在把原作者的文章贴出来,大家共享。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不可思议吗?不,这是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");elseprintf("\nFail.\n");return ret;} 我有一疑问,就是:我没有提供用户名和密码,居然可以关闭远程计算机。请问这是为什么?InitiateSystemShutdown(NULL,msg,timeout,TRUE,FALSE);我只用了这一句,参数由edit中读取,居然给人家的计算机关了!! 还有,就是他的提升权限的代码,如何翻译成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;} 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; 如何将我写的函数共享给其他单元文件? 请教高手,关于时间的问题!!! 新手问:ShowModal问题? 关于ie扩展的问题(在浏览网站的时候,QQ的工具可以在mouse停到图片上的时候显示出来并可以添加到qq表情里) 如何得到字符串中的数字啊? 实现远程登陆 请各位高手指点 : 送分,简单的告诉having这语句可以做什么效果吗 HOOK 在两张图片人像中,如何比较图片中的头像是否一致,脸的轮廓如何取下比较???在线!!!急!!! delphi是个善解人意的才女!同意请进! TStringList拆分字串为何不对? 请问常量vk_tab在哪个单元中定义?
#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;
}
我只用了这一句,参数由edit中读取,居然给人家的计算机关了!!
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;
}