views:

63

answers:

1

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?

+2  A: 

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?

Paul-Jan
Thank you for your answer Paul-Jan, I have tried in many ways to solve the problem by creating the object (and member objects) myself.
Ed.C
[pressed enter by mistake] ..but without luck. I should have mentioned: FIntf: INetSharingManager; What really surprises me is that this code complied in Delphi 7 (as the original project). That means there is a problem with the wrapper. It seems to me that nobody gives any attention to these units, It's a pity, considering this is one of the very few examples in internet in managing network connections. Thanks again!
Ed.C
Just noticed the edit. Thank you very much for your effort! It's great to know that it still works! Unfortunately I am still unable to run it (even though it compiles with no errors). I will keep working on it, hopefully will make it soon.
Ed.C