| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 988 人关注过本帖
标题:[求助]高手帮帮忙,关于C#实现关机]
只看楼主 加入收藏
nova
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-4-14
收藏
 问题点数:0 回复次数:8 
[求助]高手帮帮忙,关于C#实现关机]
麻烦请问:  
如何在WIN XP中实现关机?
具体的函数是什么,最好给个示例!!!
谢谢
搜索更多相关主题的帖子: 示例 WIN 函数 
2005-04-21 19:08
nova
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-4-14
收藏
得分:0 
没人,自己顶一下,再问个问题
请问为什么C#的可执行程序 移植性很差?????
如何提高可移植性,即封装/

2005-04-21 19:10
eastsnake
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2005-3-8
收藏
得分:0 

using System; using System.Runtime.InteropServices; class shoutdown{ [StructLayout(LayoutKind.Sequential, Pack=1)] internal struct TokPriv1Luid { public int Count; public long Luid; public int Attr; }

[DllImport("kernel32.dll", ExactSpelling=true)] internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true)] internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError=true)] internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true)] internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)] internal static extern bool ExitWindowsEx(int flg, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002; internal const int TOKEN_QUERY = 0x00000008; internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; internal const int EWX_LOGOFF = 0x00000000; internal const int EWX_SHUTDOWN = 0x00000001; internal const int EWX_REBOOT = 0x00000002; internal const int EWX_FORCE = 0x00000004; internal const int EWX_POWEROFF = 0x00000008; internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin(int flg) { bool ok; TokPriv1Luid tp; IntPtr hproc = GetCurrentProcess(); IntPtr htok = IntPtr.Zero; ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, ref htok); tp.Count = 1; tp.Luid = 0; tp.Attr = SE_PRIVILEGE_ENABLED; ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid); ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); ok = ExitWindowsEx(flg, 0); }

public static void Main() { Console.WriteLine("正在关闭计算机……"); // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。 // 在XP下可以看到帮助信息,以得到不同得参数 // SHUTDOWN /? DoExitWin(EWX_SHUTDOWN); } }

[此贴子已经被live41于2005-4-27 18:02:21编辑过]


程序员是男孩,语言是女孩; 每个男孩都希望能交往更多的女孩; 但是却没有一个男孩真正了解一个女孩; 男孩总是不能专心一个女孩,而女孩却总是在变~
2005-04-25 09:17
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
唉,C#真是没用,连关个机都要用API。
2005-04-27 18:03
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
我记得Management里面好象有,看看.NET文档里面的说明吧~

寻求挑战,追求完美 Oh,my god!
2005-04-28 14:51
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
关机要这么麻烦阿?

2005-04-28 23:33
angel_bear
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2005-4-12
收藏
得分:0 
太麻烦了呀
2005-04-29 16:31
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
I sun your mother~~~!!!谁说C#移植性很差!!
C#就是靠数据封装强出名的!!
I sun your mother!

寻求挑战,追求完美 Oh,my god!
2005-05-03 22:30
lpp333
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-5-8
收藏
得分:0 
楼上的这位 不知所云
2005-05-08 00:47
快速回复:[求助]高手帮帮忙,关于C#实现关机]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018959 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved