How to get virtual com-port number if DBT_DEVNODES_CHANGED event accrues?

Posted by Nick Toverovsky on Stack Overflow See other posts from Stack Overflow or by Nick Toverovsky
Published on 2010-03-19T13:03:25Z Indexed on 2010/04/14 8:43 UTC
Read the original article Hit count: 603

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?

© Stack Overflow or respective owner

Related posts about serial-port

Related posts about virtual-com-port