here is a code-snippet to enumerate the COM-Ports :
Code: Select all
#DIGCF_DEFAULT = 1
#DIGCF_PRESENT = 2
#DIGCF_ALLCLASSES = 4
#DIGCF_PROFILE = 8
#DIGCF_DEVICEINTERFACE = 16
#SPDRP_DEVICEDESC = 0
#MAX_CLASS_NAME_LEN = 128
#DIREG_DEV = 1
#DIREG_DRV = 2
#DICS_FLAG_GLOBAL = 1
#DICS_FLAG_CONFIGSPECIFIC = 2
#SPDRP_DEVICEDESC = 0 ; ECP-Druckeranschluss
#SPDRP_HARDWAREID = 1 ; ACPI\PNP0401
#SPDRP_COMPATIBLEIDS = 2 ; (err13)
#SPDRP_SERVICE = 4 ; Parport
#SPDRP_CLASS = 7 ; Ports
#SPDRP_CLASSGUID = 8 ; {4D36E978-E325-11CE-BFC1-08002BE10318}
#SPDRP_DRIVER = 9 ; {4D36E978-E325-11CE-BFC1-08002BE10318}\0000
#SPDRP_CONFIGFLAGS = 10 ; 127 (Zahl=Long)
#SPDRP_MFG = 11 ; (Standardanschlusstypen)
#SPDRP_FRIENDLYNAME = 12 ; ECP-Druckeranschluss (LPT1)
#SPDRP_LOCATION_INFORMATION = 13 ; (err13)
#SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 14 ; \Device\0000005c
#SPDRP_CAPABILITIES = 15 ; 48 (Zahl=Long)
#SPDRP_UI_NUMBER = 16 ; (err13)
#SPDRP_UPPERFILTERS = 17 ; (err13)
#SPDRP_LOWERFILTERS = 18
#SPDRP_BUSTYPEGUID = 19
#SPDRP_LEGACYBUSTYPE = 20
#SPDRP_BUSNUMBER = 21
#SPDRP_ENUMERATOR_NAME = 22 ; ACPI
#SPDRP_SECURITY = 23
#SPDRP_SECURITY_SDS = 24
#SPDRP_DEVTYPE = 25
#SPDRP_EXCLUSIVE = 26
#SPDRP_CHARACTERISTICS = 27
#SPDRP_ADDRESS = 28
#SPDRP_UI_NUMBER_DESC_FORMAT = 30
; Structure SP_DEVICE_INTERFACE_DATA
; cbSize.l
; InterfaceClassGuid.GUID
; Flags.l
; Reserved.l
; EndStructure
Structure SP_DEVINFO_DATA
cbSize.l
ClassGuid.GUID
DevInst.l
Reserved.l
EndStructure
Structure ComArrayStructure
ComPort.s
EndStructure
Procedure EnumComPorts()
zClassName.s="Ports"
PortName.s=""
DeviceInterfaceData.SP_DEVICE_INTERFACE_DATA
DeviceInfoData.SP_DEVINFO_DATA
hDeviceInfoSet.l=0
Size.l=0
hKeyDevice.l=0
DevCount.l=0
value.l=0
value=SetupDiClassGuidsFromName_(zClassName,0,0,@Size) ; only ask for size of array
If value=0
err=GetLastError_()
If err=#ERROR_INSUFFICIENT_BUFFER ; we will get this error and know the arraysize now
Debug "The data area passed to a system call is too small."
Debug "RequiredSize : "+Str(Size)
EndIf
If Size>0
Dim GuidArray.GUID(Size)
value=SetupDiClassGuidsFromName_(zClassName,@GuidArray(1),SizeOf(GUID)*Size,@Size)
If value>0
hDeviceInfoSet=SetupDiGetClassDevs_(@GuidArray(1),0,0,#DIGCF_PRESENT)
err=GetLastError_()
If err=#ERROR_IO_PENDING Or err=0
Debug "Overlapped I/O operation is in progress."
If hDeviceInfoSet<>#INVALID_HANDLE_VALUE
DeviceInfoData\cbSize=SizeOf(DeviceInfoData)
DeviceInterfaceData\cbSize=SizeOf(DeviceInterfaceData)
NewList ComArray.ComArrayStructure()
Repeat
value=SetupDiEnumDeviceInfo_(hDeviceInfoSet,DevCount,@DeviceInfoData)
If value=0
err=GetLastError_()
If err=#ERROR_NO_MORE_ITEMS
Debug "No more data is available."
EndIf
Break
EndIf
hKeyDevice=SetupDiOpenDevRegKey_(hDeviceInfoSet,@DeviceInfoData,#DICS_FLAG_GLOBAL,@HwProfile,#DIREG_DEV,#KEY_QUERY_VALUE)
PortName=Space(128)
lpValueName.s="portname"
lpcbData.l=Len(lpValueName)
RegQueryValueEx_(hKeyDevice,@lpValueName,0,0,@PortName,@lpcbData)
If Left(PortName,3)="COM"
AddElement(ComArray())
ComArray()\ComPort=PortName
Debug PortName
EndIf
RegCloseKey_(hKeyDevice)
DevCount+1
ForEver
If hDeviceInfoSet
SetupDiDestroyDeviceInfoList_(hDeviceInfoSet)
hDeviceInfoSet=0
EndIf
ForEach ComArray()
Debug "Found : "+ComArray()\ComPort
Next
EndIf
EndIf
EndIf
EndIf
EndIf
EndProcedure
EnumComPorts()
(Pierre Bellisle)
Edited 3 times (forgot the constants / Structures / bug in errorhandling thx droopy)
Thanks to Bingo for testing
