GetCursorPos not working if desktop is locked

D

Damjan Malis

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top