In my Delphi application I'd like to get informed about network changes using the Microsoft Windows Network List Manager API (NLM): http://msdn.microsoft.com/library/ee264321
I've looked at the linked "How to register for NLM events" example and translated it to Delphi. However, I have no idea how to continue with this.
var
pNLM: INetworkListManager;
pCpc: IConnectionPointContainer;
pConnectionPoint: IConnectionPoint;
pSink: IUnknown;
dwCookie: LongInt;
const
IID_IConnectionPointContainer: TGUID = '{B196B284-BAB4-101A-B69C-00AA00341D07}';
IID_IUnknown: TGUID = '{00000000-0000-0000-C000-000000000046}';
begin
if Succeeded(CoCreateInstance(CLASS_NetworkListManager, nil, CLSCTX_ALL, IID_INetworkListManager, pNLM)) then
begin
if Succeeded(pNLM.QueryInterface(IID_IConnectionPointContainer, pCpc)) then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkEvents, pConnectionPoint)) then
begin
if Succeeded(pCpc.QueryInterface(IID_IUnknown, pSink)) then
begin
pConnectionPoint.Advise(pSink, dwCookie);
end;
end;
end;
end;
end;
The article sais:
"After you have created the INetworkListManager object above you will receive INetworkEvents notifications from that point forward. pSink implements the INetworkEvent interface including those event processing methods such as NetworkAdded, NetworkDeleted, NetworkConnectivityChanged, and NetworkPropertyChanged."
Unfortunately I have no idea how to do that. There's no further instructions and so I hope someone here could instruct me / provide an example what else to do to call custom procedures for those events. Thanks.
You are passing the wrong object to
Advise()
. You are passing theIConnectionPointContainer
object. You need to instead write your own class that implements theINetworkEvents
interface, then create an instance of the class and pass that object toAdvise()
. That is how the NLM (or any other COM object that uses Connection Points) is able to send events to your code.Update: You need to change your
NLMEvents
unit to keep the NLM object alive afterStartNLMEventListener()
exits, then the events will work correctly. The way you have it coded, the NLM object is local toStartNLMEventListener()
and thus is released whenStartNLMEventListener()
exits, which unregisters your event sink.Try this instead: