Look RS232 > RS232C Hardware  
fCoder SIA
fCoder SIA Look RS232 Download Order Support Cookie Policy
Look RS232
Download
Order
Support
Testimonials
FAQ
Online help
Modem Programming
Serial port
RS232 Hardware
Programming
  EnumPorts

Besides usual read/write and port setting functions API has the EnumPorts function which allows to detect the quantity of ports present in the system.The convenience of this function is that at launch one can check the list of ports and use only those installed. Function syntax:

BOOL EnumPorts(LPTSTR pName, DWORD Level, LPBYTE pPorts, DWORD cbBuf, LPDWORD pcbNeeded , LPDWORD pcReturned);

Parameters specification:

LPTSTR pName - pointer to a null-terminated string, which contains the name of the server having the ports to be enumerated. If it is a NULL string, the information about the ports is taken from the local machine.

DWORD Level - detects the type of data structures stated in pPorts. This value can equal to 1 or 2.

LPBYTE pPorts - reference to the buffer which receives PORT_INFO_1 or PORT_INFO_2 structures array. Each structure contains data describing the available port. Level value specifies the structure type. Level 1 value specifies PORT_INFO_1 structures, and Level 2 value specifies PORT_INFO_2 structures.

DWORD cbBuf - defines the size (in bytes) of the buffer specified in pPorts.

LPDWORD pcbNeeded - reference to the variable which specifies the data size needed to record the enumerated ports. If cbBuf is smaller than this value EnumPorts isn't executed and GetLastError returns ERROR_INSUFFICIENT_BUFFER and the variable specified by pcbNeeded returns the needed size of the buffer. If cbBuf is equal to or greater than this value the variable specified by pcbNeeded contains the number of bytes saved to the buffer

LPDWORD pcReturned - reference to the variable that returns the number of PORT_INFO_* structures specified in pPorts. In other words this variable contains the number of ports available on the specified server.

Note

EnumPorts function can be executed successfully even if the server specified in pName doesn't detect a printer.

Example:

DWORD cbNeeded = 0,cReturned = 0;

EnumPorts(0,1,0,0,&cbNeeded,&cReturned); // Detection of the necessary memory amount

if (cbNeeded)

{

unsigned char* info = new unsigned char[cbNeeded];

memset(info,0,cbNeeded);

if(EnumPorts(0,1,info,cbNeeded,&cbNeeded,&cReturned ) ) // Reading the information about ports

{

PORT_INFO_1* pi = (PORT_INFO_1*)info; // filling the structure

while(((DWORD)pi-(DWORD)info)/sizeof(PORT_INFO_1)

{

AnsiString str = pi->pName; // Port name

if(str.Pos("COM"))

{

Strings->Add(str.Delete(str.Length(),1)); //adding the port to the list

}

pi ++;

}

delete []info;

}

}

 
  Contact us
© fCoder SIA