为什么 WTSRegisterSessionNotification 可能因拒绝访问错误而失败?

问题描述 投票:0回答:0

我收到了一些来自用户的错误报告,其中包含一个奇怪的错误。在某些机器(Windows 10 和 8.1)上调用 WTSRegisterSessionNotification 返回错误代码 5(拒绝访问):

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    FWTSNotification: Boolean;
  protected
    procedure WndProc(var Msg: TMessage); override;
    procedure CreateWnd; override;
    procedure DestroyWindowHandle; override;
  public
    { Public declarations }
  end;

...

procedure TForm1.CreateWnd; 
begin
  inherited;
  FWTSNotification:= WTSRegisterSessionNotification(Handle, NOTIFY_FOR_THIS_SESSION);
  if not FWTSNotification
  then
  begin
   var FLastError:= GetLastError;
   Raise Exception.Create(Format('WTSRegisterSessionNotification failed: [ErrorCode: #%d] - %s', [FLastError, SysErrorMessage(FLastError)]));
  end;
end;

...

procedure TForm1.WndProc(var Msg: TMessage);
begin

 case Msg.Msg of

  WM_WTSSESSION_CHANGE:
  begin
   case Msg.wParam of
    WTS_SESSION_LOCK: ...
    WTS_SESSION_UNLOCK: ...
    WTS_CONSOLE_CONNECT: ...
    WTS_CONSOLE_DISCONNECT: ...
   end;
  end;

 else
  inherited;
 end;
end;

...

procedure TForm1.DestroyWindowHandle;
begin
 if FWTSNotification
 then WTSUnRegisterSessionNotification(Handle);
 inherited;
end;

我试图在我的电脑上重现这个错误,使用 Windows 10、8、7,帐户具有管理员\用户权限,但从未出现任何错误。这个错误的原因可能是什么以及如何重现它?

windows delphi winapi pascal
© www.soinside.com 2019 - 2024. All rights reserved.