VC下编程实现网络驱动器的映射

在Microsoft Windows环境下,应用程序使用Windows网络函数(WNet)来实现网络功能而不用关心网络的供应商及具体实现。因为WNet函数是独立于网络的。

Wnet函数主要有:WnetAddConnection , WnetAddConnection2 , WnetAddConnection3 , WnetCancelConnection2等。本文主要用WnetAddConnection2,WnetCancelConnection2函数,下 面简单介绍一下.关于WNet函数更详细的资料请参考Microsoft API函数说明。


为调用此函数必须填写
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 : 指定提供网络资源的供应商。如为空,则表示供应商未知。


DWORD WNetAddConnection2(
  LPNETRESOURCE lpNetResource,  // points to structure that 
                                // specifies connection details
  LPCTSTR lpPassword,           // points to password string
  LPCTSTR lpUsername,           // points to user name string
  DWORD dwFlags                 // set of bit flags that specify       
);

WNetAddConnection2函数的
    lpPassword为远程资源的口令。
    lpUserName为远程资源的用户名。
    dwFlags标志位用于指定登录时是否重新连接
   (0时表示不重新连接,CCONNECT_UPDATE_PROFILE登录时重新连接)。

BOOL CHookKBApp::InitInstance() 
{
     NETRESOURCE netres; 
     netres.dwScope=RESOURCE_GLOBALNET;
     netres.dwType=RESOURCETYPE_ANY;
     netres.dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
     netres.dwUsage=RESOURCEUSAGE_CONNECTABLE;
     netres.lpLocalName="h:"; 
     netres.lpRemoteName="\\\\yin\\SharedDocs";
     netres.lpComment=NULL; 
     netres.lpProvider=NULL;


    if(NO_ERROR!=WNetAddConnection2(&netres,NULL,NULL,0))
        AfxMessageBox("映射失败");
    else
        AfxMessageBox("映射成功");

    
    return CWinApp::InitInstance();
}

int CHookKBApp::ExitInstance() 
{
    if(NO_ERROR!=WNetCancelConnection2("h:",0,false))
        AfxMessageBox("断开失败");

    return CWinApp::ExitInstance();
}

最后不要忘记加入MPR.lib这个库文件。在Project菜单,"Add to Project"->"Files",
选X:\Microsoft visual studio\VC98\LIb\mpr.lib   x:是你安装VC的盘符。



如果要用NET命令在命令行方式下实现:
net use y: \\PAK-2\SharedDocs  //映射
net use y: /delete                //删除




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值