How to Close Com Ports in Windows?

I have a program that is written in C++ and uses CreateProcess to open Dos programs that use the COm ports. Well the only way I can get this to work is by calling a Batch file from CreateProcess that opens the Dos program from the Batch File. One MAJOR problem I found was that the com port stays open after I closed the Dos program. I have check "Close on Exit" in the options and have even tried to put COM1AutoAssign=2 in the Windows 95\98 system.ini file. This doesn't work either. How can I close the com port once I have closed the Dos program??

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It is the DOS program's responsibility to close the COM port.
Did you try ShellExecute to start the dos program?
Clint112799Author Commented:
Hello Gus,
I have tried ShellExecute, spawn, and createProcess with no luck. Unless I have not done the code correctly, and that's why I haven't been able to get it working? With these spawn applications I found that it doesn't let the Dos program to use the COm ports. The only why I was able to get the com ports to work correctly with the Dos program was using a Batch file. But the side affect is that the com port doesn't close when I try to us it from WIndows. But If i open another Dos program I have no problems? ANy ideals on this? I've been trying to figure this one out for at lest a month.

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Clint112799Author Commented:
Well thats the funny thing. If I use CreateProcess, spawn or ShellExecute it first of all causes the Dos program not to work with the COm ports. So I used a batch file which open the DOs programs and enable the Dos program to use the com ports. But it them cause the problem with tryiing to close the com port. But if I open the Dos program though WIndows Explorer the program works fine and even closes fine? THis is what has me so confused! Why does a spawn call cause these probelms but yet I can open and run the Dos application though windows explorer


Try this code. Im not sure why you need to do this because a program that uses a COM port should obviously have the capabilities in and of itself to open a port.  But this code snippet is how you would programmatically open a COM port for reading and writing in the Win32 environment.

/* Open the COM port for reading and   writing. */
hCom = CreateFile(

/* Make sure handle is valid. */
  cout << GetLastError() << flush;
  return 0;
Open your program that uses the COM ports.
/* To close port use */

Im not sure if this would work because other programs should be blocked from using the port specified in CreateFile once the call has been executed.

However there are parameters called FILE_SHARE_READ and FILE_SHARE_WRITE used in CreateFile() to allow sharing of files between processes, however Im not sure if that applies to COM ports.

Good Luck.
If you open the Dos Program from the batch file, try this code:

      osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);           //  Fill in size
      GetVersionEx( &osvi );                               //  Get OS version info
                                        //  Start a DOS box
if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )          
//  If it's WinNT
sprintf(commandLine,"cmd /C %s",
    commandString);//  Use CMD.EXE
else                                    //  Use for Win95
sprintf(commandLine,"c:\\ /C %s",commandString);
bStatus = CreateProcess( NULL, commandLine, NULL, NULL, TRUE,0,
      NULL, NULL, &si, &pi );

I hope this helps


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Clint112799Author Commented:
Gus, dmaroff, chensu,

Thanks for the help on my program. I do have one thing that might be the reason I can't get this to work. There are two Articles from MSN: Q130402, Q138159. Since I don't know anything about WIndows programming I'm hoping that this will clear up some of the issues. Mybe this is what I'm runign into?

As for the Batch File you suggested, do I still open it the same way as I did before with CreateProcess?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.