PrintDialog 过滤打印机列表

发布于 2024-07-06 08:11:37 字数 292 浏览 10 评论 0原文

我需要,最好是用 C# - 但 C++ 也可以,找到一种方法来过滤 Windows 打印对话框中的打印机列表以进行任何 Windows 打印。

我遇到了 WinAPIOverride,并认为我必须编写自己的 dll,它重写获取打印机列表的方法,然后过滤它并返回它。 然后我必须将 dll 注入到所有正在运行的进程中。

任何人都可以帮助我完成已经开发的东西或者可能是完成此任务的更简单的方法吗? 输出打印机列表的唯一方法是通过 API 方法调用,我什至考虑过修改注册表,但这会减慢打印对话框的响应速度,以至于让用户感到厌烦。

I need to, preferably in C# - but c++ will do, find a way to filter the list of printers in the windows print dialog for any windows printing.

I have come across WinAPIOverride and have figured I am going to have to write my own dll which overrides the method to get the printers list, then filter it and return it. I would then have to inject the dll into all running processes.

Can anybody assist me with something that is already developed or perhaps an easier way of accomplishing this? The only way the list of printers comes out is from the API method call and I have even considered modifying the registry, but this will slow down the response of the print dialog box to the point that it would be annoying to the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-07-13 08:11:37

我不认为(重新)编写 DLL 是最简单的方法。 为什么不使用 WMI 来检索想要的 < a href="http://msdn.microsoft.com/en-us/library/aa394363.aspx" rel="nofollow noreferrer">信息(本例中为打印机)?

以下代码用于检索所有本地安装的打印机:
(代码示例借自 此处

    ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
    objScope.Connect();

    SelectQuery selectQuery = new SelectQuery();
    selectQuery.QueryString = "Select * from win32_Printer";
    ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
    ManagementObjectCollection MOC = MOS.Get();
    foreach (ManagementObject mo in MOC) {
      listBox1.Items.Add(mo["Name"].ToString().ToUpper());
    }

要获得跨域已知的打印机,请使用以下命令:

ConnectionOptions objConnection = new ConnectionOptions();
objConnection.Username = "USERNAME";
objConnection.Password = "PASSWORD";
objConnection.Authority = "ntlmdomain:DDI"; //Where DDI is the name of my domain
// Make sure the user you specified have enough permission to access the resource. 

ManagementScope objScope = new ManagementScope(@"\\10.0.0.4\root\cimv2",objConnection); //For the local Access
objScope.Connect();

SelectQuery selectQuery = new SelectQuery();
selectQuery.QueryString = "Select * from win32_Printer";
ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
ManagementObjectCollection MOC = MOS.Get();
foreach (ManagementObject mo in MOC) {
  listBox1.Items.Add(mo["Name"].ToString().ToUpper());
}

当然,该列表不会像您希望的那样“过滤”,因为您没有指定任何条件。 但我相信从现在开始你可以自己处理。

I don't think that (re)writing a DLL is the easiest method. Why not use WMI to retrieve the wanted information (printers in this case)?

The following code is for retrieving all the locally installed printers:
(code samples borrowed from here)

    ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
    objScope.Connect();

    SelectQuery selectQuery = new SelectQuery();
    selectQuery.QueryString = "Select * from win32_Printer";
    ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
    ManagementObjectCollection MOC = MOS.Get();
    foreach (ManagementObject mo in MOC) {
      listBox1.Items.Add(mo["Name"].ToString().ToUpper());
    }

To get the printers known accross a domain use this:

ConnectionOptions objConnection = new ConnectionOptions();
objConnection.Username = "USERNAME";
objConnection.Password = "PASSWORD";
objConnection.Authority = "ntlmdomain:DDI"; //Where DDI is the name of my domain
// Make sure the user you specified have enough permission to access the resource. 

ManagementScope objScope = new ManagementScope(@"\\10.0.0.4\root\cimv2",objConnection); //For the local Access
objScope.Connect();

SelectQuery selectQuery = new SelectQuery();
selectQuery.QueryString = "Select * from win32_Printer";
ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
ManagementObjectCollection MOC = MOS.Get();
foreach (ManagementObject mo in MOC) {
  listBox1.Items.Add(mo["Name"].ToString().ToUpper());
}

Of course, the list is not "filtered" as you would like as you didn't specify any criteria. But I'm sure you can manage from here on by yourself.

野却迷人 2024-07-13 08:11:37

感谢您提供有趣的代码。

这个想法是在不干扰用户的情况下尽可能全局地将过滤后的打印机列表应用到系统。 不幸的是,这意味着过滤列表必须应用于标准 Windows 打印对话框...

因此您的 WMI 代码虽然很酷,但并不合适。 如果我正在构建自己的打印对话框,它可能会非常方便;)

Thanks for the interesting code.

The idea is to apply a filtered printer list to the system as globally as possible without interfering with the user. This means the filtered list has to apply to the standard windows print dialogs unfortunately...

So your WMI code, albeit kind of cool, would not be appropriate. If I were building my own print dialogs, it could come in pretty handy ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文