简体   繁体   English

什么程序或进程在C#中使用特定的串行端口

[英]What program or process is using a specific serial port in C#

When I'm trying to use a com port in my C# program sometimes the port is open and is used by another program. 当我在我的C#程序中尝试使用com端口时,有时端口是打开的并且被另一个程序使用。 I want to be specific and generate warning message like this : 我希望具体并生成如下警告消息:

" COM1 port is used by other program named SomeApplication . Do you want to close that program (Note that you may lose data or other consequences)?" “其他程序名为SomeApplication使用COM1端口。是否要关闭该程序(请注意,您可能会丢失数据或其他后果)?” -Yes -No -Yes -No

How to implement this scenario in C#? 如何在C#中实现这种情况?

This is not supported on Windows, it requires walking undocumented kernel structures that are only accessible in kernel code. 这在Windows上不受支持,它需要步行未记录的内核结构,这些内核结构只能在内核代码中访问。 Keeping this info hidden is quite intentional, stopping programs from doing exactly what you contemplate doing, killing a process to acquire a resource. 保持隐藏此信息是非常有意的,阻止程序完全按照您的想法进行操作,从而终止获取资源的过程。 And in general is a countermeasure to malware that tries to take advantage of handle recycle attacks. 一般来说,这是针对恶意软件的一种对策,它试图利用处理循环攻击。

Your user could use SysInternals' Handle utility to find the process, assuming he doesn't know which ought to be rare. 您的用户可以使用SysInternals的Handle实用程序来查找该过程,假设他不知道哪个应该是罕见的。 Doing what Handle.exe does in C# code is a very long shot that you should never commit to. 在C#代码中执行Handle.exe所做的事情是一个非常长的镜头,你永远不应该承诺。 It dynamically installs a device driver so it can gain ring 0 permissions to access the kernel data. 它动态安装设备驱动程序,以便获得第0个访问内核数据的权限。 And the kernel structures have changed between Windows versions and are liable to change again in future versions. 并且Windows版本之间的内核结构发生了变化,并且在将来的版本中可能会再次发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM