By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

GetCursorPos not working if desktop is locked

P: n/a
Hi guys...

could anyone please help me with my problem getting mouse cursor position while desktop being locked (WIN + L).

MSDN says:
"The input desktop must be the current desktop when you call GetCursorPos. Call OpenInputDesktop to determine whether the current desktop is the input desktop. If it is not, call SetThreadDesktop with the HDESK returned by OpenInputDesktop to switch to that desktop."

src: http://msdn2.microsoft.com/en-us/library/ms648390.aspx

So I tried the following:

Imports System.Runtime.InteropServices

Public Class Form1

Private Const DESKTOP_SWITCHDESKTOP As Int32 = &H100&
Private Const DESKTOP_READOBJECTS = &H1&
Private Const DESKTOP_WRITEOBJECTS = &H80&

Declare Function GetCursorPos Lib "User32" (ByRef lpPoint As Point) As Long

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function OpenInputDesktop(ByVal dwFlags As Integer, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Integer) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function SetThreadDesktop(ByVal hDesktop As Int32) As Int32
End Function

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function OpenDesktop(ByVal lpszDesktop As String, ByVal dwFlags As Integer, ByVal fInderit As Boolean, ByVal dwDesiredAccess As Integer) As IntPtr
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim xx As IntPtr = OpenInputDesktop(0, True, DESKTOP_SWITCHDESKTOP)
SetThreadDesktop(xx)

Dim pt As Point
GetCursorPos(pt)
ListBox1.Items.Add(pt.X.ToString + " x " + pt.Y.ToString)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

But sadly, it doesn't work. After desktop gets locked all I get is 0 x 0 for x and y coordinates.
Maybe you can recommend me some different approach to observe mouse movement activity from windows service. After all, I don't need exact coordinates, all I need is to determine if someone is using the mouse.

Thank you in advance.

Cheers,
Damjan

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Dec 27 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Damjan,

You can't get this information while the desktop is locked. While the
desktop is locked, the security subsystem takes over, and your desktop is
off limits.

At that point, you would have to hook into the O/S (people have done it
by replacing GINA.dll, but you can't do this in managed code, and on top of
that, you can't do it in Vista, as it has a different model).

While the desktop is locked, you are going to have to do without cursor
information if you are using managed code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Damjan Maliswrote in message news:20***********************@gmail.com...
Hi guys...

could anyone please help me with my problem getting mouse cursor position
while desktop being locked (WIN + L).

MSDN says:
"The input desktop must be the current desktop when you call GetCursorPos.
Call OpenInputDesktop to determine whether the current desktop is the
input desktop. If it is not, call SetThreadDesktop with the HDESK returned
by OpenInputDesktop to switch to that desktop."

src: http://msdn2.microsoft.com/en-us/library/ms648390.aspx

So I tried the following:

Imports System.Runtime.InteropServices

Public Class Form1

Private Const DESKTOP_SWITCHDESKTOP As Int32 = &H100&
Private Const DESKTOP_READOBJECTS = &H1&
Private Const DESKTOP_WRITEOBJECTS = &H80&

Declare Function GetCursorPos Lib "User32" (ByRef lpPoint As Point) As
Long

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function OpenInputDesktop(ByVal dwFlags As Integer,
ByVal fInherit As Boolean, ByVal dwDesiredAccess As Integer) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function SetThreadDesktop(ByVal hDesktop As Int32) As
Int32
End Function

<DllImport("user32.dll", SetLastError:=True)_
Private Shared Function OpenDesktop(ByVal lpszDesktop As String, ByVal
dwFlags As Integer, ByVal fInderit As Boolean, ByVal dwDesiredAccess As
Integer) As IntPtr
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim xx As IntPtr = OpenInputDesktop(0, True, DESKTOP_SWITCHDESKTOP)
SetThreadDesktop(xx)

Dim pt As Point
GetCursorPos(pt)
ListBox1.Items.Add(pt.X.ToString + " x " + pt.Y.ToString)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub
End Class

But sadly, it doesn't work. After desktop gets locked all I get is 0 x 0
for x and y coordinates.
Maybe you can recommend me some different approach to observe mouse
movement activity from windows service. After all, I don't need exact
coordinates, all I need is to determine if someone is using the mouse.

Thank you in advance.

Cheers,
Damjan

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com

Dec 27 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.