Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.691 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Nicht identifiziertes Netzwerk WIN 7

  Alt 11. Mai 2018, 21:48
Hi, versuche mal Anhand dieses Codes herauszufinden ob Dein Ziel-Netzwerk vorhanden ist.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    procedure EnumNetworkProc(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.EnumNetworkProc(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean);
begin
  if aNetResource.dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN, RESOURCEDISPLAYTYPE_SERVER] then
    ListBox1.Items.Add(StringOfChar(' ', aLevel*4) +aNetResource.lpRemoteName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  EnumNetwork(EnumNetworkProc, RESOURCE_GLOBALNET, RESOURCETYPE_DISK);
end;

end.
Delphi-Quellcode:
unit Unit2;

interface

uses Windows;

type
  TEnumNetworkProc = procedure(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean) of object;

procedure EnumNetwork(const aEnumNetworkProc :TEnumNetworkProc; const aScope :dword = RESOURCE_GLOBALNET; const aType :dword = RESOURCETYPE_ANY);

implementation

//Procédure récursive
procedure DoEnumNetwork(const aContainer :Pointer;
                        const aEnumNetworkProc :TEnumNetworkProc;
                        const aScope :dword;
                        const aType :dword;
                        const aLevel :byte);
type
  PNetResourceArray = ^TNetResourceArray;
  TNetResourceArray = array [0..0] of TNetResource;
var
  NetHandle :THandle;
  NetResources :PNetResourceArray;
  NetResult :dword;
  Size, Count, i :Cardinal;
  Continue :boolean;
begin
  Continue := TRUE;

  NetResult := WNetOpenEnum(aScope, aType, 0, aContainer, NetHandle);

  if NetResult = NO_ERROR then
  try
    //Taille de base
    Size := 50 *SizeOf(TNetResource);
    GetMem(NetResources, Size);

    try
      while Continue do
      begin
        Count := $FFFFFFFF;
        NetResult := WNetEnumResource(NetHandle, Count, NetResources, Size);

        //Taille insuffisante ?
        if NetResult = ERROR_MORE_DATA
        then ReallocMem(NetResources, Size)
        else Break;
      end;

      //Enumère
      if NetResult = NO_ERROR then
        for i := 0 to Count - 1 do
        begin
          //Callback
          if Assigned(aEnumNetworkProc) then
          begin
            aEnumNetworkProc(NetResources^[i], aLevel, Continue);
            if not Continue then Break;
          end;

          //Appel récursif
          if (NetResources^[i].dwUsage and RESOURCEUSAGE_CONTAINER) > 0 then
            DoEnumNetwork(@NetResources^[i], aEnumNetworkProc, aScope, aType, aLevel +1);
        end;
    finally
      FreeMem(NetResources, Size);
    end;
  finally
    WNetCloseEnum(NetHandle);
  end;
end;

procedure EnumNetwork(const aEnumNetworkProc: TEnumNetworkProc; const aScope, aType: dword);
begin
  DoEnumNetwork(nil, aEnumNetworkProc, aScope, aType, 0);
end;

end.
Hier hab ich es her.
Delphi-Quellcode:
{
  From the MS-DOS prompt, you can enumerate the network
  connections (drives) by using the following command:

  net use

  Programmatically, you would call WNetOpenEnum() to start
  the enumeration of connected resources and
  WNetEnumResources() to continue the enumeration.
  The following sample code enumerates the network connections:

  Vom MS-DOS Prompt können die Netzwerk Verbdindungen
  (Laufwerke) mittels "net use" angezeigt werden.
  Das folgende Beispiel braucht die WNetOpenEnum,
  WNetEnumResources Funktionen, um die Netzwerk Verbdindungen aufzulisten.
}



procedure TForm1.Button1Click(Sender: TObject);
var
  i, dwResult: DWORD;
  hEnum: THandle;
  lpnrDrv: PNETRESOURCE;
  s: string;
const
  cbBuffer: DWORD = 16384;
  cEntries: DWORD = $FFFFFFFF;
begin
  dwResult := WNetOpenEnum(RESOURCE_CONNECTED,
    RESOURCETYPE_ANY,
    0,
    nil,
    hEnum);

  if (dwResult <> NO_ERROR) then
  begin
    ShowMessage('Cannot enumerate network drives.');
    Exit;
  end;
  s := '';
  repeat
    lpnrDrv := PNETRESOURCE(GlobalAlloc(GPTR, cbBuffer));
    dwResult := WNetEnumResource(hEnum, cEntries, lpnrDrv, cbBuffer);
    if (dwResult = NO_ERROR) then
    begin
      s := 'Network drives:'#13#10;
      for i := 0 to cEntries - 1 do
      begin
        if lpnrDrv^.lpLocalName <> nil then
          s := s + lpnrDrv^.lpLocalName + #9 + lpnrDrv^.lpRemoteName;
        Inc(lpnrDrv);
      end;
    end
    else if dwResult <> ERROR_NO_MORE_ITEMS then
    begin
      s := s + 'Cannot complete network drive enumeration';
      GlobalFree(HGLOBAL(lpnrDrv));
      break;
    end;
    GlobalFree(HGLOBAL(lpnrDrv));
  until (dwResult = ERROR_NO_MORE_ITEMS);
  WNetCloseEnum(hEnum);
  if s = 'then s := 'No network connections.';
  ShowMessage(s);
end;


{***********************************************************************
FindComp Unit from
Fatih Olcer
fatiholcer@altavista.com
***********************************************************************}

unit FindComp;

interface

uses
  Windows, Classes;

function FindComputers: DWORD;

var
  Computers: TStringList;

implementation

uses
  SysUtils;

const
  MaxEntries = 250;

function FindComputers: DWORD;
var
  EnumWorkGroupHandle, EnumComputerHandle: THandle;
  EnumError: DWORD;
  Network: TNetResource;
  WorkGroupEntries, ComputerEntries: DWORD;
  EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
  EnumBufferLength: DWORD;
  I, J: DWORD;
begin
  Computers.Clear;

  FillChar(Network, SizeOf(Network), 0);
  with Network do
  begin
    dwScope := RESOURCE_GLOBALNET;
    dwType := RESOURCETYPE_ANY;
    dwUsage := RESOURCEUSAGE_CONTAINER;
  end;

  EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
    @Network, EnumWorkGroupHandle);

  if EnumError = NO_ERROR then
  begin
    WorkGroupEntries := MaxEntries;
    EnumBufferLength := SizeOf(EnumWorkGroupBuffer);
    EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries,
      @EnumWorkGroupBuffer, EnumBufferLength);

    if EnumError = NO_ERROR then
    begin
      for I := 1 to WorkGroupEntries do
      begin
        EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
          @EnumWorkGroupBuffer[I], EnumComputerHandle);
        if EnumError = NO_ERROR then
        begin
          ComputerEntries := MaxEntries;
          EnumBufferLength := SizeOf(EnumComputerBuffer);
          EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries,
            @EnumComputerBuffer, EnumBufferLength);
          if EnumError = NO_ERROR then
            for J := 1 to ComputerEntries do
              Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName,
                3, Length(EnumComputerBuffer[J].lpRemoteName) - 2));
          WNetCloseEnum(EnumComputerHandle);
        end;
      end;
    end;
    WNetCloseEnum(EnumWorkGroupHandle);
  end;

  if EnumError = ERROR_NO_MORE_ITEMS then
    EnumError := NO_ERROR;
  Result := EnumError;
end;

initialization

  Computers := TStringList.Create;

finalization

  Computers.Free;
end.
Hier hab ich es her.

Ich hoffe das irgend etwas davon helfen kann.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat