如何取得系统上可用的通讯端口(COM/LPT)列表?(100分)

  • 主题发起人 VeryCoolBoy
  • 开始时间
V

VeryCoolBoy

Unregistered / Unconfirmed
GUEST, unregistred user!
Y

yaya8163

Unregistered / Unconfirmed
GUEST, unregistred user!
好像通过注册表的某项可以查<br>我找找
 
Y

yaya8163

Unregistered / Unconfirmed
GUEST, unregistred user!
HKEY_LOCAL_MACHINE/hardware/devicemap/serialcomm <br>
 
V

VeryCoolBoy

Unregistered / Unconfirmed
GUEST, unregistred user!
可以直接通过API取得吗?
 
A

aefhh

Unregistered / Unconfirmed
GUEST, unregistred user!
学习<br>
 
N

naughtboy

Unregistered / Unconfirmed
GUEST, unregistred user!
1.<br>procedure TJeffsForm.GetAvailableComPorts();<br>var<br>&nbsp; i : Integer;<br>&nbsp; S : String;<br>&nbsp; ComName: array[0..9] of Char;<br>&nbsp; TheComHandle: Integer;<br><br>begin<br><br>&nbsp; &nbsp; &nbsp; &nbsp; (*<br>&nbsp; &nbsp; &nbsp; &nbsp; ** Okay, Clear everything in the PortsComboBox because<br>&nbsp; &nbsp; &nbsp; &nbsp; ** we want to start off fresh.<br>&nbsp; &nbsp; &nbsp; &nbsp; *)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; PortsComboBox.Clear();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; (*<br>&nbsp; &nbsp; &nbsp; &nbsp; ** Windows 95/98 can only handle up to 50 COM ports.<br>&nbsp; &nbsp; &nbsp; &nbsp; ** All we are doing here is checking to see if Windows<br>&nbsp; &nbsp; &nbsp; &nbsp; ** "can" open up the COM port. &nbsp;If so, then we know it's<br>&nbsp; &nbsp; &nbsp; &nbsp; ** available. &nbsp;And so we add it to our ComboBox list<br>&nbsp; &nbsp; &nbsp; &nbsp; ** and of course close the COM port each time we check it.<br>&nbsp; &nbsp; &nbsp; &nbsp; *)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; for i := 1 to 50 do begin<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StrFmt( ComName, '//./COM%d', );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TheComHandle := CreateFile<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// name<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE, &nbsp;// access attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// no sharing<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// no security<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// creation action<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_ATTRIBUTE_NORMAL or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_FLAG_OVERLAPPED, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no template<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( TheComHandle &lt;&gt; INVALID_HANDLE_VALUE ) then begin<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S := Format('COM%d', );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PortsComboBox.Items.Add(S);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle( TheComHandle );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; end; {end of for loop}<br><br>end;<br>2.<br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br>reg : TRegistry; <br>ts : TStrings; <br>i : integer; <br>begin <br>reg := TRegistry.Create; <br>reg.RootKey := HKEY_LOCAL_MACHINE; <br>reg.OpenKey('hardware/devicemap/serialcomm', <br>false); <br>ts := TStringList.Create; <br>reg.GetValueNames(ts); <br>for i := 0 to ts.Count -1 do begin <br>Memo1.Lines.Add(reg.ReadString(ts.Strings)); <br>end; <br>ts.Free; <br>reg.CloseKey; <br>reg.free; <br>end;
 
V

VeryCoolBoy

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 

Similar threads

回复
0
查看
663
不得闲
S
回复
0
查看
731
SUNSTONE的Delphi笔记
S
S
回复
0
查看
613
SUNSTONE的Delphi笔记
S
顶部