本实例演示如何映射和断开网络虚拟盘。
向窗体上添加两个TLabel组件、两个TEdit组件和两个TButton组件,设计完成的主界面如图1所示。
图1 主界面 |
procedure TfrmMain.btnConnectClick(Sender: TObject); var NetShare :NETRESOURCE; begin NetShare.dwType := RESOURCETYPE_DISK; NetShare.lpLocalName := PAnsiChar(edtLocal.Text); NetShare.lpRemoteName := PAnsiChar(edtRemote.Text); NetShare.lpProvider :=’’; WNetAddConnection2(NetShare, ’’, ’’, CONNECT_UPDATE_PROFILE); end; |
procedure TfrmMain.btnDisconnectClick(Sender: TObject); var strLocalName : PChar; begin strLocalName:=PAnsiChar(edtLocal.Text); WNetCancelConnection2(strLocalName,CONNECT_UPDATE_PROFILE,False); end; |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TfrmMain = class(TForm) btnConnect: TButton; btnDisconnect: TButton; edtLocal: TEdit; Label1: TLabel; Label2: TLabel; edtRemote: TEdit; procedure btnConnectClick(Sender: TObject); procedure btnDisconnectClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmMain: TfrmMain; implementation {$R *.dfm} procedure TfrmMain.btnConnectClick(Sender: TObject); var NetShare :NETRESOURCE; begin NetShare.dwType := RESOURCETYPE_DISK; NetShare.lpLocalName := PAnsiChar(edtLocal.Text); NetShare.lpRemoteName := PAnsiChar(edtRemote.Text); NetShare.lpProvider :=’’; WNetAddConnection2(NetShare, ’’, ’’, CONNECT_UPDATE_PROFILE); end; procedure TfrmMain.btnDisconnectClick(Sender: TObject); var strLocalName : PChar; begin strLocalName:=PAnsiChar(edtLocal.Text); WNetCancelConnection2(strLocalName,CONNECT_UPDATE_PROFILE,False); end; end. |
图2 程序运行的初始画面 图3 输入参数 |