<AWT Dev> RFR: 8263311: Watch registry changes for remote printers update instead of polling

Prasanta Sadhukhan psadhukhan at openjdk.java.net
Fri Mar 12 04:58:12 UTC 2021


On Thu, 11 Mar 2021 14:59:55 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:

>> [JDK-8153732](https://bugs.openjdk.java.net/browse/JDK-8153732) implemented polling for remote printers.
>> That bug description also mentions watching the registry for changes and links to the article which describes the method yet it does so in terms of WMI. Using WMI is not necessary to watch for the registry updates.
>> 
>> It is possible to replace polling mechanism with registry change notifications. If the registry at `HKCU\Printers\Connections` is updated, refresh the list of print services.
>> 
>> It works perfectly well in my own testing with sharing a Generic / Text Only printer from another laptop. The notification comes as soon as the printer is installed, it results in a new key created under `Connections`. If a remote printer is removed, the notification is also triggered as the key corresponding to that printer is removed from the registry.
>> 
>> I updated the steps in the manual test: `RemotePrinterStatusRefresh.java`.
>
> src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp line 258:
> 
>> 256:                                                          REG_NOTIFY_CHANGE_NAME,
>> 257:                                                          NULL,
>> 258:                                                          FALSE);
> 
> [`RegNotifyChangeKeyValue`](https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regnotifychangekeyvalue) notifies the caller about changes to the attributes or contents of a specified registry key.
> 
> • `hKey`: A handle to `HKEY_CURRENT_USER\Printers\Connections` key which is opened above.
> • `bWatchSubtree = TRUE`: The function reports changes in the specified key and its subkeys.
> • `dwNotifyFilter = REG_NOTIFY_CHANGE_NAME`: Notify the caller if a subkey is added or deleted.
> • `hEvent = NULL`: If `fAsynchronous` is FALSE, `hEvent` is ignored.
> • `fAsynchronous = FALSE`: The function does not return until a change has occurred.
> 
> When a new remote printer is added, a new key is created under `HKCU\Printers\Connections`; when an existing remote printer is removed, the key below `Connections` is removed; no values are added or removed in `Connections` key, thus `REG_NOTIFY_CHANGE_LAST_SET` filter is not needed.

Is this only about addition/removal? What about printer name change? Shouldn't we get notified in that case as trying to print on printer with old name will not find the printer!!
If yes, in that regard I guess REG_NOTIFY_CHANGE_LAST_SET is the one to go for as it states
`"Notify the caller of changes to a value of the key. This can include adding or deleting a value, or changing an existing value. "`

-------------

PR: https://git.openjdk.java.net/jdk/pull/2915


More information about the awt-dev mailing list