Solved

Obtain Network Information

Posted on 2002-05-01
11
301 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
Comment Utility
...How to obtain list of users
0
 
LVL 1

Expert Comment

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

Author Comment

by:neshkov
Comment Utility
Give me some code example, please!
0
 

Author Comment

by:neshkov
Comment Utility
How to use NetServerGetInfo() and WNetOpenEnum....
0
 
LVL 8

Expert Comment

by:Cesario
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Accepted Solution

by:
Cesario earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
good luck ;-)
0
 

Author Comment

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

Author Comment

by:neshkov
Comment Utility
Give me some simple example!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now