[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check is com port is open

Posted on 2004-09-15
5
Medium Priority
?
355 Views
Last Modified: 2013-11-20
I'm writing a program that needs to check is the user has the com port open. Is there any way to check if it is open and not disrupt the com port if it is?
0
Comment
Question by:greg_100
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12064517
Just call 'CreateFile()' on the COM port in question - if it is in use, you'll get ERROR_ACCESS_DENIED
0
 

Author Comment

by:greg_100
ID: 12065003
Do you have an example of how to do this?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12065094
Well, you could just use

    HANDLE hPort;

    hPort   =   CreateFile  (   "COM1",
                                GENERIC_READ | GENERIC_WRITE,
                                0,
                                0,
                                OPEN_EXISTING,
                                0,
                                0
                            );

    if  (   INVALID_HANDLE_VALUE ==  hPort)
        {

            if ( ERROR_ACCESS_DENIED == GetLastError ())
            {
                // port in use
            }
        }

0
 

Author Comment

by:greg_100
ID: 12068427
jkr,

That worked all I had to add was CloseHandle(hPort); to the end.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12068733
Ooops, sorry, should have mentioned that...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Web hosting control panels were first developed to make it faster and easier for most users to set up and operate websites. The graphical user interface (GUI) allows users to perform tasks by pointing and clicking rather than typing highly specific…
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question