標籤:mem not for sha member char profile form sign
type TNetDiskMapper=class private FNetResource: TNetResource; FUserName,FPassWord:PWideChar; public constructor Create(DriveName,ShareURI,UserName,Password:PWideChar); destructor Destory(); function ConnectDiskMap:boolean; function disConnectDiskMap:boolean;end;var NetDiskMap: TNetDiskMapper;{ TNetDiskMapper }constructor TNetDiskMapper.Create(DriveName, ShareURI, UserName, Password: PWideChar);begin FNetResource.dwType := RESOURCETYPE_DISK{磁碟資源}; FNetResource.lpLocalName :=driveName { 指定本地裝置 }; FNetResource.lpRemoteName := PChar(ShareURI) { 指定遠程網路名稱 }; FNetResource.lpProvider := nil { 指定提供網路資源的供應商。如為空白,則表示供應商未知。 }; FUserName:=UserName { 遠端資源的使用者名稱 }; FPassword:=Password { 遠端資源的口令 };end;function TNetDiskMapper.ConnectDiskMap: boolean;begin
{ WNetAddConnection2 的參數說明:
dwFlags標誌位用於指定登入時是否重新串連(0時表示不重新串連,CCONNECT_UPDATE_PROFILE登入時重新串連)。
}
result:=false; case WNetAddConnection2(FNetResource,FPassword,FUserName,CONNECT_UPDATE_PROFILE) of NO_ERROR:begin result:=true; ShowMessage(‘映射成功‘) ; end; ERROR_ACCESS_DENIED: showmessage(‘Access is denied.‘); ERROR_ALREADY_ASSIGNED:ShowMessage(‘The device specified in the lpLocalName parameter is already connected.‘); ERROR_BAD_DEV_TYPE: ShowMessage(‘The device type and the resource type do not match.‘); ERROR_BAD_DEVICE: ShowMessage(‘The value specified in lpLocalName is invalid‘); ERROR_BAD_NET_NAME: ShowMessage(‘The value specified in the lpRemoteName parameter is not valid or cannot be located.‘); ERROR_BAD_PROFILE : ShowMessage(‘ The user profile is in an incorrect format.‘) ; ERROR_CANNOT_OPEN_PROFILE : ShowMessage(‘ The system is unable to open the user profile to process persistent connections. ‘); ERROR_DEVICE_ALREADY_REMEMBERED : ShowMessage(‘An entry for the device specified in lpLocalName is already in the user profile.‘) ; ERROR_EXTENDED_ERROR :ShowMessage(‘A network-specific error occurred. To get a description of the error, use the WNetGetLastError function. ‘); ERROR_INVALID_PASSWORD:ShowMessage(‘ The specified password is invalid. ‘); ERROR_NO_NET_OR_BAD_PATH:ShowMessage(‘ The operation cannot be performed because either a network component is not started or the specified name cannot be used.‘); ERROR_NO_NETWORK:ShowMessage(‘ The network is not present. ‘); else ShowMessage(‘其他意外終止!‘); end;end;destructor TNetDiskMapper.Destory;begin disConnectDiskMap;end;function TNetDiskMapper.disConnectDiskMap: boolean;begin if NO_ERROR= WNetCancelConnection2(FNetResource.lpLocalName,CONNECT_UPDATE_PROFILE,True) then begin result:=true; end else result:=false;end;procedure TForm1.Button1Click(Sender: TObject);begin//映射網路磁碟機 NetDiskMap:=TNetDiskMapper.Create(‘Z:‘,‘\\127.0.0.1\2016年安規調考‘,nil,nil); showmessage(booltostr(NetDiskMap.ConnectDiskMap,true));end;procedure TForm1.Button2Click(Sender: TObject);begin//斷開網路磁碟機
if assigned(NetDiskMap) and ( NetDiskMap<>nil) thenshowmessage(booltostr(NetDiskMap.disConnectDiskMap,true));end;
delphi實現映射和斷開網路磁碟機