Obtain network resource information in the delhpi Program

Source: Internet
Author: User

Obtain network resource information in the delhpi Program
Wang fa-jun

Making full use of shared resources in the LAN will undoubtedly reduce users' hardware and software investment. In our programs, we often need to list shared resources in the local area network, for example, you may need to select the name of the server where the upper-layer application is located, select an available network printer in the print module, and select shared files from other machines in the application..

Taking a three-tier Database Application of Midas as an example, we can use Delphi to create a client application and a middle-tier business processing program to access data from the middle-tier business processing program to the database, then, report the result to the client. When the client application and intermediate application and Database Service Program are not on one machine, you must specify the name of the server where the intermediate application and Database Service Program are located.

In the Delphi Program, we can use the win api to list resources such as server names, user computer names, user shared directories and files, and shared printers in the LAN. DLL.

The following uses the peer network of Win9x as an example to list resources such as the working group name, user computer name, shared directory and file, and shared printer in "Network Neighbor.

1. List the names of working groups in "Network neighbors"

In "Network neighbors", open "entire network" to view the names of all working groups in the network. In the Delphi program, you can use the API functions wnetopenenum and wnetenumresource to list the workgroup names in "Network neighbors.

The following describes the two API functions. For details, see the Delphi help file.

// Wnetopenenum is used to obtain a network resource handle and serves as the basis for wnetenumresource to list network resources. Its function prototype is as follows:

Function wnetopenenum (

Dwscope, // network range, resource_connected (connected machines), resource_globalnet (all machines ),

Resource_remembered (memory machine)

Dwtype, // resource type, resourcetype_any (all resources), resourcetype_disk (file resources ),

Resourcetype_print (print resources)

Dwusage // resource usage, 0 (all resources), resourceusage_connectable (all accessible resources ),

Resourceusage_container (all container resources) is valid only when resource_globalnet is set to dwscope: DWORD;

Lpnetresource: pnetresource; // netresource type pointer. Its memory indicates the level of network resources.

Indicates the top layer. It is valid only when dwscope is resource_globalnet. Otherwise, it should be nil.

VaR lphenum: thandle // returns the network resource handle, which is the basis for wnetenumresource to further list network resources.

): DWORD; stdcall; Return Value no_error indicates that the execution is successful; otherwise, the getlasterror application obtains the error message.

// Wnetenumresource is used to list available resources in the current network, including file resources and print resources. Its function prototype is as follows:

Function wnetenumresource (henum: thandle; // wnetenumresource further lists the basis for network resources. Generally, wnetopenenum obtains var lpccount: DWORD; // The number of resources to be obtained, $ ffffffff indicates that all resources are listed. The returned value is the actual number of resources lpbuffer: pointer; // the pointer to the buffer for receiving results, which is generally a netresource array var lpbuffersize: DWORD // the buffer size (byte) of the received result): DWORD; stdcall; Return Value: no_error (Execution successful) or error_no_more_items (resource list completed); otherwise, getlasterror is used to obtain error information.

  

To list information about a workgroup in a network, first obtain the network type in the network, and then obtain the name of the workgroup contained in each network type.

1. Obtain network type information

A lan can be composed of multiple types of networks, such as Microsoft and Novell. Use API

The wnetopenenum and wnetenumresource functions can obtain network type information.

(1) Use the wnetopenenum function to return a handle as the basis for wnetenumresource to list network resources. Our goals

The name of the working group listing "entire network". Therefore, the network scope is resource_globalnet (all machines) and resource class.

Type: resourcetype_disk (File resource), resource usage: resourceusage_container (all container resources ),

The network level is the highest level (nil). In this way, you can obtain a handle to further list network resources. The specific procedure section is as follows:

{Res: DWORD; lphenum: thandle ;}

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, nil, lphenum );

If res <> no_error then exit; // execution failed

(2) You can use the wnetenumresource API function to obtain network type information. In this case, you need to use the handle lphenum obtained above,

You can obtain an array of the netresource type. Each netresource element is a network type information.

For more information, see the following section.

Type

Tnetresourcearray = ^ tnetresource; // network type array
VaR

Netresource: tnetresource;

Buf: pointer;

Count, bufsize, Res: DWORD;

Lphenum: thandle;

P: tnetresourcearray;

I, J: smallint;

Networktypelist: tlist; begin

......

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize); // obtain network type information

If (RES = error_no_more_items) // The resource list is complete.

Or (RES <> no_error) // execution failed

Then exit;

P: = tnetresourcearray (BUF );

For I: = 0 to count-1 do // records information of each network type

Begin

Networktypelist. Add (P );

INC (P );

End;

... End;

2. Obtain the workgroup Information

After obtaining the network type information, you can obtain the server (workgroup) information in the network type based on this information.

In the wnetopenenum function, you can specify the lpnetresource parameter as a network type information to obtain a handle. In wnetenumresource, you can use this handle to obtain an array of the netresource type, the lpremotename section of each netresource element is the name of a working group (for example, "myworkgroup ").

For more information, see the following section.

// List all workgroup names in a network type

Netresource: = tnetresource (networktypelist. items [J] ^); // network type information // obtains the handle of a file resource of a network type. netresource indicates the network type information, and lphenum indicates the return handle.

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, @ netresource, lphenum );

If res <> no_error then break; // execution failed

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

Begin

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

// Obtain the file resource information of a network type,

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize );

If (RES = error_no_more_items) // The resource list is complete.

Or (RES <> no_error) // execution failed

Then break;

P: = tnetresourcearray (BUF );

For I: = 0 to count-1 do // list information of each working group

Begin

List. Add (strpas (P ^. lpremotename); // obtain the name of a workgroup.

INC (P );

End;

End;

3. Obtain the full source code of the workgroup information. // list the names of the workgroup in the network. If the returned value is true, the execution is successful,

// Return the name of the server (workgroup) in the 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; // obtain the file resource handle of the entire network. lphenum is the return name handle.

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, nil, lphenum );

If res <> no_error then exit; // raise exception (RES); // execution failed // obtain the network type information of the entire network

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize );

If (RES = error_no_more_items) // The resource list is complete.

Or (RES <> no_error) // execution failed

Then exit;

P: = tnetresourcearray (BUF );

For I: = 0 to count-1 do // records information of each network type

Begin

Networktypelist. Add (P );

INC (P );

End;

// Wnetcloseenum closes an enumerative handle

Res: = wnetcloseenum (lphenum); // close the listing once.

If res <> no_error then exit;

For J: = 0 to networktypelist. Count-1 do // list all workgroup names in each network type

Begin // list all workgroup names in a network type

Netresource: = tnetresource (networktypelist. items [J] ^); // network type information // gets the handle of a file resource of a network type. netresource is the network type information, and lphenum is the return name handle.

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, @ netresource, lphenum );

If res <> no_error then break; // execution failed

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

Begin

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

// Obtain the file resource information of a network type,

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize );

If (RES = error_no_more_items) // The resource list is complete.

Or (RES <> no_error) // execution failed

Then break;

P: = tnetresourcearray (BUF );

For I: = 0 to count-1 do // list information of each working group

Begin

List. Add (strpas (P ^. lpremotename); // obtain the name of a workgroup.

INC (P );

End;

End;

Res: = wnetcloseenum (lphenum); // close the listing once.

If res <> no_error then break; // execution failed

End;

Result: = true;

Freemem (BUF );

Networktypelist. Destroy;
End;

2. List the names of computers in a working group

In the wnetopenenum function, specify the lpremotename part of the lpnetresource parameter as the name of a working group (for example, "myworkgroup") to obtain a handle that can be used in wnetenumresource, you can obtain an array of the netresource type. The lpremotename part of each netresource element is a computer name (for example, "// wangfajun ").

For more information, see the following section.

Netresource. lpremotename: = @ groupname [1]; // specify the workgroup name

Netresource. dwdisplaytype: = resourcedisplaytype_server; // The display type is server (workgroup)

Netresource. dwusage: = resourceusage_container;

Netresource. dwscope: = resourcetype_disk; // lists file resource information.

// Obtain the network resource handle of the specified workgroup

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, @ netresource, lphenum );

The full program source code for obtaining the computer name is as follows:

// Name of the computer in the groupname of the specified working group. If the returned value is true, the execution is successful. // The function getusers (groupname: string; var list: tstringlist) is returned in the parameter list ): boolean; var

Netresource: tnetresource;

Buf: pointer;

Count, bufsize, Res: DWORD;

IND: integer;

Lphenum: thandle;

Temp: tnetresourcearray;
Begin

Result: = false;

List. Clear;

Fillchar (netresource, sizeof (netresource), 0); // initialize network level information

Netresource. lpremotename: = @ groupname [1]; // specify the workgroup name

Netresource. dwdisplaytype: = resourcedisplaytype_server; // type: Server (workgroup)

Netresource. dwusage: = resourceusage_container;

Netresource. dwscope: = resourcetype_disk; // lists file resource information.

// Obtain the network resource handle of the specified workgroup

Res: = wnetopenenum (resource_globalnet, resourcetype_disk,

Resourceusage_container, @ netresource, lphenum );

If res <> no_error then exit; // execution failed

While true do // list network resources of a specified workgroup

Begin

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

// Obtain the computer name

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize );

If res = error_no_more_items then break; // The resource list is complete.

If (RES <> no_error) Then exit; // execution failed

Temp: = tnetresourcearray (BUF );

For ind: = 0 to count-1 do // lists the names of the workgroup computers

Begin

// Obtain the computer name of the Working Group. + 2 indicates deleting "//", for example, // wangfajun => wangfajun

List. Add (temp ^. lpremotename + 2 );

INC (temp );

End;

End;

Res: = wnetcloseenum (lphenum); // close the listing once.

If res <> no_error then exit; // execution failed

Result: = true;

Freemem (BUF );
End;

3. List shared resources in a computer

In the wnetopenenum function, specify the lpremotename part of the lpnetresource parameter as the name of a computer (for example, "// wangfajun") to obtain a handle and use it in wnetenumresource, you can obtain an array of the netresource type. The lpremotename part of each netresource element is the name of the shared resource in the computer (such as the shared directory and file name, and the shared printer name, for example, "// wangfajun/Shared File ").

For more information, see the following section.

Netresource. lpremotename: = @ username [1]; // specify the computer name

// Obtain the network resource handle of the specified computer

Res: = wnetopenenum (resource_globalnet, resourcetype_any,

Resourceusage_connectable, @ netresource, lphenum );

The full program source code for obtaining the computer name is as follows:

// List the name of the shared resource in the specified username. If the returned value is true, the execution is successful. // The Shared Resource Name function getuserresource (username: string; var list: tstringlist) is returned in the parameter list): Boolean; var

Netresource: tnetresource;

Buf: pointer;

Count, bufsize, Res: DWORD;

IND: integer;

Lphenum: thandle;

Temp: tnetresourcearray;
Begin

Result: = false;

List. Clear;

Fillchar (netresource, sizeof (netresource), 0); // initialize network level information

Netresource. lpremotename: = @ username [1]; // specify the computer name

// Obtain 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 // list network resources of a specified workgroup

Begin

Count: = $ ffffffff; // unlimited number of resources

Bufsize: = 8192; // set the buffer size to 8 K.

Getmem (BUF, bufsize); // apply for memory, used to obtain the workgroup Information

// Obtain the Network Resource Name of the specified computer

Res: = wnetenumresource (lphenum, Count, pointer (BUF), bufsize );

If res = error_no_more_items then break; // The resource list is complete.

If (RES <> no_error) Then exit; // execution failed

Temp: = tnetresourcearray (BUF );

For ind: = 0 to count-1 do

Begin

// Obtain the name of the shared resource on the specified computer. + 2 indicates deleting "//",

// For example, // wangfajun => wangfajun

List. Add (temp ^. lpremotename + 2 );

INC (temp );

End;

End;

Res: = wnetcloseenum (lphenum); // close the listing once.

If res <> no_error then exit; // execution failed

Result: = true;

Freemem (BUF );
End;

The above program is successfully debugged under pwin98 + Delphi3.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.