如何遍历局域网中的所有计算机

alexdean 2005-10-22 08:27:02
请大侠帮忙
以下是实现网上邻居树的代码,但编译不通过,请帮忙看看
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//#include <shlobj.h>
//#include <Comobj.hpp>
#include <shellapi.h>
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TImageList *List;
LPMALLOC mem;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{
LPSHELLFOLDER desk; //这里就报错
LPITEMIDLIST net;
SHFILEINFO fi;
STRRET nome;
char scom[255];
TTreeNode *n;
TDir *d;

List = new TImageList(this);
List->ShareImages = true;
List->Handle =
SHGetFileInfo("",0,&fi,sizeof(SHFILEINFO),SHGFI_SMALLICON|SHGFI_SYSICONINDEX);
TreeView->Images = List;

SHGetMalloc(&mem);

SHGetDesktopFolder(&desk);

SHGetSpecialFolderLocation(NULL,CSIDL_NETWORK ,&net);
desk->GetDisplayNameOf(net,SHGDN_NORMAL ,&nome);
GetNome(&nome,net,scom);

SHGetFileInfo((LPCSTR)net,0,&fi,sizeof(SHFILEINFO),SHGFI_PIDL|SHGFI_SYSICONINDEX|SHGFI_DISPLAYNAME |SHGFI_ATTRIBUTES|SHGFI_TYPENAME );
n=TreeView->Items->Add(NULL,scom);
n->ImageIndex = fi.iIcon;
n->StateIndex = fi.iIcon;
n->SelectedIndex = fi.iIcon;
d=new TDir;
d->ParentShellFolder = desk;
d->pidl = net;
d->ppidl = net;
d->Sfogliato = true;
n->Data = d;

BrowseFolder(desk,net,net,n);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BrowseFolder(LPSHELLFOLDER padre, LPITEMIDLIST pidl,LPITEMIDLIST ppidl,TTreeNode *nodo)
{
TTreeNode *c;
if(nodo != NULL)
{
while(nodo->Count > 0) nodo->Item[0]->Delete();
}
if(padre!= NULL)
{
LPSHELLFOLDER ShellFolder=NULL;

padre->BindToObject(pidl,NULL,IID_IShellFolder ,(void **)&ShellFolder);
if(ShellFolder != NULL)
{
LPENUMIDLIST enumlist=NULL;

ShellFolder->EnumObjects(NULL,SHCONTF_FOLDERS|SHCONTF_INCLUDEHIDDEN,&enumlist);

if(enumlist != NULL)
{
int i;
LPSTRRET nome ;
LPITEMIDLIST idfiglio;
LPITEMIDLIST pp;
char scom[255];
SHFILEINFO fi;
TDir *d;
int r;
int conta1,conta2;

enumlist->Reset();
while(enumlist->Next(1,&idfiglio,NULL)!=S_FALSE)
{
LPITEMIDLIST lpNew = MergeIDList(ppidl,idfiglio);
if(!idfiglio) break;
if(lpNew== NULL) continue;
nome = (LPSTRRET)mem->Alloc(sizeof (STRRET));

memset(&fi,0,sizeof(SHFILEINFO));

r=SHGetFileInfo((LPCSTR)lpNew,0,&fi,sizeof(fi),SHGFI_PIDL|SHGFI_SYSICONINDEX|SHGFI_DISPLAYNAME |SHGFI_ATTRIBUTES|SHGFI_TYPENAME|SHGFI_SMALLICON );

ZeroMemory(nome,sizeof(STRRET));
nome->uType = STRRET_CSTR;
ShellFolder->GetDisplayNameOf(idfiglio,SHGDN_NORMAL ,nome);
GetNome(nome,idfiglio,scom);

c=TreeView->Items->AddChild(nodo,scom);
c->ImageIndex = fi.iIcon;
c->StateIndex = fi.iIcon;
c->SelectedIndex = fi.iIcon;

// Cre elelemnto fittizio
if(fi.dwAttributes & SFGAO_HASSUBFOLDER)
TreeView->Items->AddChild(c,"PP");

d = new TDir;
d->ParentShellFolder = ShellFolder;
d->pidl = idfiglio;
d->ppidl = lpNew;
d->Sfogliato = false;
c->Data = d;
}
enumlist->Release();
}
}
}
}


void __fastcall TForm1::GetNome(LPSTRRET nome, LPITEMIDLIST id, char *display)
{

LPSTR lpsz;
int cch;

switch (nome->uType)
{
case STRRET_WSTR:
cch = WideCharToMultiByte( CP_ACP,0,nome->pOleStr,-1,NULL,0,NULL,NULL);
lpsz = (LPSTR) mem->Alloc(cch);
if (lpsz != NULL)
{
WideCharToMultiByte( CP_ACP,0,nome->pOleStr,-1,lpsz,cch,NULL,NULL);
sprintf(display,"%s", lpsz);
mem->Free(lpsz);
}
break;

case STRRET_OFFSET:
sprintf(display,"%s", ((char *) id) + nome->uOffset);
break;

case STRRET_CSTR:
sprintf(display,"%s", nome->cStr);
break;
}
}



LPITEMIDLIST __fastcall TForm1::MergeIDList(LPITEMIDLIST a, LPITEMIDLIST b)
{
int cb1, cb2;

if (a) cb1 = GetPIDLSize(a);
if (cb1) cb1 = cb1 - 2;

if (b) cb2 = GetPIDLSize(b);
if (cb2) cb2 = cb2 - 2;

int total_size = cb1 + cb2 + 2;

LPITEMIDLIST lpidlNew;

if(!a || !b) return NULL;
lpidlNew = (LPITEMIDLIST)mem->Alloc(total_size);
if (lpidlNew)
{
ZeroMemory(lpidlNew, total_size);
CopyMemory(lpidlNew, a, cb1);
CopyMemory(((LPBYTE)lpidlNew) + cb1, b, cb2);
FillMemory(((LPBYTE)lpidlNew) + cb1 + cb2, 2, 0);
return lpidlNew;
}
return NULL;
}


int __fastcall TForm1::GetPIDLSize(LPITEMIDLIST lpidl)
{
unsigned short cb = 0;
while (lpidl)
{
cb = (unsigned short)(cb + lpidl->mkid.cb);
lpidl = GetNextItem(lpidl);
}
return (unsigned short)(cb + 2);
}

LPITEMIDLIST __fastcall TForm1::GetNextItem(LPITEMIDLIST pidl)
{
unsigned short nLen = pidl->mkid.cb;
if (nLen == 0) return NULL;
return (LPITEMIDLIST)((LPBYTE)pidl + nLen);
}



void __fastcall TForm1::TreeViewExpanding(TObject *Sender, TTreeNode *Node,
bool &AllowExpansion)
{
TDir *d;

if(Node != NULL)
{
d = (TDir *)Node->Data;
if(!d->Sfogliato)
{
BrowseFolder(d->ParentShellFolder,d->pidl,d->ppidl,Node);
}
d->Sfogliato = true;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TreeViewClick(TObject *Sender)
{
TDir *d;
if(TreeView->Selected)
{
char scom[MAX_PATH];
d=(TDir *)(TreeView->Selected->Data);

SHGetPathFromIDList(d->ppidl,scom);
Label1->Caption = scom;
}
}
...全文
213 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
alloutoflove 2005-10-22
  • 打赏
  • 举报
回复
我怎么记得vcl.h已经包含shlojb.h(pp)..
ccrun.com 2005-10-22
  • 打赏
  • 举报
回复
估计是自定义的一个结构。
constantine 2005-10-22
  • 打赏
  • 举报
回复
TDir 是啥东西?
constantine 2005-10-22
  • 打赏
  • 举报
回复
LPSHELLFOLDER在#include <shlobj.h>里面
而要使用#include <shlobj.h>,就要
#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.h> //这两行要放在最前面
#include <vcl.h>
ccrun.com 2005-10-22
  • 打赏
  • 举报
回复
谁说VCL.h中包含shlojb.h。

这是个很精典的老问题了。 呵呵。解决办法正如安吉儿所说。

alexdean 2005-10-22
  • 打赏
  • 举报
回复
就是不知道TDir是什么?我也是在网上找到的这段代码

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧