Hi,
Is it possible to obtain the handle to a serial port and close it, in case another app has opened it and not closed it?
Hi,
Is it possible to obtain the handle to a serial port and close it, in case another app has opened it and not closed it?
Generally, you should not be able to do that. I think it is technically possible to do, as sysinternals' Process Explorer can close files open by other apps, and I expect serial port to be treated the same as a file at that level.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, I agree, it's not something you generaly should be allowed to do, but I'm forced to work with lots of badly written software that uses the serial port and crash quite frequently, thus forcing me to reboot my computer to release the serial port. It would be quite time saving (and of course extremely satisfying :-)) if I could write an app that closes the port for me, allowing me to continue working after such incidents.
Perhaps figuring out why the apps are crashing and fix them to close the serial port would be a better spent effort. Also, if the app is still holding the port open, it is likely that the app hasn't exited, which may cause OTHER problems (including that it still has pending operations on the serial port that will interfere with your own app).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The apps are proprietary software produced by big companies (automation industry), and there is not much I can do except reporting bugs and sending in crash logs. The software is used for programming and configuring Programmable Logic Controllers (PLCs). I just don't want to reboot my machine each time the program hangs or doesn't exit properly.
You are right that this is not curing the problem, I am just tired of having to reboot every time, and if I was able to write a simple program that simply gets the handle of the serial port and closes it, so that the same app that just crashed don't complain that the serial port is in use by another app when trying to connect to the PLC, I would be happy.
Well, google for process explorer, then use it's search facility to find if the serial port is open by any app, and force-close it.
I just looked at my "teraterm" application, which has \Device\Serial0 open, and I closed it. Nothing bad has happened, but teraterm is no longer "working".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thank you. That did what I wanted :-)
It would still be interesting to see if someone had some code to do this :-)
Process explorer source-code is available for download, as I understand - I have never lookad at/for it, but I think it is available.
It is almost certainly a undocumented API (and possibly some trickery like pretending to be another process in some way) involved in this.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No, it's even a Win32 api FAQ for decades on BBS and Usenet, mainly on Win32 api group
(news://nntp.aioe.org/comp.os.ms-wind...ogrammer.win32)
(C code)