gpt4 book ai didi

delphi - 注册 NLM 事件(INetworkListManager、Advise、Sink 等)

转载 作者:行者123 更新时间:2023-12-02 02:40:44 30 4
gpt4 key购买 nike

在我的 Delphi 应用程序中,我想使用 Microsoft Windows 网络列表管理器 API (NLM) 获取有关网络更改的信息:http://msdn.microsoft.com/library/ee264321

我查看了链接的“如何注册 NLM 事件”示例并将其翻译为 Delphi。但是,我不知道如何继续下去。

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;

文章说:

"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."

不幸的是我不知道该怎么做。没有进一步的说明,所以我希望这里有人可以指导我/提供一个示例,还可以做什么来为这些事件调用自定义过程。谢谢。

最佳答案

您向 Advise() 传递了错误的对象。您正在传递 IConnectionPointContainer 对象。您需要编写自己的类来实现 INetworkEvents 接口(interface),然后创建该类的实例并将该对象传递给 Advise()。这就是 NLM(或使用连接点的任何其他 COM 对象)能够将事件发送到您的代码的方式。

更新:您需要更改 NLMEvents 单元,以在 StartNLMEventListener() 退出后保持 NLM 对象处于事件状态,然后事件才会起作用正确。按照您的编码方式,NLM 对象是 StartNLMEventListener() 的本地对象,因此当 StartNLMEventListener() 退出时会被释放,从而注销您的事件接收器。

试试这个:

unit NLMEvents;

interface

function StartNLMEventListener: HResult;
function StopNLMEventListener: HResult;

implementation

uses
Windows, ActiveX, NETWORKLIST_TLB, SysUtils, Unit1;

type
TMyNetworkEvents = class(TInterfacedObject, INetworkEvents, INetworkConnectionEvents, INetworkListManagerEvents)
public
function NetworkAdded(networkId: TGUID): HResult; stdcall;
function NetworkConnectivityChanged(networkId: TGUID; NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
function NetworkDeleted(networkId: TGUID): HResult; stdcall;
function NetworkPropertyChanged(networkId: TGUID; fFlags: NLM_NETWORK_PROPERTY_CHANGE): HResult; stdcall;
function ConnectivityChanged(NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
function NetworkConnectionConnectivityChanged(networkId: TGUID; NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
function NetworkConnectionPropertyChanged(networkId: TGUID; fFlags: NLM_NETWORK_PROPERTY_CHANGE): HResult; stdcall;
end;

var
pNLM: INetworkListManager = nil;
dwCookie1: LongInt = -1;
dwCookie2: LongInt = -1;
dwCookie3: LongInt = -1;

const
IID_IConnectionPointContainer: TGUID = '{B196B284-BAB4-101A-B69C-00AA00341D07}';
//IID_IUnknown: TGUID = '{00000000-0000-0000-C000-000000000046}';
//CLSID_NetworkListManager: TGUID = '{DCB00C01-570F-4A9B-8D69-199FDBA5723B}';

function TMyNetworkEvents.ConnectivityChanged(NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('ConnectivityChanged');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkConnectionConnectivityChanged(networkId: TGUID; NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkConnectionConnectivityChanged');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkConnectionPropertyChanged(networkId: TGUID; fFlags: NLM_NETWORK_PROPERTY_CHANGE): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkConnectionPropertyChange');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkAdded(networkId: TGUID): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkAdded');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkConnectivityChanged(networkId: TGUID; NewConnectivity: NLM_CONNECTIVITY): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkConnectivityChanged');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkDeleted(networkId: TGUID): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkDeleted');
Result := S_OK;
end;

function TMyNetworkEvents.NetworkPropertyChanged(networkId: TGUID; fFlags: NLM_NETWORK_PROPERTY_CHANGE): HResult; stdcall;
begin
Form2.Memo1.Lines.Add('NetworkPropertyChanged');
Result := S_OK;
end;

function StartNLMEventListener: HResult;
var
pCpc: IConnectionPointContainer;
pConnectionPoint: IConnectionPoint;
pSink: INetworkEvents;
begin
if pNLM = nil then
begin
Result := CoCreateInstance(CLASS_NetworkListManager, nil, CLSCTX_ALL, IID_INetworkListManager, pNLM);
if Failed(Result) then
Exit;
end else
begin
Result := S_OK;
end;

if Succeeded(pNLM.QueryInterface(IID_IConnectionPointContainer, pCpc)) then
begin
pSink := TMyNetworkEvents.Create as INetworkEvents;

if dwCookie1 = -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkEvents, pConnectionPoint)) then
begin
pConnectionPoint.Advise(pSink, dwCookie1);
pConnectionPoint := nil;
end;
end;

if dwCookie2 = -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkConnectionEvents, pConnectionPoint)) then
begin
pConnectionPoint.Advise(pSink, dwCookie2);
pConnectionPoint := nil;
end;
end;

if dwCookie3 = -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkListManagerEvents, pConnectionPoint)) then
begin
pConnectionPoint.Advise(pSink, dwCookie3);
pConnectionPoint := nil;
end;
end;
end;
end;

function StopNLMEventListener: HResult;
var
pCpc: IConnectionPointContainer;
pConnectionPoint: IConnectionPoint;
begin
if pNLM <> nil then
begin
if Succeeded(pNLM.QueryInterface(IID_IConnectionPointContainer, pCpc)) then
begin
if dwCookie1 <> -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkEvents, pConnectionPoint)) then
begin
pConnectionPoint.Unadvise(dwCookie1);
pConnectionPoint := nil;
end;
end;

if dwCookie2 <> -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkConnectionEvents, pConnectionPoint)) then
begin
pConnectionPoint.Unadvise(dwCookie2);
pConnectionPoint := nil;
end;
end;

if dwCookie3 <> -1 then
begin
if Succeeded(pCpc.FindConnectionPoint(IID_INetworkListManagerEvents, pConnectionPoint)) then
begin
pConnectionPoint.Unadvise(dwCookie3);
pConnectionPoint := nil;
end;
end;
end;

pNLM := nil;
end;

dwCookie1 := -1;
dwCookie2 := -1;
dwCookie3 := -1;

Result := S_OK;
end;

end.

关于delphi - 注册 NLM 事件(INetworkListManager、Advise、Sink 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24553706/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com