473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GetCursorPos not working if desktop is locked

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
1 6106
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Numbers | last post by:
Fellow C-Sharp developers, I have an application that must be able to display a message on a desktop even if the screen saver has locked the computer. If you pass "ServiceNotification" as a...
2
by: PeteCresswell | last post by:
I've been changing my apps to be compliant with MS's standard of writing temp data only to C:\Documents And Settings\.... But now I'm converting an app that creates .TXT files and spreadsheets...
0
by: ntm | last post by:
Hi all you good m8 I try to make my first small program in vb, but im not very succesfull :D (Im using Visual Studio 2005) Form my own program i will like to set focus on an other running...
3
by: Jay | last post by:
Hey There, Is it possible to get an unique identifier for a desktop that a particular app is running from, from a service? If there are two instances of the app running on different desktops (one...
0
by: lucretia | last post by:
Hi, I've created a layout for an Ajax side that is "single paged" in that when the browser is resized, so is the page. The header is locked to the top, the right panel is locked to the right...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
4
jinalpatel
by: jinalpatel | last post by:
I am totally new to the VBA and I have following problem. I have two tables Holderdata : Parent ; Purchasedata : child I created a search form based on holder data. the search criteria (User...
0
by: sillz | last post by:
On Apr 8, 2:00 pm, sillz <beth.sto...@gmail.comwrote: I never could get this to work. The oracle account tested fine. I ended up creating a new account in Windows with the right permissions...
6
by: Jajjo | last post by:
Hey there, I'm experimenting with GetCursorPos instead of "static_cast"ing the LOWORD and HIWORD params. This is because I want to work with positions relative to teh screen. Myt question is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.