用Delphi实现网络驱动器的映射和断开  
整理编辑:China ASP
 
---- 大家在运用Delphi编写网络应用程序时可能会遇到这样一个问题:在一个由Windows NT和Novell Netware组成的网络环境中,应用程序需要使用Novell网上的某些数据,如果每次都手工在Windows资源管理器中映射,断开网络驱动器,不仅繁琐而且显得不够专业。本文将介绍在Delphi中使用Windows32 API函数解决这一问题。 一、 基本原理
---- 在Microsoft Windows环境下,应用程序使用Windows网络函数(WNet)来实现网络功能而不用关心网络的供应商及具体实现。因为WNet函数是独立于网络的。 ---- Wnet函数主要有:WnetAddConnection , WnetAddConnection2 , WnetAddConnection3 , WnetCancelConnection2等。本文主要用WnetAddConnection2,WnetCancelConnection2函数,下面简单介绍一下,关于WNet函数更详细的资料请参考Delphi的连机文档和Microsoft API函数说。 
---- WNetAddConnection2在Windows.pas中的函数原型如下: 
     function WNetAddConnection2(var lpNetResource: TNetResource;lpPassword, lpUserName: PChar; dwFlags:DWORD): DWORD; stdcall;为调用此函数必须填写lpNetResouce结构,此结构的定义为:
typedef  struct _NETRESOUCE{
    DWORD dwScope;  
    DWORD dwType;  
    DWORD dwDisplayType;
    DWORD dwUsage;
    LPTSTR lpLocalName;
    LPTSR  lpRemoteName;
    LPTSr  lpProvider;
} NETRESOURCE; 
这里使用dwType,lpLocalName,lpRemoteName,lpProvider几个参数。其含义如下:
dwType : 用于指定网络的资源类型,有以下
        RESOURCETYPE_ANY(任何资源) , 
        RESOURCETYPE_DISK(磁盘资源) ,
        RESOURCETYPE_PRINT(打印机资源)。
lpLocalName :  指定本地设备。
lpRemoteName : 指定远程网络名。
lpProvider :      指定提供网络资源的供应商。如为空,则表示供应商未知。WNetAddConnection2函数的
    lpPassword为远程资源的口令。 
    lpUserName为远程资源的用户名。
    dwFlags标志位用于指定登录时是否重新连接(0时表示不重新连接,CCONNECT_UPDATE_PROFILE登录时重新连接)。WnetCancelConnection2在Windows.pas中的函数原型如下:
function WNetCancelConnection2(lpName: PChar; dwFlags: DWORD; fForce: BOOL): DWORD; stdcall;
  lpName : 要断开的远程网络资源或本地重定向驱动器。
  dwFlags : 含义同上。
  fForce :   True表示不管是否有文件打开,强制性断开网络驱动器;
             False 表示如有文件打开则函数运行失败。二、实例
---- 在Delphi的File菜单下选择New Application,在Form1上放置一个Button,在Button1的Click事件中键入如下代码: 
procedure TForm1.Button1Click(Sender : TObject);
var
  NetSource : TNetResource;
begin
  with NetSource do
  begin
    dwType := RESOURCETYPE_ANY;
    lpLocalName := 'X:';       // 将远程资源映射到此驱动器
    lpRemoteName := '\\hqServer\sys';  // 远程网络资源
    lpProvider := '';  // 必须赋值,如为空则使用lpRemoteName的值。
  end;  WnetAddConnection2(NetSource, 'Password', 'Guest',CONNECT_UPDATE_PROFILE);    //用户名为Guest,口令为Password
  //下次登录时重新连接
  //此时在Windows资源管理器中可看到网络驱动器X:
  if MessageDlg('Are you sure to disconnect Drive ?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then      
  //不管是否有文件打开,断开网络驱动器X:
      WNetCancelConnection2( 'X:', CONNECT_UPDATE_PROFILE, True);   
end;---- 本程序在Delphi3.0,4.0、PWindows95、Novell Netware 3.12下编译运行通过。 

解决方案 »

  1.   

    如何映射网络驱动器 
    function WNetAddConnection2W(var lpNetResource: TNetResourceW; lpPassword, lpUserName: PWideChar; dwFlags: DWORD): DWORD; stdcall; To make the call you will need to fill a lpNetResource structure with a minimum set of parameters, shown in the example below. You pass this structure as the first parameter to the call, the password, user name, and a flag that indicates whether this mapping should be persistant every time the machine is logged onto. For more info on the API itself, see Window's Programmers Reference help (find the function in Windows.pas, place your text cursor over the 
    function call, and hit F1 to bring up help). procedure TForm1.Button1Click(Sender: TObject); 
    var 
      NRW: TNetResource; 
    begin 
      with NRW do 
      begin 
        dwType := RESOURCETYPE_ANY; 
        lpLocalName := 'X:'; // map to this driver letter 
        lpRemoteName := '\\MyServer\MyDirectory'; 
        // Must be filled in. If an empty string is used, 
        // it will use the lpRemoteName. 
        lpProvider := ''; 
      end; 
      WNetAddConnection2(NRW, 'MyPassword', 'MyUserName', CONNECT_UPDATE_PROFILE);
    end; 
      

  2.   

    不是目录共享,是web共享目录???