P>Everyone is usingDelphiCompile a network applicationProgramIn a network environment consisting of Windows NT and Novell Netware, applications need to use some data on Novell, if you manually map to Windows resource manager every time, disconnecting the network drive is not complicated but not professional enough. This article describesDelphiTo solve this problem.
I. Basic Principles
In Microsoft Windows, applications use Windows network functions (wnet) to implement network functions without worrying about network vendors and specific implementations. Because wnet functions are independent of the network.
Wnet functions mainly include: wnetaddconnection,Wnetaddconnection2, Wnetaddconnection3, wnetcancelconnection2, etc. This article mainly usesWnetaddconnection2, Wnetcancelconnection2 function. The following is a brief introduction. For more information about wnet functions, seeDelphiAnd Microsoft API functions.
Wnetaddconnection2The function prototype in windows. PAS is as follows:
Function Wnetaddconnection2 (VAR lpnetresource: tnetresource; lppassword, lpusername: pchar; dwflags: DWORD): DWORD; stdcall; to call this function, you must enter the lpnetresouce structure, which is defined: typedef struct _ netresouce {DWORD dwscope; DWORD dwtype; DWORD dwdisplaytype; DWORD dwusage; lptstr lplocalname; lptsr identifier; lptsr lpprovider;} netresource; dwtype, lplocalname, identifier, lpprovider parameters. Dwtype: Specifies the resource type of the network. The options include resourcetype_any, resourcetype_disk, and resourcetype_print ). Lplocalname: Specifies the local device. Lpremotename: Specifies the remote network name. Lpprovider: the supplier that provides network resources. If it is null, the supplier is unknown. Wnetaddconnection2 The lppassword of the function is the password for a remote resource. Lpusername is the username of the Remote resource. The dwflags flag is used to specify whether to reconnect when logging on (0 indicates that the connection is not reconnected, and cconnect_update_profile indicates that the connection is reconnected when logging on ). Wnetcancelconnection2 in windows. the function prototype in PAS is as follows: function wnetcancelconnection2 (lpname: pchar; dwflags: DWORD; Fforce: bool): DWORD; stdcall; lpname: should I open the ticket and try again? Dwflags: Same as above. Fforce: True indicates that the network drive is forcibly disconnected regardless of whether a file is opened. False indicates that the function fails to run if a file is opened.
Ii. Instances
InDelphiUnder the File menu, select new application, place a button on form1, and type the following in the button1 click event:Code:
Procedure tform1.button1click (Sender: tobject); var netsource: tnetresource; begin with netsource do begin dwtype: = resourcetype_any; lplocalname: = x:; // map remote resources to this drive lpremotename: ==\ hqserver \ sys; // The remote network resource lpprovider :=; // a value must be assigned. If it is null, the value of lpremotename is used. End;Wnetaddconnection2(Netsource, password, guest, // the user name is guest, and the password is passwordconnect_update_profile); // re-connect upon next login // you can see the network drive X in Windows resource manager at this time: if messagedlg (Are you sure to disconnect drive ?, Mtconfirmation, [mbyes, mbno], 0) = mryes then // disconnect the network drive X: wnetcancelconnection2 (X:, connect_update_profile, true); end;
This program inDelphi3.0, 4.0, pwindows95, and Novell Netware 3.12 are compiled and run successfully.
Example 2,
Current,DelphiNetwork is the development trend of computers in the future. This article introducesDelphiThe source code is as follows:
Map-network-drive.pas
Procedure tform1.button1click (Sender: tobject );
VaR
Nr: tnetresource;
Begin
With NR do
Begin
Dwtype: = resourcetype _ any;
Lplocalname: = 'x: '; // map to this driver letter
Lpremotename: = '// myserver \ mydirectory ';
// Must be filled in.
Lpprovider: = ";
End;
Wnetaddconnection2(NR, 'mypassword', 'myuername ',
Connect _ update _ profile );
End;