Code: Select all
Structure API_PORT_INFO_2
PortName.s
MonitorName.s
Description.s
PortType.l
Reserved.l
EndStructure
Global NewList Portinfos.API_PORT_INFO_2()
Procedure.l GetAvailablePorts(ServerName.s="")
;http://www.purebasic.fr/english/viewtopic.php?p=215002#215002
Protected res.l
Protected pcbNeeded.l
Protected pcReturned .l
Protected *TempBuff
Protected i.l
Protected *pName
If ServerName=""
*pName=0
Else
*pName=@ServerName
EndIf
;Dim PortsStruct(0 To 100) As API_PORT_INFO_2
;Get the number of bytes needed To contain the Data returned by the API call
ret = EnumPorts_(*pName, 2, 0, 0, @pcbNeeded, @pcReturned)
If pcbNeeded
Debug "pcbNeeded = "+Str(pcbNeeded)+" Bytes"
*TempBuff = AllocateMemory(pcbNeeded) ; Allocate the Buffer
ret = EnumPorts_(*pName, 2, *TempBuff, pcbNeeded, @pcbNeeded, @pcReturned)
If ret
Debug "Ports received = "+Str(pcReturned)
;Conversion is not needed, PB makes it ;-)
For i = 0 To pcReturned - 1
;set structure over the memory area
*strPortinfos.PORT_INFO_2=*TempBuff+(i*SizeOf(PORT_INFO_2))
AddElement(Portinfos())
Portinfos()\PortName=PeekS(*strPortinfos\pPortName)
Portinfos()\MonitorName=PeekS(*strPortinfos\pMonitorName)
Portinfos()\Description=PeekS(*strPortinfos\pDescription)
Portinfos()\PortType=*strPortinfos\fPortType
Next
EndIf
;Free the Heap Space allocated for the Buffer
If *TempBuff
FreeMemory(*TempBuff)
EndIf
EndIf
ProcedureReturn pcReturned
EndProcedure
If GetAvailablePorts("\\Klaus-Core2")
ForEach Portinfos()
Debug Portinfos()\PortName
Debug Portinfos()\MonitorName
Debug Portinfos()\Description
Debug Portinfos()\PortType
Debug "-------------------------------------------------------------"
Next
EndIf