how to find the number of ports in our PC.

Hello friends,

Can anyone say how to find the number of ports and the name of ports such as COM1, COM2 etc in our PC using a C++ or VC++ program. I searched it everywhere, but i couldnt find a proper source code.

regards,
siva.

[243 byte] By [unleash_siva] at [2008-1-4]
# 1

There is no direct way to do this, plus not every port has name, in fact there are limited ports which are accessed by names (such as COM*).

Can you provide more details on why you need such information ?

RamkrishnaPawar at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
I am creating a GUI application, in which i want to display the COM* names in the listbox. So, if you tell me some way to find the COM* names i can build the list dynamically in my program. I found some code in VB and .NET regarding this. But i couldn't find method to find it in C++ or VC++. Can you tell me some way to solve this issue.
unleash_siva at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3

The way I do this in my app is to sequentially attempt to open each port and then look at the return value

sample code below to open a port

int OpenPort(LPSTR port)

{

HANDLE nCom;

char newport[80];

char ctext[256];

//to open ports com10 and above
//115831
wsprintf(newport,"\\\\.\\%s",port);

nCom=CreateFile(newport,GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);

if (nCom==INVALID_HANDLE_VALUE)
{
int error;

error=GetLastError();
wsprintf(ctext,"OpenComm(%s=%d,%d)\r\n",newport,nCom,error);
OutputDebugString(ctext);

SetLastError(error);

return 0; //no port there
}
else
{
return 1; //port found

}
}

Note:

Must close port after checking it or will not be able to open it again.

Paul

PaulMarriott at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4

Hi Paul,

Thank you so much.

If you come across any other better solution, please mail me.

Regards,

Siva.

unleash_siva at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5
Paul Marriott wrote:

The way I do this in my app is to sequentially attempt to open each port and then look at the return value

sample code below to open a port

int OpenPort(LPSTR port)

{

HANDLE nCom;

char newport[80];

char ctext[256];

//to open ports com10 and above
//115831
wsprintf(newport,"\\\\.\\%s",port);

nCom=CreateFile(newport,GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);

if (nCom==INVALID_HANDLE_VALUE)
{
int error;

error=GetLastError();
wsprintf(ctext,"OpenComm(%s=%d,%d)\r\n",newport,nCom,error);
OutputDebugString(ctext);

SetLastError(error);

return 0; //no port there
}
else
{
return 1; //port found

}
}

Note:

Must close port after checking it or will not be able to open it again.

Paul

That only works for ports that are not already opened. If you attempt to open an opened port you get INVALID_HANDLE_VALUE.

MariusBancila at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 6

I have found this article to have been useful. It discusses multiple ways to get this information:

http://www.naughter.com/enumser.html

kirants at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 7

This is the approach I prefer:

Code Snippet

// get a pointer to a combo

CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_PORT);

// try ports between 1 and 128

for(int i=1; i<=128; i++)
{
CString strPort;
strPort.Format(_T("COM%d"),i);

DWORD dwSize = 0;

// allocate a minimum buffer
LPCOMMCONFIG lpCC = (LPCOMMCONFIG) new BYTE[1];

// call used only to get the actual required size
BOOL ret = GetDefaultCommConfig(strPort, lpCC, &dwSize);
delete [] lpCC;

// allocate the necessary buffer length

lpCC = (LPCOMMCONFIG) new BYTE[dwSize];
ret = GetDefaultCommConfig(strPort, lpCC, &dwSize);

// if call was successfull add the port
if(ret)
{
pCombo->AddString(strPort);
}

delete [] lpCC;
}

MariusBancila at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 8

GetLastError reports

ERROR_ACCESS_DENIED
5
Access is denied.

if the port is already open by another program (but thus it is still there).

The only other catch with trying to open ports method is that if a USB port was on a computer and no longer there, createfile takes a while to return and report it is not there (about 20 secs)

PaulMarriott at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 9
What Marius said is the best solution.
Sarath. at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 10

Thanks for the info.

I have not heard of GetDefaultCommConfig before. The next time I need comms info I will try it out.

Paul

PaulMarriott at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...