Enabling and disabling the com ports programatically in which the printer is connected.

Hi all,

Can I disable and enable the com ports programatically .I have
connected my printer in com port(com1), and create a handle to the com
port using createfile function to which the printer is connected.I
checked the handle returned by CreateFile() for INVALID_HANDLE_VALUE,it
is ok.But the function GetCommState(hCOM, &dcb),Setcommtimeouts are
failing.
But once I disable and enable the com ports in the device manager
manually,the functions GetCommState(hCOM, &dcb),Setcommtimeouts are
getting succeded. T his also works until I delete the printer driver,if
I install again, I need to disable and enable the com ports in device
manager manually.So, I am on a idea to disable and enable the com ports
programatically. or else how can I rectify this problem

One more observation I found is, when I change to other com
ports(filename) in createfile in which the printer is not connected,the
functions GetCommState(hCOM, &dcb),Setcommtimeouts are getting succeded.

I m doing these things to get some information from printer . In my
printer I can set the baud rate,data bits etc… in the printer ,now I
need to query the printer about the baud rate and data bits set in the
printer and need to display it in the User interface.

My question is

1.)How can I disable and enable the com ports programatically

My code for createfile and getcommstate()

HANDLE hCOM;
DWORD dwError,dwEr1 ;

hCOM = CreateFile( lpFileName, //PrinterPort
GENERIC_READ | GENERIC_WRITE, //Read & write
0, //Share Mode
NULL, //Security Handle
OPEN_EXISTING, //Open the existing
0,//FILE_ATTRIBUTE_NORMAL| FILE_FLAG_OVERLAPPED,
//Overlapped
NULL );
dwError = GetLastError();

if (hCOM == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();
TRACE(“Open failed, error code %d\n”, dwError);
return FALSE;
}
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
dwError = SetCommTimeouts( hCOM, &CommTimeOuts );
dwError = GetLastError();

if (!GetCommState(hCOM, &dcb))
{
TRACE(“Open failed, error code %d\n”, dwError);
AfxMessageBox(“fail”);
}