Hi!
Previously I defined com-port number using DBT_DEVICEARRIVAL:
procedure TMainForm.WMDEVICECHANGE(var Msg: TWMDeviceChange);
var
lpdb : PDevBroadcastHdr;
lpdbpr: PDevBroadCastPort;
S: AnsiString;
begin
{Заголовок сообщения}
lpdb := PDevBroadcastHdr(Msg.dwData);
case Msg.Event of
DBT_DEVICEARRIVAL:
begin {Добавление}
if lpdb^.dbch_devicetype = DBT_DEVTYP_PORT {DBT_DEVTYP_DEVICEINTERFACE} then begin
lpdbpr:= PDevBroadCastPort(Msg.dwData);
S := StrPas(PWideChar(@lpdbpr.dbcp_name));
GetSystemController.Init(S);
end;
end;
DBT_DEVICEREMOVECOMPLETE:
begin {Удаление}
if lpdb^.dbch_devicetype = DBT_DEVTYP_PORT then begin
lpdbpr:= PDevBroadCastPort(Msg.dwData);
S := StrPas(PWideChar(@lpdbpr.dbcp_name));
GetSystemController.ProcessDisconnect(S);
end;
end;
end;
end;
Unfortunately, the hardware part of a device with which I was working changed and now Msg.Event has value BT_DEVNODES_CHANGED.
I've read msdn. It is said that I should use RegisterDeviceNotification to get any additional information.
But, if I got it right, it can't be used for serial ports.
The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events are automatically broadcast to all top-level windows for port devices. Therefore, it is not necessary to call RegisterDeviceNotification for ports, and the function fails if the dbch_devicetype member is DBT_DEVTYP_PORT.
So, I am confused. How can I define the com-port of a device, if a get DBT_DEVNODES_CHANGED in WMDEVICECHANGE event?