Подскажите, плз, как получить WinAPI средствами список доступных COM-портов?
Regards, Sergey
>Подскажите, плз, как получить WinAPI средствами список доступных COM-портов?
>
>
Прочитать из реестра: HKLM\hardware\devicemap\serialcomm
--
With best regards, Vovik
http://zfs.lg.ua/~sands
mailto:sa...@zfs.lg.ua
mailto:sa...@mikroterm.lg.ua
> Подскажите, плз, как получить WinAPI средствами список доступных
COM-портов?
;-------------------------------X8
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[])
{ char buff[512];
int i;
HANDLE fd;
for (i = 1; i < 256; i++)
{ sprintf(buff, "COM%d", i);
fd = CreateFile(buff, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if (fd != INVALID_HANDLE_VALUE)
{ printf("Найден %s\n", buff);
}
}
return 0;
}
;-------------------------------X8
--
chris
Пятница Июнь 30 2006, Sergey Sobolev пишет к All:
SS> Подскажите, плз, как получить WinAPI средствами список доступных
SS> COM-портов?
Вот, откопал в архиве соседней эхи. Код не мой, но у меня работает по тому же
принципу. Только, пожалуйста, не открывай все порты подряд, хотя бы под
NT-образными системами. ;)
procedure EnumSerialPorts(items: TStrings);
const
BufSize = 65535;
var
Buf_DevList : Array[0..BufSize] of Char;
DevName : PChar;
sDevName : string;
bSuccess : boolean;
i : integer;
sPort : string;
hPort : THandle;
dwError : DWORD;
begin
//Make sure we clear out any elements which may already be in the array
items.clear;
//On NT use the QueryDosDevice API
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
//Use QueryDosDevice to look for all devices of the form COMx. This is a
better
//solution as it means that no ports have to be opened at all.
if QueryDosDevice(nil, Buf_DevList, BufSize) = 0 then Exit;
DevName := Buf_DevList;
while DevName^ <> #00 do
begin
if (StrLIComp('COM', DevName, 3) = 0) then
begin
sDevName := StrPas(DevName);
items.Add(sDevName);
end;
DevName := StrEnd(DevName)+1;
end;
end else
begin
//On 95/98 open up each port to determine their existence
//Up to 255 COM ports are supported so we iterate through all of them
seeing
//if we can open them or if we fail to open them, get an access denied or
general error error.
//Both of these cases indicate that there is a COM port at that number.
for i := 1 to 256 do
begin
//Form the Raw device name
sPort := Format('\\.\COM%d', [i]);
//Try to open the port
bSuccess := FALSE;
hPort := CreateFile(PChar(sPort), GENERIC_READ or GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
if (hPort = INVALID_HANDLE_VALUE) then
begin
dwError := GetLastError();
//Check to see if the error was because some other app had the port
open or a general failure
if (dwError = ERROR_ACCESS_DENIED) or (dwError = ERROR_GEN_FAILURE)
then bSuccess := TRUE;
end else
begin
//The port was opened successfully
bSuccess := TRUE;
//Don't forget to close the port, since we are going to do nothing with
it anyway
CloseHandle(hPort);
end;
//Add the port number to the array which will be returned
if (bSuccess) then items.Add(Format('COM%d', [i]));
end;
end;
end;
Пока!
Andrey
■■■ 30 Июн 06, Vladimir Tkatchenko (2:5020/400)
■■■ wrote to Sergey Sobolev:
>> Подскажите, плз, как получить WinAPI средствами список доступных
>> COM-портов?
VT> Прочитать из реестра: HKLM\hardware\devicemap\serialcomm
Hе хочу нарываться на грубость, но "XP Pro", успешно рапортовала о наличии
порта даже после его удаления физически.
Hе показатель, если короче.
Good luck. [ Oleg ]
> >> Подскажите, плз, как получить WinAPI средствами список доступных
> >> COM-портов?
>
> VT> Прочитать из реестра: HKLM\hardware\devicemap\serialcomm
>
> Hе хочу нарываться на грубость,
>
Аналогично!
>но "XP Pro", успешно рапортовала о наличии порта даже после его удаления физически.
> Hе показатель, если короче.
>
>
Не знаю, что там у тебя является показателем, но у меня (и не только)
все прекрасно работает. И даже с платами расширения от Advantech!