delphi - 设置线程桌面 : ERROR_INVALID_HANDLE

标签 delphi delphi-10-seattle

为什么每次执行下面的代码时,SetThreadDesktop()失败并显示以下消息:

ERROR_INVALID_HANDLE

新桌面创建成功,表示句柄打开了。

发生了什么事?

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils;

var
  _hDesk: HDESK;

begin
  try
    _hDesk := OpenDesktop('test', 0, True, GENERIC_ALL);

  if _hDesk = 0 then
    CreateDesktop('test', nil, nil, 0, GENERIC_ALL, nil);

  if not SetThreadDesktop(_hDesk) then
  begin
    Writeln(SysErrorMessage(GetLastError));
  end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

最佳答案

CreateDesktop 返回一个句柄。您需要使用该句柄将线程切换到桌面。

_hDesk := CreateDesktop('test', nil, nil, 0, GENERIC_ALL, nil);

另外,请务必检查该函数是否成功,请参阅文档了解具体方法。

关于delphi - 设置线程桌面 : ERROR_INVALID_HANDLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55820992/

相关文章:

delphi - 如何使我的 32 位 Delphi 应用程序能够在 64 位 Windows 上使用 4GB 内存(通过 Wow64.exe)?

delphi - 如何在Delphi中实现一套标准的超链接检测规则

delphi - 使用 TJSONUnMarshal 自定义注册恢复器意外失败

delphi - Delphi 西雅图的 Windows 10 TNotification 内存泄漏?

德尔福 : How to use Format() with runtime-built array of TVarRec?

delphi - Color Listbox.Item[N],其中 N 由代码生成

delphi - Firebird SQL 脚本命令字符串限制

delphi - 为什么 Delphi 命令行为我提供了与 IDE 不同的输出?

delphi - 将 TestInsight 安装到 Delphi 10 Seattle