windows主机控制
一、开关机控制
using System.Runtime.InteropServices; //注销、关机、重启
class shutdown
{
[StructLayout(LayoutKind.Sequential, Pack = )]
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 DoFlag, 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 DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = ;
tp.Luid = ;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, , IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, );
} public void Reboot()//重启
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
} public void PowerOff()//关机
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
} public void LogOff()//注销
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
} }
二、音量控制
1.user32
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
const uint WM_APPCOMMAND = 0x319;
const uint APPCOMMAND_VOLUME_UP = 0x0a;
const uint APPCOMMAND_VOLUME_DOWN = 0x09;
const uint APPCOMMAND_VOLUME_MUTE = 0x08;
public void volumeUp() {//音量加
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
}
public void volumeDown()
{//音量减
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
}
public void volumeMuet()
{//静音或取消
SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);
}
2.CoreAudioApi
using CoreAudioApi;
using System.Runtime.InteropServices; namespace LHProgectClient
{
class volumeControl
{
//实例化CoreAudioApi里的类
MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = null; //初始化
public volumeControl()
{
defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
} //是否静音
public bool MUTE
{
get { return defaultDevice.AudioEndpointVolume.Mute; }
set { defaultDevice.AudioEndpointVolume.Mute = value; }
} //音量加
public void volumeUp()
{
defaultDevice.AudioEndpointVolume.VolumeStepUp();
}
//音量减
public void volumeDown()
{
defaultDevice.AudioEndpointVolume.VolumeStepDown();
}
//静音、取消
public void volumeMute()
{
defaultDevice.AudioEndpointVolume.Mute = !defaultDevice.AudioEndpointVolume.Mute;
}
}
}
windows主机控制的更多相关文章
- Windows主机与centOS虚拟机之间"ping不通"
为什么要遇到这个问题 这是我重新安装centOS7.5虚拟机之后遇到的问题——我需要安装一个SecureCRT工具,结果主机与虚拟机没有ping通. 在安装这个工具之前需要进行主机与虚拟机的相互pin ...
- 虚拟机和windows主机中的文件共享
22:54 2015/12/22 虚拟机和windows主机中的文件共享:特别推荐:我的一个老师特别推荐的方法:在windows安装SSH Secure File Transfer Client,直接 ...
- 怎么把Windows主机上的目录共享到Ubuntu上
使用Oracle VM VirtualBox在Windows主机上创建了一台Ubuntu虚拟机,怎么把宿主机上的目录共享到Ubuntu上,可使用以下方法: eg.把Windows主机上D盘里的test ...
- Linux主机共享目录给Windows主机的方法
Linux主机共享目录可以通过samba来实现 首先,来看下百科上关于samba的介绍: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Ser ...
- zabbix监控windows主机网卡流量
监控windows主机网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 客户端配置:(172.30.1.120,wi ...
- Cacti监控Windows主机,Windows主机的正确配置
使用cacti监控Windows主机的时候经常遇到无法获取Windows主机的snmp信息和Windows主机的硬件信息,主要原因是Windows主机没有正确配置snmp,以下是正确的配置步骤:1.安 ...
- Nagios利用NSClient++监控Windows主机
在Nagios的libexec下有check_nt这个插件,它就是用来检查windows机器的服务的.其功能类似于check_nrpe.不过还需要搭配另外一个软件NSClient++,它则类似于NRP ...
- Vmware10中Centos7挂载Windows主机的共享文件夹,提示:Error: cannot mount filesystem: No such device
1.设置共享权限 2.安装VMware tools 点击虚拟机 点击安装 VMware tools 将/run/media/zhaojq/VMware\ Tools 目录下的VMwareTools-9 ...
- [转帖]Ansible批量远程管理Windows主机(部署与配置)
2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统: CentOS6.8 IP ...
随机推荐
- Datetime与Datetime2的区别
Datetime: 时间格式,对应于数据库中的DateTime类型,对应于.NET里面的System. DateTime类型.DateTime支持日期从1753年1月1日到9999年12 ...
- springmvc常用jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans ...
- Windows模拟linux终端工具Cmder+Gow
1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...
- Mysql性能优化一:SQL语句性能优化
这里总结了52条对sql的查询优化,下面详细来看看,希望能帮助到你 1, 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2,应尽量避免在 w ...
- Queue模块初识
Queue模块实现了多生产者.多消费者队列.它特别适用于信息必须在多个线程间安全地交换的多线程程序中.这个模块中的Queue类实现了所有必须的锁语义.它依赖于Python中线程支持的可用性:参见thr ...
- linux备忘录-账号管理与ACL权限设定
知识 账号管理中的一些文件结构 /etc/passwd 每一行的内容都为下面结构 账号名称:密码:UID:GID:用户信息说明:家目录:shell ---- UID ---- -- 0 -> 代 ...
- ExtJS6.0扩展日期选择控件为也可以选择时间
PS:ExtJS自带的日期选择控件只能够选择日期,但是现在的需求需要精确到秒,所以在网上搜索了一些例子(大部分是4.0的)作为参考,然后改出了6.0可用的一个日期时间选择控件. 1.找到extjs6. ...
- Linux上删除空行的方法
grep . data.txt grep-v'^$' data.txt grep'[^$]' data.txt sed'/^$/d' data.txt sed'/^\s*$/d' data.txt # ...
- idea tomcat 启动报错 org.apache.catalina.core.StandardService.initInternal Failed to initialize connector
org.apache.catalina.core.StandardService.initInternal Failed to initialize connector org.apache.cata ...
- input只改变光标的颜色 不改变字的颜色
color: red; text-shadow: 0px 0px 0px #000; -webkit-text-fill-color: transparent;把这些放到input里文字通过阴影实现 ...