Recent

Author Topic: Usage of IMMNotificationClient to register endpoint notifications  (Read 1122 times)

Nevada Smith

  • New Member
  • *
  • Posts: 20
I am trying to use Windows Core API to register for notifications on audio device changes.

I am able to create a IMMDeviceEnumerator.

Code: Pascal  [Select][+][-]
  1. DeviceEnum := CreateComObject(CLASS_IMMDeviceEnumerator) as IMMDeviceEnumerator;

As the next step, I need to use RegisterEndpointNotificationCallback to register for notifications. I am assuming I have to do something like, but I might be wrong.

Code: Pascal  [Select][+][-]
  1. OleCheck(DeviceEnum.RegisterEndpointNotificationCallback(IMMNotificationClient(@NotificationClient)));

However, I do not know how to instantiate IMMNotificationClient.
1. If I have to use CreateComObject , I do not know the CLSID. I have gone through header files in Mingw, but I could not find the CLSID.
2. In certain C projects, they mock a C++ (COM) object starting with a struct and then by adding appropriate methods.

Please advise...

Nevada Smith

  • New Member
  • *
  • Posts: 20
Re: Usage of IMMNotificationClient to register endpoint notifications
« Reply #1 on: May 19, 2022, 07:52:33 pm »
This was sort of a stupid question. It was as simple as :

Code: Pascal  [Select][+][-]
  1. TDeviceListener = class(TObject, IMMNotificationClient)

You have to implement the virtual methods from IUnknown from which IMMNotificationClient is derived.
Code: Pascal  [Select][+][-]
  1. function TDeviceListener.QueryInterface(constref  iid: TGuid; out obj): HRESULT;
  2.   stdcall;
  3. begin
  4.      // Returning S_OK blinging is not entirely correct, but sort of does the job for proof-of-concept
  5.      Result := S_OK;
  6. end;
  7.  
  8. function TDeviceListener._AddRef(): HRESULT; stdcall;
  9. begin
  10.   Inc(FRefCount);
  11.   Result:=FRefCount;
  12. end;
  13.  
  14. function TDeviceListener._Release(): HRESULT; stdcall;
  15. begin
  16.   Dec(FRefCount);
  17.   Result:=FRefCount;
  18. end;                                                                            

And then use it  :D

Code: Pascal  [Select][+][-]
  1. OleCheck(DeviceEnum.RegisterEndpointNotificationCallback(Self));
« Last Edit: May 19, 2022, 07:54:25 pm by Nevada Smith »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Usage of IMMNotificationClient to register endpoint notifications
« Reply #2 on: May 20, 2022, 05:48:38 am »
Thanks for bothering return to let us know how you solved the issue. It may well help another user in the future.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Usage of IMMNotificationClient to register endpoint notifications
« Reply #3 on: May 20, 2022, 04:09:18 pm »
This was sort of a stupid question. It was as simple as :

Code: Pascal  [Select][+][-]
  1. TDeviceListener = class(TObject, IMMNotificationClient)

Best inherit from TInterfacedObject instead of TObject as that already provides suitable implementations for QueryInterface, _AddRef and _Release.

 

TinyPortal © 2005-2018