这是我的一个dll中的函数
//功能: 得到操作系统中串口,并且添加到TStrings中
//参数: CommList 得到的串口
//返回值: CommList
//备注:在使用过程中,不能根据ItemIndex来得到串口号,应该用
// 选择的Com的尾数来得到串口号。当系统中曾经出现过COM5和COM6,
// 但是当COM5取消后,用原来的方法将会把COM6当作COM6,这样就会
// 有问题
//作者:yanghs
//日期:2003-04-28
pTStrings:=^TStrings;
function WsGetCommToList(CommList
TStrings):Integer;stdcall;
var
i: Integer;
S: String;
ComName: array[0..9] of Char;
TheComHandle: Integer;
j:Integer;
begin
CommList^.Clear();
(*
** Windows 95/98 can only handle up to 50 COM ports.
** All we aredo
ing here is checking to see if Windows
** "can" open up the COM port. If so, then
we know it's
** available. And so we add it to our ComboBox list
** and of course close the COM port each time we check it.
*)
for i := 1 to 20do
//50
begin
StrFmt(ComName, '//./COM%d',
);
//'///COM%d'
TheComHandle := CreateFile
(
ComName, // name
GENERIC_READ or GENERIC_WRITE, // access attributes
0, // no sharing
nil, // no security
OPEN_EXISTING, // creation action
FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_OVERLAPPED, // attributes
0 // no template
);
if (TheComHandle=-1) or (TheComHandle=INVALID_HANDLE_VALUE) then
begin
j:=GetLastError;
if (j=ERROR_ACCESS_DENIED) or (j=ERROR_SUCCESS) then
begin
S := Format('COM%d', );
CommList^.Add(S);
end;
end
else
begin
S := Format('COM%d', );
CommList^.Add(S);
end;
CloseHandle(TheComHandle);
end;
Result:=0;
end;