作業上的用的,在網上找了很久都沒翻到代碼。。。。有沒有高手能提供點Delphi寫的獲取電腦上所有串并口資訊的程式。。。
uj5u.com熱心網友回復:
可以從注冊表取串口串列procedure TFormCom.EnumComPorts(Ports: TStrings);
var
KeyHandle: HKEY;
ErrCode, Index: Integer;
ValueName, Data: string;
ValueLen, DataLen, ValueType: DWORD;
TmpPorts: TStringList;
begin
ErrCode:= RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'HARDWARE\DEVICEMAP\SERIALCOMM', 0,
KEY_READ, KeyHandle);
if ErrCode <> ERROR_SUCCESS then
raise Exception.Create('打開串口串列的注冊表項出錯');
TmpPorts:= TStringList.Create;
try
Index:= 0;
repeat
ValueLen:= 256;
DataLen:= 256;
SetLength(ValueName, ValueLen);
SetLength(Data, DataLen);
ErrCode:= RegEnumValue(KeyHandle, Index, PChar(ValueName),
Cardinal(ValueLen), nil, @ValueType, PByte(PChar(Data)), @DataLen);
if ErrCode = ERROR_SUCCESS then
begin
SetLength(Data, DataLen);
TmpPorts.Add(Data);
Inc(Index);
end
else if ErrCode <> ERROR_NO_MORE_ITEMS then
// raise Exception.Create('打開串口串列的注冊表項出錯');
raise Exception.Create('無可用串口');
until (ErrCode <> ERROR_SUCCESS);
TmpPorts.Sort;
Ports.Assign(TmpPorts);
finally
RegCloseKey(KeyHandle);
TmpPorts.Free;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/143639.html
標籤:語言基礎/算法/系統設計
上一篇:一個操作界面在不同主機上多人可能同時使用,如何保存資料?
下一篇:將滑鼠形狀改為筆觸的問題
