在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 //删除
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 //删除
本文介绍如何使用WNet函数在Windows环境下进行网络资源映射,包括WnetAddConnection2和WnetCancelConnection2函数的应用,并提供了具体的代码示例。
1044

被折叠的 条评论
为什么被折叠?



