Как работать с поименованными каналами под Win95-NT в сети

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Реальность из дистрибутива Windows ME — Удаленный доступ к домашней сети.

сервер:

StrPCopy(buff, Edit1.Text);

fPipeHandle := CreateNamedPipe(buff,

Pipe_Access_Duplex or File_Flag_Overlapped,

Pipe_Type_Message or Pipe_ReadMode_Byte or Pipe_Wait,

5, $400, $400, 235, nil);

клиент:

StrPCopy(buff,Edit1.Text);

fFileHandle:=CreateFile(buff,

Generic_Read or Generic_Write,

File_Share_Read or File_Share_Write,

nil,

Open_Existing,

File_Attribute_Normal or File_Flag_Overlapped or Security_Anonymous,

0);

if fFileHandle Invalid_Handle_Value then

begin

{/codecitation}

Как определить — подключен ли компьютер к сети

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Летит компьютеp с 9-го этажа, и дyмает: «Вот бы щас зависнyть…»

Воспользуемся функцией GetSystemMetrics(), переслав ей флаг SM_NETWORK:

procedure TForm1.Button2Click(Sender: TObject);

begin

if GetSystemMetrics(SM_NETWORK) and $01 = $01 then

ShowMessage(‘Machine is attached to network’)

else

ShowMessage(‘Machine is not attached to network’);

end;

{/codecitation}

Как найти все комьютеры в сети 4

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Автор: http://delphi.mastak.ru

var

Computer: array[1..500] of string[25];

ComputerCount: Integer;

procedure FindAllComputers(Workgroup: string);

var

EnumHandle: THandle;

WorkgroupRS: TNetResource;

Buf: array[1..500] of TNetResource;

BufSize: Integer;

Entries: Integer;

Result: Integer;

begin

ComputerCount := 0;

Workgroup := Workgroup #0;

FillChar(WorkgroupRS, SizeOf(WorkgroupRS), 0);

with WorkgroupRS do

begin

dwScope := 2;

dwType := 3;

dwDisplayType := 1;

dwUsage := 2;

lpRemoteName := @Workgroup[1];

end;

WNetOpenEnum(RESOURCE_GLOBALNET,

RESOURCETYPE_ANY,

0,

@WorkgroupRS,

EnumHandle);

repeat

Entries := 1;

BufSize := SizeOf(Buf);

Result :=

WNetEnumResource(EnumHandle,

Entries,

@Buf,

BufSize);

if (Result = NO_ERROR) and (Entries = 1) then

begin

Inc(ComputerCount);

Computer[ComputerCount] := StrPas(Buf[1].lpRemoteName);

end;

until (Entries 1) or (Result NO_ERROR);

WNetCloseEnum(EnumHandle);

end; { Find All Computers }

{/codecitation}

Как найти все комьютеры в сети 3

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Автор: SmaLL

WEB-сайт: http://forum.vingrad.ru

function EnumerateFunc(hwnd: HWND; hdc: HDC; lpnr: PNetResource): Boolean;

const

cbBuffer: DWORD = 16384; // 16K is a good size

var

hEnum, dwResult, dwResultEnum: DWORD;

lpnrLocal: array

[0..16384 div SizeOf(TNetResource)] of TNetResource;

// pointer to enumerated structures

i: integer;

cEntries: Longint;

begin

centries := -1; // enumerate all possible entries

// Call the WNetOpenEnum function to begin the enumeration.

dwResult := WNetOpenEnum(

RESOURCE_CONTEXT, // Enumerate currently connected resources.

RESOURCETYPE_DISK, // all resources

0, // enumerate all resources

lpnr, // NULL first time the function is called

hEnum // handle to the resource

);

if (dwResult NO_ERROR) then

begin

// Process errors with an application-defined error handler

Result := False;

Exit;

end;

// Initialize the buffer.

FillChar(lpnrLocal, cbBuffer, 0);

// Call the WNetEnumResource function to continue

// the enumeration.

dwResultEnum := WNetEnumResource(hEnum, // resource handle

DWORD(cEntries), // defined locally as -1

@lpnrLocal, // LPNETRESOURCE

cbBuffer); // buffer size

// This is just printing

for i := 0 to cEntries — 1 do

begin

// loop through each structure and

// get remote name of resource… lpnrLocal[i].lpRemoteName)

end;

// Call WNetCloseEnum to end the enumeration.

dwResult := WNetCloseEnum(hEnum);

if (dwResult NO_ERROR) then

begin

// Process errors… some user defined function here

Result := False;

end

else

Result := True;

end;

{/codecitation}

Как найти все комьютеры в сети 2

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Автор: Pegas

WEB-сайт: http://forum.vingrad.ru

function TNetForm.FillNetLevel(xxx: PNetResource; List: TListItems): Word;

type

PNRArr = ^TNRArr;

TNRArr = array[0..59] of TNetResource;

var

x: PNRArr;

tnr: TNetResource;

I: integer;

EntrReq,

SizeReq,

twx: THandle;

WSName: string;

LI: TListItem;

begin

Result := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,

RESOURCEUSAGE_CONTAINER, xxx, twx);

if Result = ERROR_NO_NETWORK then

Exit;

if Result = NO_ERROR then

begin

New(x);

EntrReq := 1;

SizeReq := SizeOf(TNetResource) * 59;

while (twx 0) and

(WNetEnumResource(twx, EntrReq, x, SizeReq) ERROR_NO_MORE_ITEMS) do

begin

for i := 0 to EntrReq — 1 do

begin

Move(x^[i], tnr, SizeOf(tnr));

case tnr.dwDisplayType of

RESOURCEDISPLAYTYPE_SERVER:

begin

if tnr.lpRemoteName » then

WSName := tnr.lpRemoteName

else

WSName := tnr.lpComment;

LI := list.Add;

LI.Caption := copy(WSName, 3, length(WSName) — 2);

//list.Add(WSName);

end;

else

FillNetLevel(@tnr, list);

end;

end;

end;

Dispose(x);

WNetCloseEnum(twx);

end;

end;

Пример вызова:

FillNetLevel(nil, ListView1.Items);

{/codecitation}

Как найти все комьютеры в сети

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Автор: http://forum.vingrad.ru

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.

{/codecitation}

Как найти все компьютеры в рабочей группе

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Я пришел к тебе с дискетой — рассказать, что сеть упала.

var

Computer: array [1..500] of string[25];

ComputerCount: Integer;

procedure FindAllComputers(Workgroup: string);

var

EnumHandle: THandle;

WorkgroupRS: TNetResource;

Buf: array [1..500] of TNetResource;

BufSize: Integer;

Entries: Integer;

Result: Integer;

begin

ComputerCount := 0;

Workgroup := Workgroup #0;

FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);

with WorkgroupRS do

begin

dwScope := 2;

dwType := 3;

dwDisplayType := 1;

dwUsage := 2;

lpRemoteName := @Workgroup[1];

end;

WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @WorkgroupRS, EnumHandle);

repeat

Entries := 1;

BufSize := SizeOf(Buf);

Result := WNetEnumResource(EnumHandle, Entries, @Buf, BufSize);

if (Result = NO_ERROR) and (Entries = 1) then

begin

Inc( ComputerCount );

Computer[ ComputerCount ] := StrPas(Buf[1].lpRemoteName);

end;

until

(Entries 1) or (Result NO_ERROR);

WNetCloseEnum( EnumHandle );

end;

{/codecitation}

Как можно проверить, запущена программа с локального диска или с сетевого окружения, то есть с сети

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Издевательство над сис.админом: «Заходи, гостем будешь…»

var

DriveType: TDriveType;

DriveChar: string;

begin

DriveChar:=ExtractFileDrive(ParamStr(0));

DriveType:=TDriveType(GetDriveType(PChar(DriveChar ‘\’)));

case driveType of

dtFixed: ShowMessage(‘Fixed ‘ DriveChar ‘\’);

dtNetWork: ShowMessage(‘NetWork ‘ DriveChar ‘\’);

end;

end;

{/codecitation}

Изменить пароль пользователя для указанного сервера и домена

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Оформил: DeeCo

Автор: http://www.swissdelphicenter.ch

function NetUserChangePassword(Domain: PWideChar; UserName: PWideChar; OldPassword: PWideChar;

NewPassword: PWideChar): Longint; stdcall; external ‘netapi32.dll’

Name ‘NetUserChangePassword’;

// Changes a user’s password for a specified network server or domain.

// Requirements: Windows NT/2000/XP

// Windows 95/98/Me: You can use the PwdChangePassword function to change a user’s

// Windows logon password on these platforms

procedure TForm1.Button1Click(Sender: TObject);

begin

NetUserChangePassword(PWideChar(WideString(‘\\COMPUTER’)),

PWideChar(WideString(‘username’)),

PWideChar(WideString(‘oldpass’)),

PWideChar(WideString(‘newpass’)));

end;

{/codecitation}

Извлечь адреса сетевых карт

{codecitation class=»brush: pascal; gutter: false;» width=»600px»}

Оформил: DeeCo

Автор: http://www.swissdelphicenter.ch

{for NT4 and Windows 2000, and you must have Admin privileges}

{fьr NT4 und Windows 2000, und Adminrechte erforderlich}

uses

Registry;

const

nString = ‘SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\NetworkCards’;

nEthernet = ‘Ethernet’;

nEtherjet = ‘Etherjet’; {if Hardware IBM PL300 with Chip 10/100}

nTcpIp1 = ‘SYSTEM\CurrentControlSet\Services\’;

nTcpIp2 = ‘\Parameters\Tcpip’;

var

reg: TRegistry;

buffer1, buffer2, buffer3: array [1..32] of Char;

i: Integer;

ipaddress, subnetmask, DefaultGateway: string;

Adapter, Adapter_Key: string;

stringlist: TStrings;

function search_adapter_key: string;

var

astring, description, st: string;

nPos: integer;

ServiceName: string;

begin

reg := TRegistry.Create;

stringlist := TStringList.Create;

reg.RootKey := HKEY_LOCAL_MACHINE;

reg.OpenKey(nString, False);

reg.GetKeyNames(stringlist); {search all subkeys

such alle unterschlьssel}

reg.CloseKey;

for i := 0 to (stringlist.Count — 1) do

begin

st := stringlist[i];

aString := nString ‘\’ st;

reg := TRegistry.Create;

reg.RootKey := HKEY_LOCAL_MACHINE;

reg.OpenKey(aString, False);

description := reg.ReadString(‘Description’);

nPos:= AnsiPos(nEthernet, description); {search description for string Ethernet

Beschreibung nach dem string Ethernet absuchen}

if nPos > 0 then

begin

ServiceName := reg.ReadString(‘ServiceName’);

Adapter_Key := nTcpIp1 ServiceName nTcpIp2;

end;

nPos := AnsiPos(nEtherjet, description); {search description for string ‘Etherjet

if IBM PL300 with MotherboardChip}

if nPos > 0 then

begin

ServiceName := reg.ReadString(‘ServiceName’);

Adapter_Key := nTcpIp1 ServiceName nTcpIp2;

end;

reg.CloseKey;

end;

Result := Adapter_Key;

end;

procedure search_for_adapter_NT;

var

BufSize: Integer; {Bufsize requested but not used

Bufsize erforderlich aber nicht benutzt}

begin

adapter := search_adapter_key;

if adapter » then

begin

Reg := TRegistry.Create;

try

Reg.RootKey := HKEY_LOCAL_MACHINE;

Reg.OpenKey(adapter, False);

Reg.ReadBinaryData(‘IpAddress’, buffer1, BufSize);

finally

Reg.CloseKey;

Reg.Free;

end;

Reg := TRegistry.Create;

try

Reg.RootKey := HKEY_LOCAL_MACHINE;

Reg.OpenKey(adapter, False);

Reg.ReadBinaryData(‘SubnetMask’, buffer2, BufSize);

finally

Reg.CloseKey;

Reg.Free;

end;

Reg := TRegistry.Create;

try

Reg.RootKey := HKEY_LOCAL_MACHINE;

Reg.OpenKey(adapter, False);

Reg.ReadBinaryData(‘DefaultGateway’, buffer3, BufSize);

finally

Reg.CloseKey;

Reg.Free;

end;

end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

Memo1.Clear;

search_for_adapter_NT;

Memo1.Lines.Add(buffer1);

Memo1.Lines.Add(buffer2);

Memo1.Lines.Add(buffer3);

end;

{/codecitation}