Solved

Obtain Network Information

Posted on 2002-05-01
11
306 Views
Last Modified: 2010-04-04
Hi All
How can I obtain information about my network:
Logged-In Users, ServerName, Directory Tree, etc.
I want to get this info from workstation - Win9x.
Use Delphi3. Some working code examples, please!
Thanks in advance
0
Comment
Question by:neshkov
  • 7
  • 3
11 Comments
 

Author Comment

by:neshkov
ID: 6982978
...How to obtain list of users
0
 
LVL 1

Expert Comment

by:piscean
ID: 6983064
Look for the server name first and then obtain from the server the currently logged in users...
0
 

Author Comment

by:neshkov
ID: 6983079
Give me some code example, please!
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:neshkov
ID: 6983149
How to use NetServerGetInfo() and WNetOpenEnum....
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6983627
This a sample code form TeamB

The NetServerGetInfo function is implemented differently on Windows NT and Windows 95/98.
Under NT, it should be imported from NETAPI32.DLL. Under Win9x, it
should be imported from SVRAPI.DLL. The parameters are also a little
different.

This is a code snippet from one of my projects that calls the function
to get the machine type. Note that it may not compile without slight
modification, but it should demonstrate the concepts involved.

const
  CNLEN = 15;
  SV_TYPE_WORKSTATION         = $00000001;
  SV_TYPE_SERVER              = $00000002;
  SV_TYPE_SQLSERVER           = $00000004;
  SV_TYPE_DOMAIN_CTRL         = $00000008;
  SV_TYPE_DOMAIN_BAKCTRL      = $00000010;
  SV_TYPE_TIME_SOURCE         = $00000020;
  SV_TYPE_AFP                 = $00000040;
  SV_TYPE_NOVELL              = $00000080;
  SV_TYPE_DOMAIN_MEMBER       = $00000100;
  SV_TYPE_PRINTQ_SERVER       = $00000200;
  SV_TYPE_DIALIN_SERVER       = $00000400;
  SV_TYPE_XENIX_SERVER        = $00000800;
  SV_TYPE_SERVER_UNIX         = SV_TYPE_XENIX_SERVER;
  SV_TYPE_NT                  = $00001000;
  SV_TYPE_WFW                 = $00002000;
  SV_TYPE_SERVER_MFPN         = $00004000;
  SV_TYPE_SERVER_NT           = $00008000;
  SV_TYPE_POTENTIAL_BROWSER   = $00010000;
  SV_TYPE_BACKUP_BROWSER      = $00020000;
  SV_TYPE_MASTER_BROWSER      = $00040000;
  SV_TYPE_DOMAIN_MASTER       = $00080000;
  SV_TYPE_SERVER_OSF          = $00100000;
  SV_TYPE_SERVER_VMS          = $00200000;
  SV_TYPE_WINDOWS             = $00400000;
  SV_TYPE_DFS                 = $00800000;
  SV_TYPE_ALTERNATE_XPORT     = $20000000;
  SV_TYPE_LOCAL_LIST_ONLY     = $40000000;
  SV_TYPE_DOMAIN_ENUM         = $80000000;
  SV_TYPE_ALL                 = $FFFFFFFF;  

type
  PServerData = ^TServerData;
  TServerData = record
    Name: string[15];
    Comment: string[255];
    VerMajor: Integer;
    VerMinor: Integer;
    ServerType: Integer;
    Connected: Boolean;
  end;

  PServerInfo1 = ^TServerInfo1;
  TServerInfo1 = packed record
    sv1_name: array[0..CNLEN] of Char;
    sv1_version_major: Byte;
    sv1_version_minor: Byte;
    sv1_type: LongInt;
    sv1_comment: PChar;
  end;

  PServerInfo101 = ^TServerInfo101;
  TServerInfo101 = packed record
    sv101_platform_id: Integer;
    sv101_name: PWideChar;
    sv101_version_major: Integer;
    sv101_version_minor: Integer;
    sv101_type: Integer;
    sv101_comment: PWideChar;
  end;

  TNetServerGetInfo95 = function(pszServer: PChar; sLevel: Word;
pbBuffer: Pointer;
  cbBuffer: Word; var pcbTotalAvail: Word): Integer; stdcall;

  TNetServerGetInfoNT = function(pszServer: PWideChar; Level: Integer;
    var pbBuffer: Pointer): Integer; stdcall;

  TNetApiBufferFree = function(Buffer: Pointer): Integer; stdcall;

var
  NetDLLName: string = '';
  NetDLLHandle: THandle = 0;

procedure InitServerDLL;
begin
  if NetDLLName = '' then
  begin
    ifWin32Platform = VER_PLATFORM_WIN32_NT then
      NetDLLName := 'NETAPI32.DLL' else
    if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then
      NetDLLName := 'SVRAPI.DLL';
  end;
  if (NetDLLName <> '') and (NetDLLHandle = 0) then
    NetDLLHandle := LoadLibrary(PChar(NetDLLName));
end;

function GetServerInfo95(const Name: string; var ServerData:
TServerData): Boolean;
var
  NetServerGetInfo: TNetServerGetInfo95;
  pBuffer: Pointer;
  TotalAvail: Word;
  NetResult: Integer;
begin
  Result := False;
  @NetServerGetInfo := GetProcAddress(NetDLLHandle,
'NetServerGetInfo');
  if Assigned(NetServerGetInfo) then
  begin
    Result := True;
    GetMem(pBuffer, 1024);
    NetResult := NetServerGetInfo(PChar(Name), 1, pBuffer, 1024,
TotalAvail);
    if NetResult = 0 then
      with PServerInfo1(pBuffer)^ do
      begin
        ServerData.Name := StrPas(sv1_name);
        if sv1_comment <> nil then
          ServerData.Comment := StrPas(sv1_comment) else
          ServerData.Comment := '';
        ServerData.VerMajor := sv1_version_major;
        ServerData.VerMinor := sv1_version_minor;
        ServerData.ServerType := sv1_type;
      end else
    begin
      ServerData.Name := Name;
      ServerData.Comment := '';
      ServerData.VerMajor := 0;
      ServerData.VerMinor := 0;
      ServerData.ServerType := 0;
    end;
    if NetResult <> ERROR_BAD_NETPATH then
      ServerData.Connected := True;
    FreeMem(pBuffer);
  end;
end;

function GetServerInfoNT(const Name: string; var ServerData:
TServerData): Boolean;
var
  NetServerGetInfo: TNetServerGetInfoNT;
  NetApiBufferFree: TNetApiBufferFree;
  pBuffer: Pointer;
  pszServer: array[0..128] of WideChar;
  NetResult: Integer;
begin
  Result := False;
  @NetServerGetInfo := GetProcAddress(NetDLLHandle,
'NetServerGetInfo');
  @NetApiBufferFree := GetProcAddress(NetDLLHandle,
'NetApiBufferFree');
  if Assigned(NetServerGetInfo) and Assigned(NetApiBufferFree) then
  begin
    Result := True;
    pBuffer := nil;
    NetResult := NetServerGetInfo(StringToWideChar(Name, pszServer,
129),
      101, pBuffer);
    if NetResult = 0 then
      with PServerInfo101(pBuffer)^ do
      begin
        ServerData.Name := WideCharToString(sv101_name);
        if sv101_comment <> nil then
          ServerData.Comment := WideCharToString(sv101_comment) else
          ServerData.Comment := '';
        ServerData.VerMajor := sv101_version_major;
        ServerData.VerMinor := sv101_version_minor;
        ServerData.ServerType := sv101_type;
        ServerData.Connected := True;
      end else
    begin
      ServerData.Name := Name;
      ServerData.Comment := '';
      ServerData.VerMajor := 0;
      ServerData.VerMinor := 0;
      ServerData.ServerType := 0;
      if NetResult <> ERROR_BAD_NETPATH then
        ServerData.Connected := True;
    end;
    NetApiBufferFree(pBuffer);
  end;
end;

function GetServerInfo(const Name: string; var ServerData:
TServerData): Boolean;
begin
  Result := False;
  InitServerDLL;
  if (NetDLLHandle <> 0) then
  begin
    if OSPlatform = VER_PLATFORM_WIN32_NT then
      Result := GetServerInfoNT(Name, ServerData) else
    if OSPlatform = VER_PLATFORM_WIN32_WINDOWS then
      Result := GetServerInfo95(Name, ServerData);
  end;
end;

---
Yorai Aminov (TeamB)

Best Regards

Cesario
0
 
LVL 8

Accepted Solution

by:
Cesario earned 200 total points
ID: 6983639
An Exmaple with WNetOpenEnum

Browsing for a Network Machine (ala Network Neighborhood)

From: mloeffler@teletrade.com (Michael J. Loeffler)
I started messing around with a utility like this, just for fun. I never finished it. I know it did work at the time. You might be able to use some of the code as a base point. Don't know if you feel like poring through the details, but hope it helps.

{
  Network resource utility.  Similar in function to NetWork-
  Neighborhood.

  Michael J. Loeffler
  1997.01.31
}

unit netres_main_unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  ComCtrls, StdCtrls, Buttons, Menus, ExtCtrls;

type
  TfrmMain = class(TForm)
    tvResources: TTreeView;
    btnOK: TBitBtn;
    btnClose: TBitBtn;
    Label1: TLabel;
    barBottom: TStatusBar;
    popResources: TPopupMenu;
    mniExpandAll: TMenuItem;
    mniCollapseAll: TMenuItem;
    mniSaveToFile: TMenuItem;
    mniLoadFromFile: TMenuItem;
    grpListType: TRadioGroup;
    grpResourceType: TRadioGroup;
    dlgOpen: TOpenDialog;
    dlgSave: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure mniExpandAllClick(Sender: TObject);
    procedure mniCollapseAllClick(Sender: TObject);
    procedure mniSaveToFileClick(Sender: TObject);
    procedure mniLoadFromFileClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    ListType, ResourceType: DWORD;
    procedure ShowHint(Sender: TObject);
    procedure DoEnumeration;
    procedure DoEnumerationContainer(NetResContainer: TNetResource);
    procedure AddContainer(NetRes: TNetResource);
    procedure AddShare(TopContainerIndex: Integer; NetRes:
TNetResource);
    procedure AddShareString(TopContainerIndex: Integer; ItemName:
String);
    procedure AddConnection(NetRes: TNetResource);
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.ShowHint(Sender: TObject);
begin
  barBottom.Panels.Items[0].Text:=Application.Hint;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  Application.OnHint:=ShowHint;
  barBottom.Panels.Items[0].Text:='';
end;

procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
  Close;
end;

{
  Enumerate through all network resources:
}
procedure TfrmMain.DoEnumeration;
var
  NetRes: Array[0..2] of TNetResource;
  Loop: Integer;
  r, hEnum, EntryCount, NetResLen: DWORD;
begin
  case grpListType.ItemIndex of
    { Connected resources: }
    1: ListType:=RESOURCE_CONNECTED;
    { Persistent resources: }
    2: ListType:=RESOURCE_REMEMBERED;
    { Global: }
    else ListType:=RESOURCE_GLOBALNET;
  end;

  case grpResourceType.ItemIndex of
    { Disk resources: }
    1: ResourceType:=RESOURCETYPE_DISK;
    { Print resources: }
    2: ResourceType:=RESOURCETYPE_PRINT;
    { All: }
    else ResourceType:=RESOURCETYPE_ANY;
  end;

  Screen.Cursor:=crHourGlass;

  try
    { Delete any old items in the tree view: }
    for Loop:=tvResources.Items.Count-1 downto 0 do
      tvResources.Items[Loop].Delete;
  except
  end;

  { Start enumeration: }
  r:=WNetOpenEnum(ListType,ResourceType,0,nil,hEnum);
  if r<>NO_ERROR then
  begin
    if r=ERROR_EXTENDED_ERROR then
      MessageDlg('Unable to Enumerate the Network.'+#13+
        'A network-specific error occurred.',mtError,[mbOK],0)
    else
      MessageDlg('Unable to Enumerate the Network.',
        mtError,[mbOK],0);
    Exit;
  end;

  try
    { We got a valid enumeration handle; walk the resources: }
    while (1=1) do
    begin
      EntryCount:=1;
      NetResLen:=SizeOf(NetRes);
      r:=WNetEnumResource(hEnum,EntryCount,@NetRes,NetResLen);
      case r of
        0: begin
          { It's a container, iterate it: }
          if NetRes[0].dwUsage=RESOURCEUSAGE_CONTAINER then
            DoEnumerationContainer(NetRes[0])
          else
            { Persistent and connected resources show up here: }
            if ListType in [RESOURCE_REMEMBERED,RESOURCE_CONNECTED]
then
              AddConnection(NetRes[0]);
        end;

        { Done: }
        ERROR_NO_MORE_ITEMS: Break;
        { Other error: }
        else begin
          MessageDlg('Error Walking Resources.',mtError,[mbOK],0);
          Break;
        end;
      end;
    end;

  finally
    Screen.Cursor:=crDefault;
    { Close enumeration handle: }
    WNetCloseEnum(hEnum);
  end;
end;

{
  Enumerate through the specified container:
  This function is usually recursively called.
}
procedure TfrmMain.DoEnumerationContainer(NetResContainer:
TNetResource);
var
  NetRes: Array[0..10] of TNetResource;
  TopContainerIndex: Integer;
  r, hEnum, EntryCount, NetResLen: DWORD;
begin
  { Add container name to tree view: }
  AddContainer(NetResContainer);
  { Keep track of this item as current root: }
  TopContainerIndex:=tvResources.Items.Count-1;
  { Start enumeration: }
  if ListType=RESOURCE_GLOBALNET then
    { Enumerating global net: }
    r:=WNetOpenEnum(ListType,ResourceType,RESOURCEUSAGE_CONTAINER,
      @NetResContainer,hEnum)
  else
    { Enumerating connections or persistent (won't normally get here):
}
    r:=WNetOpenEnum(ListType,ResourceType,RESOURCEUSAGE_CONTAINER,
      nil,hEnum);
  { Couldn't enumerate through this container; just make a note
    of it and continue on: }
  if r<>NO_ERROR then
  begin
    AddShareString(TopContainerIndex,'<Couldn''t Enumerate Resources
(Error #'+
      IntToStr(r)+'>');
    WNetCloseEnum(hEnum);
    Exit;
  end;

  { We got a valid enumeration handle; walk the resources: }
  while (1=1) do
  begin
    EntryCount:=1;
    NetResLen:=SizeOf(NetRes);
    r:=WNetEnumResource(hEnum,EntryCount,@NetRes,NetResLen);
    case r of
      0: begin
        { Yet another container to enumerate; call this function
          recursively to handle it: }
        if (NetRes[0].dwUsage=RESOURCEUSAGE_CONTAINER) or
          (NetRes[0].dwUsage=10) then
          DoEnumerationContainer(NetRes[0])
        else
          case NetRes[0].dwDisplayType of
            { Top level type: }
            RESOURCEDISPLAYTYPE_GENERIC,
            RESOURCEDISPLAYTYPE_DOMAIN,
            RESOURCEDISPLAYTYPE_SERVER: AddContainer(NetRes[0]);
            { Share: }
            RESOURCEDISPLAYTYPE_SHARE:
AddShare(TopContainerIndex,NetRes[0]);
          end;
        end;
      ERROR_NO_MORE_ITEMS: Break;
      else begin
        MessageDlg('Error #'+IntToStr(r)+' Walking
Resources.',mtError,[mbOK],0);
        Break;
      end;
    end;
  end;

  { Close enumeration handle: }
  WNetCloseEnum(hEnum);
end;

procedure TfrmMain.FormShow(Sender: TObject);
begin
  DoEnumeration;
end;

{
  Add item to tree view; indicate that it is a container:
}
procedure TfrmMain.AddContainer(NetRes: TNetResource);
var
  ItemName: String;
begin
  ItemName:=Trim(String(NetRes.lpRemoteName));
  if Trim(String(NetRes.lpComment))<>'' then
  begin
    if ItemName<>'' then ItemName:=ItemName+'  ';
    ItemName:=ItemName+'('+String(NetRes.lpComment)+')';
  end;
  tvResources.Items.Add(tvResources.Selected,ItemName);
end;

{
  Add child item to container denoted as current top:
}
procedure TfrmMain.AddShare(TopContainerIndex: Integer; NetRes:
TNetResource);
var
  ItemName: String;
begin
  ItemName:=Trim(String(NetRes.lpRemoteName));
  if Trim(String(NetRes.lpComment))<>'' then
  begin
    if ItemName<>'' then ItemName:=ItemName+'  ';
    ItemName:=ItemName+'('+String(NetRes.lpComment)+')';
  end;

tvResources.Items.AddChild(tvResources.Items[TopContainerIndex],ItemName);
end;

{
  Add child item to container denoted as current top;
  this just adds a string for purposes such as being unable
  to enumerate a container.  That is, the container's shares
  are not accessible to us.
}
procedure TfrmMain.AddShareString(TopContainerIndex: Integer;
ItemName: String);
begin

tvResources.Items.AddChild(tvResources.Items[TopContainerIndex],ItemName);
end;

{
  Add a connection to the tree view.
  Mostly used for persistent and currently connected
  resources to be displayed.
}
procedure TfrmMain.AddConnection(NetRes: TNetResource);
var
  ItemName: String;
begin
  ItemName:=Trim(String(NetRes.lpLocalName));
  if Trim(String(NetRes.lpRemoteName))<>'' then
  begin
    if ItemName<>'' then ItemName:=ItemName+'  ';
    ItemName:=ItemName+'-> '+Trim(String(NetRes.lpRemoteName));
  end;
  tvResources.Items.Add(tvResources.Selected,ItemName);
end;

{
  Expand all containers in the tree view:
}
procedure TfrmMain.mniExpandAllClick(Sender: TObject);
begin
  tvResources.FullExpand;
end;

{
  Collapse all containers in the tree view:
}
procedure TfrmMain.mniCollapseAllClick(Sender: TObject);
begin
  tvResources.FullCollapse;
end;

{
  Allow saving of tree view to a file:
}
procedure TfrmMain.mniSaveToFileClick(Sender: TObject);
begin
  if dlgSave.Execute then
    tvResources.SaveToFile(dlgSave.FileName);
end;

{
  Allow loading of tree view from a file:
}
procedure TfrmMain.mniLoadFromFileClick(Sender: TObject);
begin
  if dlgOpen.Execute then
    tvResources.LoadFromFile(dlgOpen.FileName);
end;

{
  Rebrowse:
}
procedure TfrmMain.btnOKClick(Sender: TObject);
begin
  DoEnumeration;
end;

end.


Best Regards

Cesario
0
 

Author Comment

by:neshkov
ID: 6983793
Thank you very much Cesario. The points are yours (for the second answer) but do you can write somthing more simple - with one ListBox and one Button for example.  
0
 

Author Comment

by:neshkov
ID: 6983799
I have found this answer in PAQ : http://www.experts-exchange.com/questions/Q.20130805.html
Best Regards
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6983831
good luck ;-)
0
 

Author Comment

by:neshkov
ID: 6983848
...OK I'll be back as soon as possible. I must sleep. as I say - the points are yours...
0
 

Author Comment

by:neshkov
ID: 6983858
Give me some simple example!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Virtuailstring tree compare node issue 14 127
Adoquery sql  left join does not work 25 99
firemonkey Android Listview Sort items 7 54
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question