Shutdown Implementation (delphi) when the network connection is interrupted

Well, this feature is not difficult, is to use several functions. It took me a few hours to complete, just learning delphi few days, always look for the use of api functions, and later in the shutdown privilege card .. there erase / ..

InternetGetConnectedState(nil, 0) //这个函数需要调用winInet头文件,此函数返回本地系统的网络连接状态。
 
 

The function parameters and return values

BOOL InternetGetConnectedState(

  __out LPDWORD lpdwFlags,
  __in DWORD dwReserved
  );

 lpdwFlags[out]  

------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It points to a variable , connected to the variable that receives the description. This parameter can still return a valid flag when the function returns FLASE. The parameter may be one or more of the following values.
value meaning
INTERNET_CONNECTION_CONFIGURED0x40 Local system has a valid connection to the Internet, but it might or might not be currently connected.
INTERNET_CONNECTION_LAN 0x02 Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM0x01 Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY0x08 No longer used.
INTERNET_CONNECTION_OFFLINE 0x20 Local system is in offline mode.
INTERNET_CONNECTION_PROXY0x04 Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED0x10 Local system has RAS installed.
   dwReserved[in]  
Retention. It must be zero.

return value

  When there is a modem or a LAN connection, it returns TRUE, when there is no internet connection or all of the current connection is not activated, returns false.
  When the function returns false, the program can call GetLastError be received error code .

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If the function returns TRUE, indicating that at least a connection is valid. It does not guarantee that this is even a valid connection to a specific host . Should always check the program returns to the server using the API connection error code , is used to determine the connection status. Use InternetCheckConnection function may determine whether a host connected to the specified connection establishment.

  Also it shows a return value is TRUE active modem connection or a LAN connection is active. Representatives of the modem and LAN FALSE not in a connected state. If it returns FALSE, INTERNET_CONNECTION_CONFIGURED identity will be set to indicate that the automatic dialing is set to "Always Dial", but is not currently active. If the automatic dialing is not set, the function returns FALSE.
  As other additional functions WinINet API, this function is not from DLLMain or global constructor , destructor secure calls.

note

  WinINet不支持实现服务器。另外,它也不应该用来作为服务。实现服务器或者服务可用Microsoft Windows HTTP Services (WinHTTP)

---------------------------------------------------------------------------------- 下面是对窗口进行提权(SE_SHUTDOWN_NAME )关机权限---------------------------------------------------------------

var
  hToken:Thandle;
  tp:TOKEN_PRIVILEGES;
  buf:Cardinal;
  aLuid:Int64;
begin
  OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hToken);
  tp.PrivilegeCount:=1;
  LookupPrivilegeValue(nil,'SeShutdownPrivilege',aLuid);
  tp.Privileges[0].Luid:=aLuid;
  tp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  AdjustTokenPrivileges(hToken,false,tp,SizeOf(tp),nil,buf);
  CloseHandle(hToken);
end;

代码也是我复制过来,以后有时间在解释一下吧!!

-----------------------------------------------------------------------------------------------这个是关机函数ExitWindowsEx---------------------------------------------------------------------------------------------

参数

ExitWindowsEX()函数用来退出、重启或注销系统。

  函数原型:
  BOOL ExitWindowsEx(
  UINT uFlags, // 关闭参数
  DWORD dwReserved // 系统保留,一般取0
  );



参数uFlag

  参数:uFlags
  指定关闭的类型。此参数必须有下列值的组合:

EWX_FORCE

  EWX_FORCE
  强制终止进程。当此标志设置,Windows不会发送消息WM_QUERYENDSESSION和WM_ENDSESSION的消息给目前在系统中运行的程序。这可能会导致应用程序丢失数据。因此,你应该只在紧急情况下使用此标志。

EWX_LOGOFF

  EWX_LOGOFF
  关闭所有进程,然后注销用户。

EWX_POWEROFF

  EWX_POWEROFF
  关闭系统并关闭电源。该系统必须支持断电。
  Windows要求:
  Windows NT中调用进程必须有 SE_SHUTDOWN_NAME 特权。
  Windows 9X中:可以直接调用。

EWX_REBOOT

  EWX_REBOOT
  关闭系统,然后重新启动系统。
  Windows要求:
  Windows NT中:调用进程必须有SE_SHUTDOWN_NAME特权。
  Windows 9X中:可以直接调用。

EWX_SHUTDOWN

  EWX_SHUTDOWN
  关闭系统,安全地关闭电源。所有文件缓冲区已经刷新到磁盘上,所有正在运行的进程已经停止。
  Windows要求:
  Windows NT中:调用进程必须有SE_SHUTDOWN_NAME特权。
  Windows 9X中:可以直接调用。

参数dwReserved

  参数:dwReserved
  保留,这参数被忽略。一般取0。
  返回值
  如果函数成功,返回值为非零。
  如果函数失败,返回值是零。想获得更多 错误信息 ,请调用GetLastError函数。

编辑本段备注

  ExitWindowsEx函数返回后,启动了关闭。关闭或注销。
  在关机或登录操作中,应用程序在允许关闭的时间具体数额内回应关机请求。如果时间到期时,Windows会显示一个对话框,允许用户强行关闭应用程序:关闭、重试,或取消关机要求。如果存在EWX_FORCE指定值,Windows会关闭应用程序而不显示该对话框。
  Windows NT中:关闭或重新启动系统,调用进程必须使用AdjustTokenPrivileges函数使SE_SHUTDOWN_NAME特权。Windows 95中:安全特权,不支持或需要。


Guess you like

Origin blog.csdn.net/showmessage0804/article/details/7952581