Get network resource information in the DELHPI program

zhaozj2021-02-11  216

Get network resource information Wang Haolun in the DELHPI program

Take advantage of shared resources in the LAN will undoubtedly reduce user hardware and software investment. In our program, you often need to list shared resources in a local area network. For example, in the MIDAS multi-layer application, you may want to select the name of the server in the upper layer application, select the available network printer in the print module, in the application Select a shared file on other machines.

With a MIDAS three-layer database application as an example, we can use Delphi to make a client application and an intermediate business handler, which is filed by the intermediate layer service handler to the data, and then feedback to the client. When the client application and the intermediate layer program and the database service program are not on a machine, you need to specify the name of the server in the intermediate layer program and the database service program.

In the Delphi program, we can list the server names in the local area network through the Win API, the user computer name, user sharing directory, and files, shared printers, etc., these APIs are file MPR. DLL is available.

The following is an example in Win9X's peer-to-peer network, enumerating the Working Group name, user computer name, shared directory, and files, sharing printers such as "Network Neighbors".

First, list the workgroup name in "Network Neighbors"

In "Network Neighbors", open "Overall Network", you can see the name of all working groups in the network. In the Delphi program, you can use the API functions WNETOpenENUM and WNETENUMRESOURCE to list the workgroup name in "Network Neighbors".

Below is an introduction to the two API functions, please refer to the Delphi Help file.

// WNETOPENUM is used to obtain a network resource handle, which is the basis for the WNETENUMRESOURCE to list network resources, and its function prototype is as follows:

Function WnetopENENUM

DWSCOPE, // network range, resource_connected, resource_globalnet (all machines),

Resource_remembed (Memory Machine)

DWTYPE, // Resource Type, ResourceType_any (All Resources), ResourceType_Disk (File Resources),

Resourcetype_print (Printing Resources)

Dwusage // Resource Uses, 0 (All Resources), ResourceUsage_Connectable (all accessible resources),

Resourceusage_Container (all container resources) are only valid for resource_globalnet: DWORD;

LpnetResource: PNetResource; // NetResource type pointer, its memory represents the hierarchy of network resources, when nil

Indicates the uppermost layer, only when DWSCOPE is resource_globalnet, it should be NIL when other cases

Var lphenum: THANDLE / / Returns the network resource handle, which is the basis for the WNETENUMRESOURCE to enumerate the network resources): dword; stdcall; return value no_error indicates the execution, otherwise apply getLastError to get an error message

// WseTenumResource is used to list resources available in the current network, including file resources and print resources, which are as follows:

Function WneetenumResource (HENUM: THANDLE; / / WNETENUMRESOURCE further lists the basis of network resources, usually gets var lpcount: dword from WNEToPENENUM; // To get the number of resources, $ ffffff means listing all resources, returning value for actual resources LPBuffer: Pointer; // Receive the pointer of the buffer of the result, generally the number of NetResource types VAR lpbuffersize: DWORD // Buffer size (byte): dword; stdcall; return value is no_error (execution success) or error_no_more_items The resource list is complete), otherwise apply the getLastError to get the error message.

To list the workgroup information in the network, first get the network type in the network, then get the name of the workgroup included in each type of network.

1, get network type information

The local area network can be composed of a variety of types of networks, such as a network of Microsoft, Novell, and the like. Use API

Functions WNETOpenENUM and WNETENUMRESOURCE can get network type information.

(1) Returns a handle with the WNEToPENENUM function, which is the basis for the WNETENUMRESOURCE lists network resources. Our eye

It is the name of the working group of "The entire network", so the network range is Resource_GlobalNet (all machines), resource classes

RESOURTYPE_DISK (file resource), resource use is Resourceusage_Container (all Container resources),

The network level is the highest layer (NIL), which allows a handle to further enumerate network resources. The specific program segment is as follows:

{RES: DWORD; lphenum: thandle;

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_Container, NIL, LPHENUM;

If res <> no_error thr; // execution failed

(2) Use the API function WNETENUMRESOURCE to get network type information, at which point to use the handle LPhenum,

You can get an array of NetResource types, each NetResource element, is a network type information.

Please refer to the following blocks and comment information.

Type

TNetResourceArray = ^ TNetResource; // Network type array variable VAR

NetResource: TNETRESOURCE;

BUF: POINTER;

Count, BufSize, Res: DWORD

Lphenum: thandle;

P: TNetResourceArray;

I, J: smallint;

NetworkTypelist: TList; Begin

......

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (BUF, BUFSIZE); // Apply for memory for obtaining a working group information res: = WnertenumResource (lphenum, count, pointer (buf); // Get network type information

IF (res = error_no_more_items) // resource list

or (res <> no_error) // Execution failed

.

P: = TNetResourceArray (BUF);

For i: = 0 to count - 1 DO // Record information of each network type

Begin

NetworkTypelist.Add (p);

INC (P);

END;

... End;

2. Get working group information

After getting the network type information, you can get the server (working group) information in this type of network according to this information.

In the WNETOpenenum function, specify the parameter LpNetResource as a network type information, you can get a handle, use this handle in WneetenumResource, you can get a NetResource type array, each NetResource element's LPREMOTENAME section is a workgroup name ( Such as "MyWorkgroup").

Please refer to the following blocks and comment information.

/ / List all the working group names in a network type

NetResource: = TNetResource (NetWorktyPelist.Items [J] ^); // Network Type Information // Gets the handle of file resources for a network type, NetResource is network type information, lphenum is a return handle

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_container, @ netResource, lphenum;

If res <> no_error dam; // execution failed

While true do // lists information about all working groups of a network type

Begin

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (buf, bufsize); // Apply for memory to obtain workgroup information

// Get a file resource information of a network type,

Res: = WneetenumResource (Lphenum, Count, Pointer (BUF), BUFSIZE

IF (res = error_no_more_items) // resource list

or (res <> no_error) // Execution failed

Then Break;

P: = TNetResourceArray (BUF);

For i: = 0 to count - 1 do // list information from each working group

Begin

List.add (StrPas (p ^ .lpremotename); // get the name of a workgroup

INC (P);

END;

END;

3. Get the full source code of the workgroup information // list the name of the workgroup in the entire network, and the return value indicates that the execution is successful.

// Name of the server (Working Group) in // Parameter list Function GetServerList (VAR List: Tstringlist): boolean; var

NetResource: TNETRESOURCE;

BUF: POINTER;

Count, BufSize, Res: DWORD

Lphenum: thandle;

P: TNetResourceArray; I, J: smallint;

NetworkTypelist: TList; Begin

Result: = FALSE;

NetWorktyPelist: = TList.create;

List.clear; // Get the handle of the file resources in the entire network, lphenum is the return handle

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_Container, NIL, LPHENUM;

If res <> no_error life exit; // raise exception (res); // Execute failed // Get network type information in the entire network

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (buf, bufsize); // Apply for memory to obtain workgroup information

Res: = WneetenumResource (Lphenum, Count, Pointer (BUF), BUFSIZE

IF (res = error_no_more_items) // resource list

or (res <> no_error) // Execution failed

.

P: = TNetResourceArray (BUF);

For i: = 0 to count - 1 DO // Record information of each network type

Begin

NetworkTypelist.Add (p);

INC (P);

END;

// WNETCLOSEENUM Turn off a hook

Res: = WnetCloseenum (lphenum); // Close a list

IF RES <> NO_ERROR THEN EXIT;

For j: = 0 to networktypelist.count-1 do // list all the workgroup names in each network type

Begin // lists all the working group names in a network type

NetResource: = TNetResource (NetWorktyPelist.Items [j] ^); // Network Type Information // Gets the handle of file resources for a network type, NetResource is network type information, lphenum is the return handle

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_container, @ netResource, lphenum;

If res <> no_error dam; // execution failed

While true do // lists information about all working groups of a network type

Begin

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (buf, bufsize); // Apply for memory to obtain workgroup information

// Get a file resource information of a network type,

Res: = WneetenumResource (Lphenum, Count, Pointer (BUF), BUFSIZE

IF (res = error_no_more_items) // resource list

or (res <> no_error) // Execution failed

Then Break;

P: = TNetResourceArray (BUF);

For i: = 0 to count - 1 do // list information from each working group

Begin

List.add (StrPas (p ^ .lpremotename); // get the name of a workgroup

INC (P);

END;

END;

Res: = WnetCloseenum (lphenum); // Close a list

If res <> no_error dam; // execution failed

END;

RESULT: = TRUE

FreeMem (buf);

NetWorktyPelist.destroy;

Second, list the computer name in a working group

In the WNETOpenenum function, specify the LPREMOTENAME section in the parameter lpnetResource as a name (such as "MyWorkGroup"), you can get a handle, using this handle in WneetenumResource, you can get a NetResource type array, each NetResource element The LPREMOTENAME section is a computer name (such as "// wangfajun").

Please refer to the following blocks and comment information.

NetResource.lpremotename: = @Groupname [1]; // Specify the name of the workgroup

NetResource.dwdisplayType: = ResourceDisplayType_server; // Display type is server (working group)

NetResource.dwusage: = Resourceusage_Container;

NetResource.dwscope: = resourcetype_disk; // list file resource information

/ / Get the network resource handle of the specified workgroup

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_container, @ netResource, lphenum;

Get the full program source code for the computer name as follows:

/ / Lists the computer name in the specified Workgroup GroupName, and the return value indicates that the execution is successful, // Parameter list Return to the computer name Function GetUsers (Groupname: string; var list: tstringlist): boolean; var

NetResource: TNETRESOURCE;

BUF: POINTER;

Count, BufSize, Res: DWORD

Ind: inteer;

Lphenum: thandle;

Temp: TNetResourceArray; Begin

Result: = FALSE;

List.clear;

Fillchar (NetResource, Sizeof (NetResource), 0); // Initialization Network Hierarchy Information

NetResource.lpremotename: = @Groupname [1]; // Specify the name of the workgroup

NetResource.dwdisplayType: = ResourceDisplayType_server; // Type Server (Workgroup)

NetResource.dwusage: = Resourceusage_Container;

NetResource.dwscope: = resourcetype_disk; // list file resource information

/ / Get the network resource handle of the specified workgroup

Res: = WNETOPENUM (Resource_GlobalNet, ResourceType_Disk,

Resourceusage_container, @ netResource, lphenum;

If res <> no_error thr; // execution failed

While true do // lists the network resources of the designated workgroup

Begin

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (buf, bufsize); // Apply for memory to obtain workgroup information

// Get the computer name

Res: = WneetenumResource (Lphenum, Count, Pointer (BUF), BUFSIZE

If res = error_no_more_items the Break; // Resource list

IF (res <> no_error) THEN EXIT; / / Execution Failure

Temp: = TNetResourceArray (BUF);

For ind: = 0 to count - 1 do // List the computer name of the working group

Begin

/ / Get the computer name of the working group, 2 means delete "//", such as // wangfajun => wangfajun

List.add (temp ^ .lpremotename 2);

Inc (TEMP);

END;

END;

Res: = WnetCloseenum (lphenum); // Close a list

If res <> no_error thr; // execution failed

RESULT: = TRUE

FreeMem (buf);

Third, list sharing resources in a computer

In the WNEToPENENUM function, specify the LPREMOTENAME section in the parameter LpNetResource as a computer name (such as "// wangfajun"), you can get a handle, using this handle in WneetenumResource, you can get a NetResource type array, each NetResource The element's LPREMOTENAME section is a shared resource name in the computer (which can be shared directory and file name, shared printer name, etc., such as "// wangfajun / shared file").

Please refer to the following blocks and comment information.

NetResource.lpremotename: = @username [1]; // Specify the computer name

/ / Get the network resource handle of the specified computer

Res: = WNETOPENENUM (Resource_GlobalNet, ResourceType_any,

Resourceusage_Connectable, @ NetResource, Lphenum;

Get the full program source code for the computer name as follows:

/ / Lists the shared resource name in the specified computer username, and the return value indicates that the execution is successful, // Parameter list Returns Function GetUserResource (username: string; var list: tstringlist): boolean; var

NetResource: TNETRESOURCE;

BUF: POINTER;

Count, BufSize, Res: DWORD

Ind: inteer;

Lphenum: thandle;

Temp: TNetResourceArray; Begin

Result: = FALSE;

List.clear;

Fillchar (NetResource, Sizeof (NetResource), 0); // Initialization Network Hierarchy Information

NetResource.lpremotename: = @username [1]; // Specify the computer name

/ / Get the network resource handle of the specified computer

Res: = WNETOPENENUM (Resource_GlobalNet, ResourceType_any,

Resourceusage_connectable, @ netResource, lphenum); if res <> no_ERROR THEN EXIT; / / Execution failed

While true do // lists the network resources of the designated workgroup

Begin

Count: = $ fffffff; // Number of unlimited resources

BUFSIZE: = 8192; // Buffer size Set to 8K

GetMem (buf, bufsize); // Apply for memory to obtain workgroup information

/ / Get the network resource name of the specified computer

Res: = WneetenumResource (Lphenum, Count, Pointer (BUF), BUFSIZE

If res = error_no_more_items the Break; // Resource list

IF (res <> no_error) THEN EXIT; / / Execution Failure

Temp: = TNetResourceArray (BUF);

For ind: = 0 to count - 1 DO

Begin

/ / Get the name of the shared resource in the specified computer, 2 means delete "//",

// 如 // wangfajun => WANGFAJUN

List.add (temp ^ .lpremotename 2);

Inc (TEMP);

END;

END;

Res: = WnetCloseenum (lphenum); // Close a list

If res <> no_error thr; // execution failed

RESULT: = TRUE;

FreeMem (buf);

The above program is debugged under PWIN98 Delphi3.0.

转载请注明原文地址:https://www.9cbs.com/read-3961.html

New Post(0)