Windows 下用 API 控制系统的锁定/注销/重启/关机

在 Windows 下,有个函数叫 ExitWindowsEx ,可以用来控制系统的锁定、登出、重启和关机。主要就是通过给该函数传入不同的值来执行不同的操作。掌握该函数的使用就能更好的控制你的计算机。

ExitWindowsEx函数原型

传入值和对于功能

EWX_HYBRID_SHUTDOWN(0x00400000)
从 Windows8 开始的混合关机模式。
Beginning with Windows 8: You can prepare the system for a faster startup by combining the EWX_HYBRID_SHUTDOWN flag with the EWX_SHUTDOWN flag.

EWX_LOGOFF(0)
关闭所有进程并注销。
Shuts down all processes running in the logon session of the process that called the ExitWindowsEx function. Then it logs the user off.

EWX_POWEROFF(0x00000008)
关闭系统并切断电源,系统支持电源关闭功能。
Shuts down the system and turns off the power. The system must support the power-off feature.

EWX_REBOOT(0x00000002)
关闭系统并重启。
Shuts down the system and then restarts the system.

EWX_RESTARTAPPS(0x00000040)
关闭系统并重启,被注册到重启项的应用将一起重启。
Shuts down the system and then restarts it, as well as any applications that have been registered for restart using the RegisterApplicationRestart function.

EWX_SHUTDOWN(0x00000001)
关闭系统并在安全状态下关闭电源。
Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
Continue Reading…

Windows 下根据进程ID获取进程名

在 Windows 下,如果要获取打开的某个前台窗口所对应的后台进程名,是没有现成的函数的。但是这并不是意味着我们不能利用微软提供的其他API来实现该功能。
其中一种实现思路就是:
1.利用GetWindowThreadProcessId获取创建指定窗口线程的标识和创建窗口的进程的标识符;
2.利用CreateToolhelp32Snapshot和Process32First建立系统中全部进程的快照并获取进程信息;
3.然后使用该函数得到的进程ID号,在快照信息中进行匹配,匹配到相应的进程,输出进程名。

通过进程号获取进程名

Continue Reading…

Windows Socket C/S 多线程通信程序

在 Socket 编程中,发送数据的函数 send() 和接收数据的函数 recv() ,往往需要等待发送和接收完成返回相应的值后才能执行下一条指令。所以当我们把 send() 和 recv() 放到同一个函数流程里的时候往往会导致I/O阻塞,无法实现同时收发数据。使用多线程编程,将监听函数、接收函数和发送函数分别放入不同的线程中,可以很好的避免出现以上问题。

如下为 Windows 下使用 Socket 编写 C/S 架构多线程通信 Demo ,代码很简单。

流程图

socket
Continue Reading…