3

我列举了我系统上的桌面(我正在使用 SysInternals Desktops,所以我知道我有额外的)使用:

EnumDesktops(Nothing, AddressOf EnumDesktopsProc, Nothing)

使用这些声明:

Private Declare Auto Function EnumDesktops Lib "User32" (ByVal hWinSta As IntPtr, ByVal edp As EnumDesktopProc, ByVal lParam As IntPtr) As Boolean
Private Declare Auto Function OpenDesktop Lib "User32" (<MarshalAs(UnmanagedType.LPTStr)> ByVal DesktopName As String, ByVal dwFlags As Integer, <MarshalAs(UnmanagedType.Bool)>ByVal fInherit As Boolean, ByVal dwDesiredAccess As AccessMask) As DesktopHandle

Private desktopNames As New List(Of String)

Private Delegate Function EnumDesktopProc(<MarshalAs(UnmanagedType.LPTStr)>ByVal DesktopName As String, ByVal lParam As IntPtr) As Boolean

Private Function EnumDesktopsProc(
          ByVal DesktopName As String,
          ByVal lParam As IntPtr
        ) As Boolean

    desktopNames.Add(DesktopName)
    Return True
End Function

我检索到:

"WinSta0"
"Service-0x0-3e7$"
"Service-0x0-3e4$"
"Service-0x0-3e5$"
"SAWinSta"
"__X78B95_89_IW"
"Service-0x0-119d1e$"
"Service-0x0-4e05f$"

当我试图打开它们时:

OpenDesktop(DesktopName, 0, False, AccessMask.DESKTOP_ENUMERATE)

这里这里的数据在哪里复制AccessMask很大,并且是一个)EnumDesktopHandleSafeHandle

他们都返回NULLNothing)并GetLastWin32Error返回2 "The system cannot find the file specified."。为什么?

我检查了没有效果的替代声明OpenDesktop和不同的AccessMask值(除非声明实际上是错误的)。我注意到在向 中添加“路径”时可能会出现不同的错误DesktopName,特别是161 "The specified path is invalid."

4

1 回答 1

4

的文档EnumDesktops至少是误导性的,如果不是错误的话。传递NULL( Nothing) 作为第一个参数是返回“会话 0”窗口站的桌面。您需要使用GetProcessWindowStation(). 即将上面的第一行更改为:

Private Declare Auto Function GetProcessWindowStation Lib "User32" () As IntPtr

EnumDesktops(GetProcessWindowStation(), AddressOf EnumDesktopsProc, Nothing)

EnumDesktops(文档中添加了一条用户评论,但我错过了,所以我认为其他人也可能。)

请注意,文档GetProcessWindowStation 明确表示它不需要关闭,因此不需要包装在SafeHandle.

于 2012-11-10T15:05:39.533 回答