Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printer tray changes are not detected by papersources #9034

Closed
N3IS opened this issue Apr 25, 2023 · 14 comments
Closed

Printer tray changes are not detected by papersources #9034

N3IS opened this issue Apr 25, 2023 · 14 comments
Assignees
Labels
area-System.Drawing System.Drawing issues 📭 waiting-author-feedback The team requires more information from the author Priority:2 Work that is important, but not critical for the release

Comments

@N3IS
Copy link

N3IS commented Apr 25, 2023

.NET version

.NET6

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

The papersources of printersettings do not detect tray changes made with printer driver while the application is running. To detect these changes I need to restart the application. Added and removed printers are detected live but trays (papersources) not.

Steps to reproduce

while (true) 
{
    var printerNames = PrinterSettings.InstalledPrinters.Cast<string>().ToList();

    foreach (var printerName in printerNames)
    {
        var printerSettings = new PrinterSettings { PrinterName = printerName };
        var trays = printerSettings.PaperSources.Cast<PaperSource>().Select(ps => ps.SourceName);

       Console.WriteLine($"{printerName} - {string.Join(", ", trays)}");
    }

    Console.WriteLine("----------------------------------------------------------------------------------------");
    Console.ReadLine();
}
@N3IS N3IS added the untriaged The team needs to look at this issue in the next triage label Apr 25, 2023
@merriemcgaw merriemcgaw added the area-System.Drawing System.Drawing issues label Apr 25, 2023
@merriemcgaw merriemcgaw removed the untriaged The team needs to look at this issue in the next triage label Apr 25, 2023
@JeremyKuhne JeremyKuhne added this to the Future milestone Apr 25, 2023
@JeremyKuhne
Copy link
Member

I don't know that we can do much here. We just ask Windows for the data and I don't see any information about how it caches or might not be cached.

@N3IS can you give more explicit details about your scenario? If I can reproduce the caching behavior, I can try to reach out to the Windows team to see if there is anything we can do.

@JeremyKuhne JeremyKuhne added the 📭 waiting-author-feedback The team requires more information from the author label Apr 25, 2023
@N3IS
Copy link
Author

N3IS commented Apr 26, 2023

My scenario is that we provide a printing service. The user can define to which printer and to which tray (papersource) he wants to print at. If now e.g. a new tray of a printer gets added via windows printer driver because it should be used from now on we need to restart our application on production to detect these change and make it usable.

To reproduce this locally I installed some printer driver from our printserver and added or removed some trays inside the driver configuration while my example code was running. The changes are only displayed in the console if I restart the application.

@ghost ghost removed the 📭 waiting-author-feedback The team requires more information from the author label Apr 26, 2023
@JeremyKuhne JeremyKuhne added the Priority:2 Work that is important, but not critical for the release label Apr 26, 2023
@elachlan
Copy link
Contributor

@kennykerr does rust have a similar issue with DeviceCapabilitiesW?

@kennykerr
Copy link

@jonwis may know who can help with this API. Otherwise, you can try:

https://docs.microsoft.com/en-us/answers/topics/windows-api.html

@jonwis
Copy link

jonwis commented Jul 17, 2023

Hey @mziller - can you help out here? Is there an event that the Win32 https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-devicecapabilitiesw API offers to determine changes in the printer? Maybe https://learn.microsoft.com/en-us/windows/win32/printdocs/findfirstprinterchangenotification needs to track changes in the printer?

@mziller
Copy link

mziller commented Jul 17, 2023

Right I'd start with DeviceCapabilities passing DC_BINNAMES (you should be able to pass nullptr for the port and DevMode, giving just the printer name) and see if that also has the same issue. If so I suspect the config DLL for the driver may be caching the information (the driver DLL is loaded in the printing application's address space). You can see the config file used by the driver in printmanagement.msc in the driver properties dialog.

@elachlan
Copy link
Contributor

@N3IS can you please check the driver config?

@N3IS
Copy link
Author

N3IS commented Jul 18, 2023

This is happening with all printers and drivers. I guess it is no driver dependent issue. Also I am not the owner of the driver it is a third party printer driver.

@elachlan
Copy link
Contributor

elachlan commented Dec 8, 2023

@N3IS on the machine with the issue, can you run printmanagement.msc and then go to "All Drivers" then right click the driver for the printer with the issue and select "Properties". Then check the "File" of the "Config File".

It should look something like this:
image

@N3IS
Copy link
Author

N3IS commented Dec 8, 2023

I can confirm by using DeviceCapabilitiesW function that we have the same result. So tray changes aren't recognized.
We used this piece of code to validate it. We also updated the application to .NET8 with updated libraries.

internal static class Program
{
    private const int DC_BINNAMES = 12;

    [DllImport("winspool.drv", EntryPoint = "DeviceCapabilitiesW", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern int DeviceCapabilities(string pDevice, string pPort, int fwCapability, IntPtr pOutput, IntPtr pDevMode);

    private static void Main(string[] _)
    {
        while (true)
        {
            const string printerName = "SHARP MX-3060N PS";

            var pOutput = Marshal.AllocHGlobal(1000);
            var result = DeviceCapabilities(printerName, null, DC_BINNAMES, pOutput, IntPtr.Zero);

            Console.WriteLine(printerName);

            for (var i = 0; i < result; i++)
            {
                var trayName = Marshal.PtrToStringAuto(pOutput + 48 * i);
                Console.WriteLine("\t" + trayName);
            }

            Marshal.FreeHGlobal(pOutput);

            Console.WriteLine("----------------------------------------------------------------------------------------");
            Console.ReadLine();
        }
    }
}

Here are screenshots of the used printer and it's config how we change the amount of trays. We have the printer driver only available in German.
image
image

@elachlan
Copy link
Contributor

@mziller Please see response above. Does that mean the driver is probably caching the results?

@mziller
Copy link

mziller commented Dec 11, 2023

Right the driver would have had to specify the optional trays that can be added as installable options in the driver config file (GPD or PPD) for this to work dynamically:

https://learn.microsoft.com/en-us/windows-hardware/drivers/print/handling-installable-features-and-options
https://learn.microsoft.com/en-us/windows-hardware/drivers/print/v4-driver-property-bags#queue-property-bag

@elachlan
Copy link
Contributor

@N3IS This is something you have to raise with the Printer manufacturer, so they can support it via their driver.

@elachlan elachlan added the 📭 waiting-author-feedback The team requires more information from the author label Dec 14, 2023
@ghost
Copy link

ghost commented Dec 28, 2023

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

It will be closed if no further activity occurs within 7 days of this comment.

@ghost ghost closed this as completed Jan 4, 2024
@ghost ghost removed this from the Future milestone Jan 4, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 4, 2024
@ghost ghost removed the 💤 no-recent-activity label Feb 4, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Drawing System.Drawing issues 📭 waiting-author-feedback The team requires more information from the author Priority:2 Work that is important, but not critical for the release
Projects
None yet
Development

No branches or pull requests

7 participants