DoumanAsh / clipboard-win

Rust win clipboard utilities
https://crates.io/crates/clipboard-win
Boost Software License 1.0
53 stars 17 forks source link

Win, `is_format_avail(13)` fails after Ctrl + C #33

Open fufesou opened 8 months ago

fufesou commented 8 months ago

Hi, thanks for this great crate.

It's not a bug of this crate, but it's a related strange issue.

issue

Have you ever meet the issue that:

  1. OS, windows
  2. Run an application as a system process
  3. Ctrl + C
  4. is_format_avail(13)

is_format_avail(13) returns 0, but GetLastError() returns 0.

related

Hi, I've used this crate through arboard .

Someone is facing this problem. But I cannot find out why it happens.

https://github.com/rustdesk/rustdesk/issues/6859#issuecomment-1969497478

https://github.com/rustdesk/rustdesk/issues/6859#issuecomment-1969503837

The code in arboard is

https://github.com/fufesou/arboard/blob/aa0de068a9b1bccad764a67276007768111ec3aa/src/platform/windows.rs#L489

thanks

It seems this issue happens only on his one machine.

It may be related to his machine settings.

It would be great if anyone knows about this issue or can give a little hint.

DoumanAsh commented 8 months ago

abroad code seems to be correct as it acquire clipboard before checking format So the only reason I can think of is that there is bug in MS API

Note that IsClipboardFormatAvailable returning false is actually no error, it is just lack of content on clipboard

I provide wrapper over enumeration API https://docs.rs/clipboard-win/latest/clipboard_win/raw/struct.EnumFormats.html

But it seems you already tried and see clipboard format is available

So I would suspect windows bug first

Another thing to consider is that I would recommend to check for both CF_TEXT and CF_UNICODETEXT The reason being is that I'm not sure which format was originally pasted, but if it is CF_TEXT Unicode shall be available, but only as synthesized format (hence it could be reason of bug in MS API)

Note that all clipboard APIs will have errno set as 0 when format is not available so you could technically go ahead and use size directly skipping format is available

fufesou commented 8 months ago

Note that all clipboard APIs will have errno set as 0 when format is not available so you could technically go ahead and use size directly skipping format is available

I've tried, but it fails

https://github.com/fufesou/arboard/blob/59f66d91b30a268bb52329e5dc53a46c637d7c7f/src/platform/windows.rs#L489

1709515120814

https://github.com/rustdesk/rustdesk/issues/6859#issuecomment-1971296975

I'll try to find out what error can be returned by GetLastError().

DoumanAsh commented 8 months ago

GetLastError will return 0 when there is no data available on clipboard

Note that while clipboard is global, it is tied to user session (but this should not be a problem if service is part of user session)

DoumanAsh commented 8 months ago

There is important note about service on MS docs: https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications

Windows Service applications run in a different window station than the interactive station of the logged-on user. A window station is a secure object that contains a Clipboard, a set of global atoms, and a group of desktop objects. Because the station of the Windows service is not an interactive station, dialog boxes raised from within a Windows service application will not be seen and may cause your program to stop responding. Similarly, error messages should be logged in the Windows event log rather than raised in the user interface.

The Windows service classes supported by the .NET Framework do not support interaction with interactive stations, that is, the logged-on user. The .NET Framework also does not include classes that represent stations and desktops. If your Windows service must interact with other stations, you will need to access the unmanaged Windows API. For more information, see the Windows SDK documentation.

The interaction of the Windows service with the user or other stations must be carefully designed to include scenarios such as there being no logged on user, or the user having an unexpected set of desktop objects. In some cases, it may be more appropriate to write a Windows application that runs under the control of the user.

DoumanAsh commented 8 months ago

@fufesou I checked my windows laptop and looking at default clipboard service it is runs (log ons) as my user If you have multiple users of the rustdesk (I would recommend to check how they have service configured in tab Log On of the service) to see if it is different or not

fufesou commented 8 months ago

RustDesk service is log on as "Local System account" by default. It works fine on my machine.

Maybe this option doesn't affect.

DoumanAsh commented 8 months ago

Well it was wild guess, in any case I do not have believe it has anything to do with winapi itself but I'll keep this issue open until you'll find solution