2
votes

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?

1

1 Answers

2
votes

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?