How to deregister correctly from receiving notifications using IConnectPoint Unadvise?

395 views Asked by At

In the documentation for Mobile Broadband API, it says:

The following procedure describes how to register for notifications.

1.Get an IConnectionPointContainer interface by calling QueryInterface on an IMbnInterfaceManager > object.
2.Call FindConnectionPoint on the returned interface and pass IID_IMbnPinEvents to riid.
3.Call Advise on the returned connection point and pass a pointer to an IUnknown interface on an > object that implements IMbnPinEvents to pUnk.

Notifications can be terminated by calling Unadvise on the connection point returned in step 2.

I have got some code that carries out the first 3 steps, and it successfully registers for MBN events. However, now I need to de-register temporarily from receiving these events.
So, after a couple of first attempts that ended with COM exceptions, I tried the following code (with try/catch blocks):

//First get the notifications

public void RegisterEvent(object iUnk, Guid guid, out uint storedTag)
{
IConnectionPoint icp = null;
Guid curGuid = guid;
storedTag = 0;
if ((curGuid == typeof(IMbnInterfaceManagerEvents).GUID) )              
{
  // For this event, the connection point is defined on the interface manager object
  m_InterfaceManagerCPC.FindConnectionPoint(ref curGuid, out icp);
  // Call Advise on the connection point to register
  icp.Advise(iUnk, out storedTag);
  //Save the IConnectionPoint object
  interfaceManagerCP = icp;

}

//Now deregister the events

public void DeregisterEvent(Guid guid, uint storedTag)
{
IConnectionPoint icp = null;
Guid curGuid = guid;

// Find the appropriate connection point to call Unadvise on
if ((curGuid == typeof(IMbnInterfaceManagerEvents).GUID) )              
{
  // Call Unadvise on the saved connection point to de-register
  interfaceManagerCP.Unadvise(storedTag);
}

When I run this code, I get no errors or exceptions from MBN. But the event handlers aren't de-registered. I can still see in the log files, MBN events arriving and being handled.

Can anyone tell me what I am missing? Thank you.

1

There are 1 answers

0
user1725145 On BEST ANSWER

I think I figured out the problem. I have many different types of GUIDs, and I thought they all used the same IConnectionPoint, so I saved it to the same object in the RegisterEvent function.

When I tried creating a new IConnectionPoint object for each GUID, and saving each IConnectionPoint separately, the DeregisterEvent function also worked properly.