delphi實現映射和斷開網路磁碟機

來源:互聯網
上載者:User

標籤: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實現映射和斷開網路磁碟機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.