今天早晨才看到这个文章,用delphi写了一下,果然好用。
现在把原作者的文章贴出来,大家共享。
现在把原作者的文章贴出来,大家共享。
解决方案 »
- qq传送文件,对话框右边区域显示进度提示
- 如何知道TIdTcpServer与TidTcpClient断开连接?
- delphi中的时间最多能精确到多少位?普通书上说只有秒?
- 如何访问用程序实现域用户登陆?
- 如何将CHART1的曲线赋给另一CHART2?
- 关于OleContainer的问题,我急死了,解决,要多少分给多少分.
- Delphi7 下如何把工程编译为Unicode字符型??
- 各位老大 谁有时间帮我写个MP3的播放器 期末考急用 帮帮我~~~
- 请问如何设置TADOQuery为Readonly,我找不到相关属性
- 关于pascal一问
- 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;
}