Call to TMouse.GetCursorPos sometimes fails with “A call to an OS function failed”

谁说胖子不能爱 提交于 2019-12-05 11:01:12

CursorPos uses the Windows GetCursorPos method. The remarks on MSDN says it has two requirements:

  • "The calling process must have WINSTA_READATTRIBUTES access to the window station."
  • "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."

So, chances are that the screensaver is running on another desktop. Alternately, if you're using Vista I'm pretty sure the password dialog (for unlocking a computer) runs on another desktop as well.

Since you have the source for this component, you may want to write your own wrapper for CursorPos that returns a dummy value when there's a problem. (Edit: or a commenter suggested handling the failure to get a position inline instead of writing a function to return a dummy value.)

Finally, you can call GetLastError to see what the last Windows error was, after it's thrown the exception. That should tell you for sure what the actual problem it's encountering is. As in a comment (thanks!) you have already encountered the error message in the exception message.

Without seeing the code and which version of Windows, one can only guess. I would look into the code of the TtaoHoverTimer.Timer procedure in unit taoCntrr.

Try calling the GetCursorPos(cursorPos); method in the Windows unit.

Something like this:

var
   cursorPos       : TPoint;

begin
     GetCursorPos(cursorPos);
     cursorPos := ScreenToClient(cursorPos);

It works with no problem on all my applications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!