ComPort-Library icon indicating copy to clipboard operation
ComPort-Library copied to clipboard

Issue with EnumComPorts- Solved

Open tantegrete opened this issue 3 years ago • 2 comments

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;

tantegrete avatar May 12 '21 11:05 tantegrete

Oops - no code-formatting ? try with a file... EnumComPorts.txt

tantegrete avatar May 12 '21 11:05 tantegrete

It would be nice if you could fork the project, apply the changes and submit a pull request. Then I can easily merge your changes.

CWBudde avatar Jun 11 '21 22:06 CWBudde