工控编程吧
标题:
335上位机VC MFC获取局域网共享资源
[打印本页]
作者:
qq263946146
时间:
2016-9-21 21:05
标题:
335上位机VC MFC获取局域网共享资源
(, 下载次数: 1)
上传
点击文件名下载附件
获取局域网共享资源
功能展示
工业控制中局域网是很常见的一种网络,网络中资源可以共享,提高工作效率,节省资源,当前例程实现局域网中共享资源列举的功能,效果如图,程序运行就会罗列出整个局域网共享的资源,点击刷新可以重新检查网络中的共享资源;
要点提示
例程共享资源的列举主要用到函数WNetOpenEnum(), WNetEnumResource(), WNetCloseEnum(),函数的介绍之前例程已给出,这里要注意的是在使用这些函数前得事先引入函数所在的头文件及库文件,如果例程中只有添加了代码#pragma comment(lib,“mpr.lib”),才能正常编译通过;
实现功能
1.新建基于对话框的应用程序
2.按照例程添加一树状控件IDC_TREE1关联变量CTreeCtrl m_Tree;及按钮控件<刷新>关联点击函数void CGkbc8Dlg::OnButton1() {FullFill();}
3.在主对话框中添加变量CImageList m_imagelist;并初始化
m_imagelist.Create(16,16,ILC_COLOR24|ILC_MASK,0,0);
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
m_Tree.SetImageList(&m_imagelist,LVSIL_NORMAL);
FullFill();
4. 前面有出现一自定义函数FullFill();
void CGkbc8Dlg::FullFill()
{
m_Tree.DeleteAllItems();
HTREEITEM Root,Group,Name;
Root=m_Tree.InsertItem("整个网络",0,0);
m_Tree.Expand(Root,TVE_EXPAND);
DWORD Count=0xFFFFFFFF,Bufsize=4096,Res;
NETRESOURCE* nRes;
NETRESOURCE* nRes1;
NETRESOURCE* nRes2;
NETRESOURCE* nRes3;
HANDLE lphEnum;
LPVOID Buf = new char[4096];
LPVOID Buf1 = new char[4096];
LPVOID Buf2 = new char[4096];
LPVOID Buf3 = new char[4096];
Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER,NULL,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count,Buf,&Bufsize);
nRes=(NETRESOURCE*)Buf;
for(DWORD n=0;n<Count;n++,nRes++)
{
DWORD Count1=0xFFFFFFFF;
Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER,nRes,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count1,Buf1,&Bufsize);
nRes1=(NETRESOURCE*)Buf1;
for(DWORD i=0;i<Count1;i++,nRes1++)
{
Group = m_Tree.InsertItem(nRes1->lpRemoteName,1,1,Root);
DWORD Count2=0xFFFFFFFF;
Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER,nRes1,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count2,Buf2,&Bufsize);
nRes2=(NETRESOURCE*)Buf2;
for(DWORD j=0;j<Count2;j++,nRes2++)
{
CString sName = nRes2->lpRemoteName;
sName = sName.Right(sName.GetLength()-2);
Name = m_Tree.InsertItem(sName,2,2,Group);
DWORD Count3=0xFFFFFFFF;
Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE,nRes2,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count3,Buf3,&Bufsize);
nRes3=(NETRESOURCE*)Buf3;
for(DWORD k=0;k<Count3;k++,nRes3++)
{
CString sShare = nRes3->lpRemoteName;
sShare = sShare.Right(sShare.GetLength()-3-sName.GetLength());
m_Tree.InsertItem(sShare,3,3,Name);
}
}
}
}
delete Buf; delete Buf1; delete Buf2; delete Buf3;
WNetCloseEnum(lphEnum);
}
我们来演示下功能实现的整个过程
[iqiyi]http://player.video.qiyi.com/5b4001f5f6180aae039f00de210e6dd6/0/0/w_19rstht21t.swf-albumId=6348149709-tvId=6348149709-isPurchase=0-cnId=12[/iqiyi]
(, 下载次数: 0)
上传
点击文件名下载附件 [weixinlianxi]1[/weixinlianxi]
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4