Skip to content

Issue with EnumComPorts- Solved #5

@tantegrete

Description

@tantegrete

There is an issue while enumerating available comports and access-violation when closing port.
Returned Stringslist contains additional zeros after portname (here 5x with 10.3.2 and Win 8.1)
This may lead into wrong comparison of a list-entry and a default value: e.g. "COM3ooooo" and "COM3".
The evildoer will be DataLen (in RegEnum) that will return always a value of 10 (here in my setup).
Might be a prob by Microsoft ?!

Next prob is: When i use a 'wrong' port name (with zeros) the designated port will open without error.
But if i close the pot, an access violation occured (reading from adress) !
I couldn't find out the reason yet.
Using 'zero-cleaned' port-names everything is fine.

Here is my modified code :

procedure EnumComPorts(Ports: TStrings);
var
KeyHandle: HKEY;
ErrCode, Index: Integer;
ValueName, Data: array[0..256] of char; // string;
ValueLen, DataLen, ValueType : DWORD;
begin
if NOT Assigned(Ports) then exit;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
'HARDWARE\DEVICEMAP\SERIALCOMM',
0,
KEY_READ,
KeyHandle) <> ERROR_SUCCESS) then exit;
try
Index := 0;
repeat
ValueLen := 256;
DataLen := 256;
ErrCode := RegEnumValue(KeyHandle,
Index,
@valuename, // PChar(ValueName),
ValueLen,
nil,
@ValueType,
PByte(@DaTa), // PByte(PChar(Data)),
@DataLen);
if ErrCode = ERROR_SUCCESS then
begin
Ports.Add(Data); // TmpPorts.Add(Data);
Inc(Index);
end;
until (ErrCode <> ERROR_SUCCESS) ;
finally
RegCloseKey(KeyHandle);
end;
end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions