如何从设备和打印机IShellFolder获取打印机名称?

时间:2015-09-29 10:01:00

标签: c# .net printing printers

我正在使用代码获取系统打印机图标(只有我找到的方式 - 使用IShellFolder),现在我想将它们与InstalledPrinters连接,但问题是 - 我找不到找到真实打印机名称的方法(例如作为“\ ServerName \ PrinterName”),与显示“设备和打印机”shell文件夹的内容名称不同,只能与PrinterSettings一起使用。

我用于在“设备和打印机”shell文件夹中检索打印机图标和打印机标题的代码:

    Shell32.IShellFolder iDesktopFolder = Shell32.GetDesktopFolder();
    try
    {
        IntPtr pidlPrintersFolder;
        if (Shell32.SHGetFolderLocation(_hwndOwner, (int)Shell32.CSIDL.CSIDL_PRINTERS, IntPtr.Zero, 0, out pidlPrintersFolder) == 0)
            try
            {
                StringBuilder strDisplay = new StringBuilder(260);
                Guid guidIShellFolder = Shell32.IID_IShellFolder;
                IntPtr ptrPrintersShellFolder;
                iDesktopFolder.BindToObject(pidlPrintersFolder, IntPtr.Zero, ref guidIShellFolder, out ptrPrintersShellFolder);
                Object objPrintersShellFolder = Marshal.GetTypedObjectForIUnknown(ptrPrintersShellFolder, Shell32.ShellFolderType);
                try
                {
                    Shell32.IShellFolder printersShellFolder = (Shell32.IShellFolder)objPrintersShellFolder;

                    IntPtr ptrObjectsList;

                    printersShellFolder.EnumObjects(_hwndOwner, Shell32.ESHCONTF.SHCONTF_NONFOLDERS, out ptrObjectsList);
                    Object objEnumIDList = Marshal.GetTypedObjectForIUnknown(ptrObjectsList, Shell32.EnumIDListType);
                    try
                    {
                        Shell32.IEnumIDList iEnumIDList = (Shell32.IEnumIDList)objEnumIDList;
                        IntPtr[] rgelt = new IntPtr[1];
                        IntPtr pidlPrinter;
                        int pceltFetched;
                        Shell32.STRRET ptrString;
                        while (iEnumIDList.Next(1, rgelt, out pceltFetched) == 0 && pceltFetched == 1)
                        {
                            printersShellFolder.GetDisplayNameOf(rgelt[0],                                        
                                Shell32.ESHGDN.SHGDN_NORMAL, out ptrString);                                        
                            if (Shell32.StrRetToBuf(ref ptrString, rgelt[0], strDisplay,
                                (uint)strDisplay.Capacity) == 0)
                            {
                                pidlPrinter = Shell32.ILCombine(pidlPrintersFolder, rgelt[0]);
                                string printerDisplayName = strDisplay.ToString();

                                Shell32.SHFILEINFO shinfo = new Shell32.SHFILEINFO();
                                Shell32.SHGetFileInfo(this._pidl, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), Shell32.SHGFI.PIDL | Shell32.SHGFI.AddOverlays | Shell32.SHGFI.Icon);
                                Icon printerIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
                                Shell32.DestroyIcon(shinfo.hIcon);

                                // HOW TO GET PRINTER NAME (\\ServerName\printername) WITH IS DIFFERENT FROM A PRINTER NAME IN "DEVICES AND PRINTERS" SYSTEM FOLDER
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(objEnumIDList);
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(objPrintersShellFolder);
                }
            }
            finally
            {
                Shell32.ILFree(pidlPrintersFolder);
            }
    }
    finally
    {
        Marshal.ReleaseComObject(iDesktopFolder);
    }

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

自己找答案。

要枚举的IShellFolder打印机就必须用IShellFolder.ParseDisplayName方法,不EnumObjects,和从PrinterSettings.IntalledPrinters(诸如“\服务器名\ PrinterName的”网络打印机)发送打印机名称以IShellFolder.ParseDisplayName PARAM pszDisplayName,的IShellFolder“设备和打印机“很好地合作。

因此,在此之后我们可以枚举真实的打印机名称(“\ ServerName \ PrinterName”),打印机显示名称(“ServerName上的PrinterName”作为“设备和打印机”中列出的打印机)和打印机图标。

我会在一些编辑后在这里发布完整代码供父亲搜索。

答案 1 :(得分:1)

在循环中,您可以在当前printersShellFolder.GetUIObjectOf上使用IID_IDataObject rgelt[0],并使用IDataObject::GetData作为剪贴板格式调用"PrinterFriendlyName"以获取真实的打印机名称(以STGMEDIUM)编码。

您可以在当前pidl上使用printersShellFolder.GetUIObjectOf IID_IQueryInfo来获取打印机状态。