全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2993
推到 Plurk!
推到 Facebook!

WM_WTSSESSION_CHANGE

尚未結案
greyair
一般會員


發表:1
回覆:0
積分:0
註冊:2008-11-29

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-11-29 20:12:00 IP:121.63.xxx.xxx 訂閱
delphi 如何掛上windows的登入/登出event?
我用WM_WTSSESSION_CHANGE 怎么無法獲取消息?
以下是參考網路上的一個例子,可是無法獲取事件,各位大大幫忙看看。


{********************************************************************
Unit Wtsapi
********************************************************************}

Unit Wtsapi;

interface

{
Copyright(c) By Thomas Stutz
Date: 10. April 2002
Version: 1.0.0
}

uses
Windows;

const
// The WM_WTSSESSION_CHANGE message notifies applications of changes in session state.
WM_WTSSESSION_CHANGE = $2B1;

// wParam values:
WTS_CONSOLE_CONNECT = 1;
WTS_CONSOLE_DISCONNECT = 2;
WTS_REMOTE_CONNECT = 3;
WTS_REMOTE_DISCONNECT = 4;
WTS_SESSION_LOGON = 5;
WTS_SESSION_LOGOFF = 6;
WTS_SESSION_LOCK = 7;
WTS_SESSION_UNLOCK = 8;
WTS_SESSION_REMOTE_CONTROL = 9;

// Only session notifications involving the session attached to by the window
// identified by the hWnd parameter value are to be received.
NOTIFY_FOR_THIS_SESSION = 0;
// All session notifications are to be received.
NOTIFY_FOR_ALL_SESSIONS = 1;


function RegisterSessionNotification(Wnd: HWND; dwFlags: DWORD): Boolean;
function UnRegisterSessionNotification(Wnd: HWND): Boolean;
function GetCurrentSessionID: Integer;

implementation

function RegisterSessionNotification(Wnd: HWND; dwFlags: DWORD): Boolean;
// The RegisterSessionNotification function registers the specified window
// to receive session change notifications.
// Parameters:
// hWnd: Handle of the window to receive session change notifications.
// dwFlags: Specifies which session notifications are to be received:
// (NOTIFY_FOR_THIS_SESSION, NOTIFY_FOR_ALL_SESSIONS)
type
TWTSRegisterSessionNotification = function(Wnd: HWND; dwFlags: DWORD): BOOL; stdcall;
var
hWTSapi32dll: THandle;
WTSRegisterSessionNotification: TWTSRegisterSessionNotification;
begin
Result := False;
hWTSAPI32DLL := LoadLibrary('Wtsapi32.dll');
if (hWTSAPI32DLL > 0) then
begin
try @WTSRegisterSessionNotification :=
GetProcAddress(hWTSAPI32DLL, 'WTSRegisterSessionNotification');
if Assigned(WTSRegisterSessionNotification) then
begin
Result:= WTSRegisterSessionNotification(Wnd, dwFlags);
end;
finally
if hWTSAPI32DLL > 0 then
FreeLibrary(hWTSAPI32DLL);
end;
end;
end;

function UnRegisterSessionNotification(Wnd: HWND): Boolean;
// The RegisterSessionNotification function unregisters the specified window
// Parameters:
// hWnd: Handle to the window
type
TWTSUnRegisterSessionNotification = function(Wnd: HWND): BOOL; stdcall;
var
hWTSapi32dll: THandle;
WTSUnRegisterSessionNotification: TWTSUnRegisterSessionNotification;
begin
Result := False;
hWTSAPI32DLL := LoadLibrary('Wtsapi32.dll');
if (hWTSAPI32DLL > 0) then
begin
try @WTSUnRegisterSessionNotification :=
GetProcAddress(hWTSAPI32DLL, 'WTSUnRegisterSessionNotification');
if Assigned(WTSUnRegisterSessionNotification) then
begin
Result:= WTSUnRegisterSessionNotification(Wnd);
end;
finally
if hWTSAPI32DLL > 0 then
FreeLibrary(hWTSAPI32DLL);
end;
end;
end;

function GetCurrentSessionID: Integer;
// Getting the session id from the current process
type
TProcessIdToSessionId = function(dwProcessId: DWORD; pSessionId: DWORD): BOOL; stdcall;
var
ProcessIdToSessionId: TProcessIdToSessionId;
hWTSapi32dll: THandle;
Lib : THandle;
pSessionId : DWord;
begin
Result := -1;
Lib := GetModuleHandle('kernel32');
if Lib <> 0 then
begin
ProcessIdToSessionId := GetProcAddress(Lib, '1ProcessIdToSessionId');
if Assigned(ProcessIdToSessionId) then
begin
ProcessIdToSessionId(GetCurrentProcessId(), DWORD(@pSessionId));
Result:= pSessionId;
end;
end;
end;

end.


{********************************************************************
Example:
********************************************************************}

unit Unit1;

interface

uses
Windows, Messages, {...}, Wtsapi;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FRegisteredSessionNotification : Boolean;
procedure AppMessage(var Msg: TMSG; var HAndled: Boolean);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.AppMessage(var Msg: TMSG; var Handled: Boolean);
var
strReason: string;
begin
Handled := False;
// Check for WM_WTSSESSION_CHANGE message
if Msg.Message = WM_WTSSESSION_CHANGE then
begin
case Msg.wParam of
WTS_CONSOLE_CONNECT:
strReason := 'WTS_CONSOLE_CONNECT';
WTS_CONSOLE_DISCONNECT:
strReason := 'WTS_CONSOLE_DISCONNECT';
WTS_REMOTE_CONNECT:
strReason := 'WTS_REMOTE_CONNECT';
WTS_REMOTE_DISCONNECT:
strReason := 'WTS_REMOTE_DISCONNECT';
WTS_SESSION_LOGON:
strReason := 'WTS_SESSION_LOGON';
WTS_SESSION_LOGOFF:
strReason := 'WTS_SESSION_LOGOFF';
WTS_SESSION_LOCK:
strReason := 'WTS_SESSION_LOCK';
WTS_SESSION_UNLOCK:
strReason := 'WTS_SESSION_UNLOCK';
WTS_SESSION_REMOTE_CONTROL:
begin
strReason := 'WTS_SESSION_REMOTE_CONTROL';
// GetSystemMetrics(SM_REMOTECONTROL);
end;
else
strReason := 'WTS_Unknown';
end;
// Write strReason to a Memo
Memo1.Lines.Add(strReason ' ' IntToStr(msg.Lparam));
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// register the window to receive session change notifications.
FRegisteredSessionNotification := RegisterSessionNotification(Handle, NOTIFY_FOR_THIS_SESSION);
Application.OnMessage := AppMessage;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// unregister session change notifications.
if FRegisteredSessionNotification then
UnRegisterSessionNotification(Handle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
// retrieve current session ID
ShowMessage(Inttostr(GetCurrentSessionID));
end;
系統時間:2024-04-26 19:04:45
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!