If you really need to do it with VB6, search the web for solutions using something like "vb6 enumerate serial ports" as search term.
Don't try to do it yourself. There are a lot of things to observe like naming of ports with numbers above 9 and that
CreateFile
fails not only for not existing ports but also for existing ports that are actually opened (in use).
The only reliable method besides the .NET
SerialPort.GetPortNames
method is using the Windows Setup API. But I don't know if someone has used that with VB6.
[EDIT: Notes on code posted as comment]
Quote:
every time when i'm running the program when i'm selecting the com port which is available also its showing that port not open pls configure
That is because this always fails due to the wrong share mode:
hCOM = CreateFile("COM" & iPortNum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, _
sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.
To specify a COM port number greater than 9, use the following syntax: "\\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
So it should be:
hCOM = CreateFile("\\.\COM" & iPortNum, GENERIC_READ + GENERIC_WRITE, 0, _
ByVal 0&, OPEN_EXISTING, 0, 0&)
[/EDIT]