编写 Windows 端口监视器基础知识

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/901766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:32:13  来源:igfitidea点击:

Writing Windows Port Monitor Basics

cwindowswinapiprinting

提问by ctrlalt313373

I'm trying to find a basic example, tutorial, or blog post on how to write a printer port monitor. I downloaded the Windows DDK and dug through localmon, but it appears that this sample is much more complex than just the nuts and bolts basics and from my understanding it is a bit different than an OEM port monitor because of how it handles the registry key and port enumeration. Does anyone know of a blog post, tutorial, or even book that walks the reader through the basic code to get one up and going? I've found a few links talking about the conceptual stuff, but nothing that is hands on code.

我正在尝试查找有关如何编写打印机端口监视器的基本示例、教程或博客文章。我下载了 Windows DDK 并通过 localmon 进行了挖掘,但是这个示例似乎比基本的基本知识复杂得多,据我了解,它与 OEM 端口监视器有点不同,因为它处理注册表项的方式和端口枚举。有没有人知道一篇博文、教程,甚至是一本书,可以引导读者完成基本代码以开始学习?我发现了一些讨论概念性内容的链接,但没有什么是动手代码的。

回答by Tony Edgecombe

I wrote mine from the specs, there aren't really that many API's to implement.

我根据规范编写了我的规范,实际上并没有那么多要实现的 API。

The one thing that regularaly trips people up is EnumPorts, the spooler allocates enough memory for ALL the ports, not just yours. So you need to make sure you fill any strings from the end of the spoolers buffer, don't put them straight after your structures.

经常让人们绊倒的一件事是 EnumPorts,假脱机程序为所有端口分配足够的内存,而不仅仅是你的。因此,您需要确保从假脱机缓冲区的末尾填充任何字符串,不要将它们直接放在您的结构之后。

It doesn't say so in the specs but you can safely put the UI and Server functions in the same DLL.

规范中没有这么说,但您可以安全地将 UI 和服务器函数放在同一个 DLL 中。

It's also possible to create a single port monitor that supports NT and the later Windows 2000 type port monitors.

还可以创建支持 NT 和更高版本的 Windows 2000 类型端口监视器的单端口监视器。

The code in RedMon is much easier to read than the localmon example, it's worth looking at before you start. It's quite nice because you can compile it in VS, you don't need to use the DDK to build it.

RedMon 中的代码比 localmon 示例更容易阅读,在开始之前值得一看。非常好,因为你可以在VS中编译它,你不需要使用DDK来构建它。

回答by Citrone

I can recomend http://www.codeproject.com/KB/printing/wpa.aspx, which describes how to write a printer driver and also has good hints about what's necessary to build a port monitor.

我可以推荐http://www.codeproject.com/KB/printing/wpa.aspx,它描述了如何编写打印机驱动程序,并且对构建端口监视器的必要性也有很好的提示。

But my opinion is that a good tutorial in this area is not available on the Internet (I would be glad to find somebody who can show me that I'm wrong). So, when I had to deal with this task I was forced to do it the hard way: I've read carefully the MSDN explanations starting from this point: http://msdn.microsoft.com/en-us/library/ff561109.aspx. In parallel with reading MSDN I also checked the code in DDK you mentioned and try to understand it. I'm sure this solution could also work for you.

但我的观点是 Internet 上没有这方面的好的教程(我很高兴找到可以告诉我我错了的人)。所以,当我不得不处理这个任务时,我被迫以艰难的方式去做:从这一点开始,我仔细阅读了 MSDN 的解释:http: //msdn.microsoft.com/en-us/library/ff561109 .aspx。在阅读 MSDN 的同时,我还检查了您提到的 DDK 中的代码并尝试理解它。我相信这个解决方案也适合你。

回答by R Ubben

I have been over that exact same territory for a serial printer. About the best example I found was this article in Dr Dobbs Journal. The good part is that both a serial port driver and the user-space control program are covered and the project can also be used as an example of how to set up Visual Studio to compile a driver. This is also something a little difficult to find information about. The article discusses an old NT style driver, which worked well for me on XP.

对于串行打印机,我已经超过了完全相同的领域。我发现的最好的例子是Dobbs 博士杂志上的这篇文章。好的部分是涵盖了串行端口驱动程序和用户空间控制程序,并且该项目还可以用作如何设置 Visual Studio 来编译驱动程序的示例。这也是有点难以找到相关信息的事情。这篇文章讨论了一个旧的 NT 风格的驱动程序,它在 XP 上对我很有效。

There are quite a few good articles on CodeProjectabout writing drivers and programs to interact with them. They include source code and most deal with the newer WDM and WDF style drivers.

CodeProject上有很多关于编写驱动程序和程序以与它们交互的好文章。它们包括源代码并且大多数处理较新的 WDM 和 WDF 风格的驱动程序。

OSROnlineis another good source, especially for discussion of specific issues and common mistakes. They also have some great utilities you will need.

OSROnline是另一个很好的来源,特别是对于特定问题和常见错误的讨论。他们还有一些您需要的很棒的实用程序。

Some of the most clearly written and understandable driver code I came across was Mark Russinovich's sample code. Although Microsoft withdrew all of the source when they purchased Sysinternals, some of the best examples can still be found cached here and there.

我遇到的一些编写得最清楚和易懂的驱动程序代码是 Mark Russinovich 的示例代码。尽管微软在购买 Sysinternals 时撤回了所有源代码,但仍然可以在这里和那里找到一些最好的例子。

Drivers are pretty interesting. Whatever else you do though, do it in a virtual machine. Really.

司机很有趣。不管你做什么,在虚拟机中做。真的。