VC下编程实现网络驱 4000 动器的映射
2014-09-13 08:50
429 查看
在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 //删除
相关文章推荐
- VC下编程实现网络驱动器的映射
- vc++网络安全编程范例(18)-open ssl 实现数字证书编程
- vc++网络安全编程范例(19)实现数字信封打包与拆解
- VC++网络安全编程范例(9)-基于OPENSSL实现对称算法与BASE64编码
- vc++网络安全编程范例(17)-open ssl 实现文件加密与解密
- [VC网络编程笔记-使用API函数操作互斥对象实现线程同步
- vc++网络安全编程范例(17)-open ssl 实现文件加密与解密
- VC下用MFC类实现网络编程
- 利用vc++中的winInet进行网络编程实现了下载网站的源代码
- VC++网络安全编程范例(8)-摘要签名和验证编程实现
- VC下用MFC类实现网络编程
- VC++网络安全编程范例(8)-摘要签名和验证编程实现
- VC++网络安全编程范例(7)-实现哈希摘要算法
- NTLM 网络安全认证编程实现(VC)
- VC++网络安全编程范例(9)-基于OPENSSL实现对称算法与BASE64编码
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- vc网络编程入门
- VC++下命名管道编程的原理及实现
- 用VC++ 5.0实现视频捕获编程(转载)