private static string GetPrinterFriendlyNameIfPrinter(IShellItem2 shellItem) { // Devices_PrimaryCategory returns "Printers" for printers and faxes on Windows 10 but "Printers and faxes" on Windows 7. using (var categoryIds = new PropVariantSafeHandle()) { shellItem.GetProperty(PKEY.Devices_CategoryIds, categoryIds).ThrowIfError(); if (!categoryIds.ToStringVector().Any(id => id.StartsWith("PrintFax", StringComparison.OrdinalIgnoreCase))) { return(null); } } // The canonical or "friendly name" needed to match the devmode // https://blogs.msdn.microsoft.com/asklar/2009/10/21/getting-the-printer-friendly-name-from-the-device-center-shell-folder/ // PKEY_Devices_InterfacePaths doesn't seem to ever be found, but PKEY_Devices_FriendlyName works so... shellItem.GetString(PKEY.Devices_FriendlyName, out var friendlyName).ThrowIfError(); return(friendlyName.ReadAndFree()); }
private static extern HResult PropVariantToStringVectorAlloc(PropVariantSafeHandle propvar, out CoTaskMemSafeHandle pprgsz, out uint pcElem);