0

I need the "Printer Friendly Names", and I'm trying what "wqw" suggested in this thread:

How I can get printer name from device and printers IShellFolder?

The (legacy) code we use to get "Device and printers" printer names + icons is not exactly the same as the one posted in the thread, but very similar. When calling the GetTypedObjectForIUnknown as shown below, however, it results in an exception (something along the lines of "could not convert an object of type System.__ComObject to type System.Windows.IDataObject". What am I missing?

printersShellFolder.GetUIObjectOf(IntPtr.Zero, 1, rgelt, ref IID_IDataObject,
                                  0, out var ppv);
var objDataType = Marshal.GetTypedObjectForIUnknown(ppv, DataObjectType);
var dataType = (IDataObject)objDataType;
var printerFriendlyName = dataType.GetData("PrinterFriendlyName");
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • To get the list of printers with their properties (including various names) you can either use the "old" Setup Api (need interop) or the newer WinRT API (C# binding provided). Here is a "Device Manager" example of the former here: https://stackoverflow.com/a/47525848/403671 and the latter here https://github.com/smourier/DeviceExplorer – Simon Mourier Oct 16 '22 at 08:10
  • Thanks! I'm not familiar working with this type of C# (pointers and stuff). Do you know how I should handle the ppv in the code example to get an IDataObject from it? – ProgrammingParadise Oct 17 '22 at 07:45

0 Answers0