views:

82

answers:

2

I understand how to retrieve the UNC path for a mapped drive from the registry (HKEY_CURRENT_USER\Network), but I also have a need to retrieve remote connections to network resources that were not mapped.

For example, opening the 'Run' dialog and typing <\server0123\share$>. If I type "net use", I would see this mapping, but I have been unable to determine where on the file system or registry this information is stored.

alt text

Does anyone know have a location I can query this from, or an API I can call to obtain this? Suggestions involving vbscript, C, and Delphi are more than welcome!

+2  A: 

Mick, try using the Win32_NetworkConnection WMI Class

check this sample

program GetWMI_Win32_NetworkConnection;

{$APPTYPE CONSOLE}

uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;


Procedure  GetWin32_NetworkConnection;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_NetworkConnection','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    Writeln('Caption        '+colItem.Caption);
    Writeln('Name           '+colItem.Name);
    Writeln('ConnectionState'+colItem.ConnectionState);
    Writeln('ConnectionType '+colItem.ConnectionType);
    Writeln('Description    '+colItem.Description);
    Writeln('DisplayType    '+colItem.DisplayType);
    Writeln('LocalName      '+colItem.LocalName);
    Writeln('ProviderName   '+colItem.ProviderName);
    Writeln('RemoteName     '+colItem.RemoteName);
    Writeln('RemotePath     '+colItem.RemotePath);
    Writeln('ResourceType   '+colItem.ResourceType);
    Writeln('Status         '+colItem.Status);
    Writeln('UserName       '+colItem.UserName);
    Writeln;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_NetworkConnection;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.
RRUZ
Note that for a lot of WMI things you need to be a (local) administrator on your PC. I'm not sure that this holds for the Win32_NetworkConnection as well, but I have been bitten by this in the past very badly.
Jeroen Pluimers
+3  A: 

WNetOpenEnum(RESOURCE_REMEMBERED,...)

(If you need to support Win9x, you probably have to fall back to NetUseEnum)

Anders