TNetSharingManager访问冲突问题

发布于 2024-09-28 10:33:27 字数 1555 浏览 5 评论 0原文

我正在尝试在 Delphi 2010 中编译这个项目,它使用TNetSharingManager。我已导入类型库并尝试编译它,但不幸的是我在此函数中遇到访问冲突:

function TNetSharingManager.GetDefaultInterface: INetSharingManager;
begin
  if FIntf = nil then
    Connect;
  Assert(FIntf  nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call "Connect" or "ConnectTo" before this operation');
  Result := FIntf;
end;

(NETCONLib_TLB 的一部分) 错误在于: if FIntf = nil then 由于某些奇怪的原因。

调用它的代码:

procedure TForm1.GetConnectionList(Strings,IdList: TStrings);
var
  pEnum: IEnumVariant;
  vNetCon: OleVARIANT;
  dwRetrieved: Cardinal;
  pUser: NETCONLib_TLB.PUserType1;
  NetCon : INetConnection;
begin
  Strings.Clear;
  IdList.Clear;
  pEnum := ( NetSharingManager.EnumEveryConnection._NewEnum as IEnumVariant);
  while (pEnum.Next(1, vNetCon, dwRetrieved) = S_OK) do
  begin
     (IUnknown(vNetCon) as INetConnection).GetProperties(pUser);
     NetCon := (IUnknown(vNetCon) as INetConnection);

     if (pUser.Status in [NCS_CONNECTED,NCS_CONNECTING])//remove if you want disabled NIC cards also
     and (pUser.MediaType in [NCM_LAN,NCM_SHAREDACCESSHOST_LAN,NCM_ISDN] )
     and (GetMacAddress(GuidToString(pUser.guidId))'' ) then
     begin
       //we only want valid network cards that are enabled
       Strings.Add(pUser.pszwName );
       IdList.Add(GuidToString(pUser.guidId));
     end;
  end;
end;

我不明白为什么我不能与 nil 进行比较。有什么想法吗?

I'm trying to compile this project in Delphi 2010, which uses TNetSharingManager. I have imported the type library and tried compiling it, but unfortunately I'm getting an Access Violation in this function:

function TNetSharingManager.GetDefaultInterface: INetSharingManager;
begin
  if FIntf = nil then
    Connect;
  Assert(FIntf  nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call "Connect" or "ConnectTo" before this operation');
  Result := FIntf;
end;

(part of NETCONLib_TLB)
The error is in : if FIntf = nil then for some odd reason..

The code which is calling it:

procedure TForm1.GetConnectionList(Strings,IdList: TStrings);
var
  pEnum: IEnumVariant;
  vNetCon: OleVARIANT;
  dwRetrieved: Cardinal;
  pUser: NETCONLib_TLB.PUserType1;
  NetCon : INetConnection;
begin
  Strings.Clear;
  IdList.Clear;
  pEnum := ( NetSharingManager.EnumEveryConnection._NewEnum as IEnumVariant);
  while (pEnum.Next(1, vNetCon, dwRetrieved) = S_OK) do
  begin
     (IUnknown(vNetCon) as INetConnection).GetProperties(pUser);
     NetCon := (IUnknown(vNetCon) as INetConnection);

     if (pUser.Status in [NCS_CONNECTED,NCS_CONNECTING])//remove if you want disabled NIC cards also
     and (pUser.MediaType in [NCM_LAN,NCM_SHAREDACCESSHOST_LAN,NCM_ISDN] )
     and (GetMacAddress(GuidToString(pUser.guidId))'' ) then
     begin
       //we only want valid network cards that are enabled
       Strings.Add(pUser.pszwName );
       IdList.Add(GuidToString(pUser.guidId));
     end;
  end;
end;

I don't understand why I cannot compare with nil. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-10-05 10:33:27

当触发该错误时,TNetSharingManager 对象本身实际上可能已经死亡(或者首先没有创建)。 FIntF = nil 表达式是对类的实际字段的第一个引用,即它将指向无效的地址空间。

[编辑] 我下载了源代码并按照步骤导入了 TLB (Delphi 2010)。要执行该应用程序,我必须 (a) 以管理员身份运行 Delphi,因为默认情况下我不是高级用户,并且 (b) 必须添加对 pUser pUser <> 的检查。 nil 因为最终的 getProperties 返回一个 nil 结构,但除此之外代码运行良好。不幸的是,我似乎无法重现您的问题。

重读您的问题,您在编译时是否获得了 AV?

It is likely the TNetSharingManager object itself has actually died (or wasn't created in the first place) when that error is triggered. The FIntF = nil expression is the first reference to an actual field of the class, i.e. it will be pointing into invalid address space.

[Edit] I download the source and followed the steps to import the TLB (Delphi 2010). To execute the appilcation, I had to (a) run Delphi as an admin, because I'm not a power user by default and (b) had to add a check for pUser <> nil because the final getProperties returns a nil-structure, but other than that the code run fine. So unfortunately, I can't seem to reproduce your problem.

Rereading your question, are you getting an AV while compiling?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文