uses WinSpool;
procedure GetComPortNames(AList: TStrings);
var
BytesNeeded, Returned, I: DWORD;
Success: Boolean;
Buffer: Pointer;
InfoPtr: PPortInfo1;
TempStr: string;
begin
Success := EnumPorts(nil, 1, nil, 0, BytesNeeded, Returned);
if (not Success) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
GetMem(Buffer, BytesNeeded);
try
Success := EnumPorts(nil, 1, Buffer, BytesNeeded, BytesNeeded, Returned);
for I := 0 to Returned - 1 do
begin
InfoPtr := PPortInfo1(DWORD(Buffer) + I * SizeOf(TPortInfo1));
TempStr := InfoPtr^.pName;
if Copy(TempStr, 1, 3) = 'COM' then
AList.Add(Copy(TempStr, 1, 4));
end;
finally
FreeMem(Buffer);
end;
end;
if not Success then
raise EComError.Create('EnumPorts function failed');
end;