R
rockroll
Unregistered / Unconfirmed
GUEST, unregistred user!
初学VC,我想把以前程序里的如下代码转换成C++:
procedure TSearchHostThread.Execute;
Var
EnumHandle : THandle;
WorkgroupRS : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : DWord;
Entries : DWord;
Result : Integer;
begin
Workgroup := Workgroup + #0;
FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);
With WorkgroupRSdo
begin
dwScope := 2;
dwType := 3;
dwDisplayType := 1;
dwUsage := 2;
lpRemoteName := @Workgroup[1];
end;
WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
@WorkgroupRS,
EnumHandle );
Repeat
Entries := 1;
BufSize := SizeOf(Buf);
Result := WNetEnumResource( EnumHandle,Entries,@Buf,BufSize );
If (Result = NO_ERROR) and (Entries = 1) then
begin
myhost:= StrPas(Buf[1].lpRemoteName);
Synchronize(AddHost);
end;
Until (Entries <> 1) or (Result <> NO_ERROR);
WNetCloseEnum( EnumHandle );
end;
转换后的代码如下:
void CNet_zwDlg::OnButton1()
{
// TODO: Add your control notification handler code here
NETRESOURCE NR;
LPNETRESOURCE lpNetResource; // pointer to resource structure
HANDLE EnumHandle=0;
// pointer to enumeration handle buffer
LPNETRESOURCE Buf[500];
DWORD BufSize;
DWORD Entries;
DWORD Result;
//result of enum
//CString str;
// convert CString m_dir to Char;
int tem = m_host.GetLength();
char* c = new char[tem];
for(int i=0;i<tem;i++)
{
c=m_host;
}
c="workgroup";
/*UpdateData(true);
m_out= c;
UpdateData(false);*/
NR.dwScope = RESOURCE_GLOBALNET;
NR.dwType = RESOURCETYPE_ANY;
NR.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN;
NR.dwUsage = RESOURCEUSAGE_CONTAINER;
NR.lpRemoteName = c;
lpNetResource = &NR;
lpNetResource = (LPNETRESOURCE)NULL;
Result=WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
lpNetResource,
&EnumHandle );
if(Result != NO_ERROR)
{
UpdateData(true);
m_out+="error in WNetOpenEnum/r/n";
UpdateData(false);
}
int temp=0;
BufSize = 63840;
do
{
Entries = 500;
Buf[temp] = (LPNETRESOURCE)GlobalAlloc( GPTR, BufSize );
Result = WNetEnumResource( EnumHandle,&Entries,Buf[temp],&BufSize );
UpdateData(true);
if (Result == NO_ERROR)
{
CString str;
str.Format("%s",Buf[tem++]->lpRemoteName);
m_out+= str;
//m_out+=(*lpNetResource).lpRemoteName;
}
else
{
m_out+="error in enum/r/n";
}
UpdateData(false);
}
while(Result == NO_ERROR &&
Entries == 1);
WNetCloseEnum( EnumHandle );
}
编译通过,但运行后得不到工作组中的主机名,不知哪儿错了,请大家指点一下。
procedure TSearchHostThread.Execute;
Var
EnumHandle : THandle;
WorkgroupRS : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : DWord;
Entries : DWord;
Result : Integer;
begin
Workgroup := Workgroup + #0;
FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);
With WorkgroupRSdo
begin
dwScope := 2;
dwType := 3;
dwDisplayType := 1;
dwUsage := 2;
lpRemoteName := @Workgroup[1];
end;
WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
@WorkgroupRS,
EnumHandle );
Repeat
Entries := 1;
BufSize := SizeOf(Buf);
Result := WNetEnumResource( EnumHandle,Entries,@Buf,BufSize );
If (Result = NO_ERROR) and (Entries = 1) then
begin
myhost:= StrPas(Buf[1].lpRemoteName);
Synchronize(AddHost);
end;
Until (Entries <> 1) or (Result <> NO_ERROR);
WNetCloseEnum( EnumHandle );
end;
转换后的代码如下:
void CNet_zwDlg::OnButton1()
{
// TODO: Add your control notification handler code here
NETRESOURCE NR;
LPNETRESOURCE lpNetResource; // pointer to resource structure
HANDLE EnumHandle=0;
// pointer to enumeration handle buffer
LPNETRESOURCE Buf[500];
DWORD BufSize;
DWORD Entries;
DWORD Result;
//result of enum
//CString str;
// convert CString m_dir to Char;
int tem = m_host.GetLength();
char* c = new char[tem];
for(int i=0;i<tem;i++)
{
c=m_host;
}
c="workgroup";
/*UpdateData(true);
m_out= c;
UpdateData(false);*/
NR.dwScope = RESOURCE_GLOBALNET;
NR.dwType = RESOURCETYPE_ANY;
NR.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN;
NR.dwUsage = RESOURCEUSAGE_CONTAINER;
NR.lpRemoteName = c;
lpNetResource = &NR;
lpNetResource = (LPNETRESOURCE)NULL;
Result=WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
lpNetResource,
&EnumHandle );
if(Result != NO_ERROR)
{
UpdateData(true);
m_out+="error in WNetOpenEnum/r/n";
UpdateData(false);
}
int temp=0;
BufSize = 63840;
do
{
Entries = 500;
Buf[temp] = (LPNETRESOURCE)GlobalAlloc( GPTR, BufSize );
Result = WNetEnumResource( EnumHandle,&Entries,Buf[temp],&BufSize );
UpdateData(true);
if (Result == NO_ERROR)
{
CString str;
str.Format("%s",Buf[tem++]->lpRemoteName);
m_out+= str;
//m_out+=(*lpNetResource).lpRemoteName;
}
else
{
m_out+="error in enum/r/n";
}
UpdateData(false);
}
while(Result == NO_ERROR &&
Entries == 1);
WNetCloseEnum( EnumHandle );
}
编译通过,但运行后得不到工作组中的主机名,不知哪儿错了,请大家指点一下。