//////////////////////////////////////// //////////////////////////////////////// ////////
// Purpose: enumerate network neighbors
// Parameter: firstenum = 1. Start enumeration. If true is returned, set this parameter to 0 and continue enumeration until false is returned.
// Reserve = workgroup or domain name
// Return: True is returned for success. The neighborname attribute contains a network neighbor name;
// If no new network neighbor or method fails, false is returned.
Bool cCommunication: enumneighbor (long firstenum, lpctstr Reserve)
{
// Todo: add your dispatch handler code here
Strlasterror = "";
Strneighborname = "";
netresource; // result content of the current enumeration
handle henum; // enumeration handle
DWORD dreturn; // error return code
If (firstenum = 1) // call 1st times
{
Netresource nets;
Zeromemory (& nets, sizeof (netresource ));
Nets. lpremotename = (char *) reserve;
Nets. dwdisplaytype = resourcedisplaytype_server;
Nets. dwusage = resourceusage_container;
Nets. dwscope = resourcetype_disk;
Dreturn = wnetopenenum (resource_globalnet,
Resourcetype_disk,
Resourceusage_container,
& Nets,
& Henum );
If (dreturn! = No_error)
{
Dreturn =: getlasterror ();
Switch (dreturn)
{
Case error_extended_error:
Strlasterror = "error_extended_error ";
Return false;
Case error_not_container:
Strlasterror = "error_not_container ";
Return false;
Case error_invalid_parameter:
Strlasterror = "error_invalid_parameter ";
Return false;
Case error_no_network:
Strlasterror = "error_no_network ";
Return false;
}
}
DWORD dcount = 0 xfffffff; // maximum number of enumerated items
DWORD dlen = 16384; // The allocated enumerated result Memory Length is 16 K
Lpnetresource = (char *) globalalloc (gptr, dlen );
Dcurneighbor = 0;
Dneighborcount = 0;
Do
{
Dreturn = wnetenumresource (henum, & dcount, lpnetresource, & dlen );
If (dreturn = no_error)
{
Dcurneighbor = 0;
Dneighborcount = dcount;
}
Else if (dreturn! = Error_no_more_items)
{
Wnetcloseenum (henum );
Globalfree (hglobal) lpnetresource );
Lpnetresource = NULL;
Strlasterror = "network neighbor Enumeration failed ";
Return false;
}
} While (dreturn! = Error_no_more_items );
Wnetcloseenum (henum );
If (dneighborcount = 0)
{
Globalfree (hglobal) lpnetresource );
Lpnetresource = NULL;
Strlasterror = "no host ";
Return false;
}
Copymemory (& netresource, lpnetresource + sizeof (netresource) * dcurneighbor, sizeof (netresource ));
Dcurneighbor ++;
Strneighborname = netresource. lpremotename + 2; // obtain the name of the host connected to the local host. + 2 is to remove '\' before the host name '\\'
Return true;
}
Else
{
If (lpnetresource = NULL)
{
Strlasterror = "1st calls to this method should set 1st parameters to 1 ";
Return false;
}
If (dcurneighbor = dneighborcount)
{
Globalfree (hglobal) lpnetresource );
Lpnetresource = NULL;
Return false;
}
Copymemory (& netresource, lpnetresource + sizeof (netresource) * dcurneighbor, sizeof (netresource ));
Dcurneighbor ++;
Strneighborname = netresource. lpremotename + 2; // obtain the name of the host connected to the local host.
Return true;
}
}
//////////////////////////////////////// //////////////////////////////////////// ////////
// Purpose: If enumneighbor () is used to enumerate network neighbors, the enumerated neighbor name is returned.
BSTR cCommunication: getneighborname ()
{
Cstring strresult;
// Todo: add your property handler here
Strresult = strneighborname;
Return strresult. allocsysstring ();
}
// Discard
Void cCommunication: setneighborname (lpctstr lpsznewvalue)
{
// Todo: add your property handler here
}
//////////////////////////////////////// //////////////////////////////////////// ////////
// Purpose: After the enumdomain () Enumeration Working Group or domain is successful, return the enumerated working group or domain name
BSTR cCommunication: getdomainname ()
{
Cstring strresult;
// Todo: add your property handler here
Strresult = strdomain;
Return strresult. allocsysstring ();
}
// Discard
Void cCommunication: setdomainname (lpctstr lpsznewvalue)
{
// Todo: add your property handler here
}
//////////////////////////////////////// //////////////////////////////////////// ////////
// Purpose: actual enumerated workgroup or domain
// Parameter: netresource = network type
// Return: True is returned for success, and false is returned for failure.
Bool cCommunication: enumthedomain (netresource)
{
Handle henum; // enumeration handle
DWORD dreturn; // error return code
Netresource nets; // the result of the current enumeration
// Clear the structure of the content to be returned
Zeromemory (& nets, sizeof (netresource ));
// If the first enumerated domain and workgroup dcurneigbordm = 0, dneighborcountdm = 0
// If the domain and workgroup enumeration in the previous network type ends, the domain and the workgroup in the next network type are enumerated;
// At this time, netresource already contains information about the next network type
If (dcurneighbordm = dneighborcountdm)
{
Dreturn = wnetopenenum (resource_globalnet,
Resourcetype_disk,
Resourceusage_container,
& Netresource,
& Henum );
If (dreturn! = No_error)
{
Dreturn =: getlasterror ();
Switch (dreturn)
{
Case error_extended_error:
Strlasterror = "error_extended_error ";
Return false;
Case error_not_container:
Strlasterror = "error_not_container ";
Return false;
Case error_invalid_parameter:
Strlasterror = "error_invalid_parameter ";
Return false;
Case error_no_network:
Strlasterror = "error_no_network ";
Return false;
}
}
DWORD dcount = 0 xfffffff; // maximum number of enumerated items
DWORD dlen = 16384; // The allocated enumerated result Memory Length is 16 K
Lpnetresourcedm = (char *) globalalloc (gptr, dlen );
// Necessary Initialization
Dcurneighbordm = 0;
Dneighborcountdm = 0;
Do
{
Dreturn = wnetenumresource (henum, & dcount, lpnetresourcedm, & dlen );
If (dreturn = no_error)
{
Dcurneighbordm = 0;
Dneighborcountdm = dcount;
}
Else if (dreturn! = Error_no_more_items)
{
Wnetcloseenum (henum );
Globalfree (hglobal) lpnetresourcedm );
Lpnetresourcedm = NULL;
Strlasterror = "enumeration of working groups or domains failed ";
Return false;
}
} While (dreturn! = Error_no_more_items );
Wnetcloseenum (henum );
}
If (dneighborcountdm = 0)
{
Globalfree (hglobal) lpnetresourcedm );
Lpnetresourcedm = NULL;
Return false;
}
Copymemory (& nets, lpnetresourcedm + sizeof (netresource) * dcurneighbordm, sizeof (netresource ));
Dcurneighbordm ++;
Strdomain = nets. lpremotename;
If (dcurneighbordm = dneighborcountdm)
{
Globalfree (hglobal) lpnetresourcedm );
Lpnetresourcedm = NULL;
}
Return true;
}
///////////////////////////////////// //////////////////////////////////////// /// //
// purpose: enumeration workgroup or field
// parameter: firstenum = 1, start enumeration; if true is returned, set this parameter to 0, continue to enumerate until false is returned
// reserve = keep
// return: True is returned successfully. The domainname attribute contains an cited working group or domain name;
// if no new workgroup or domain or method fails, false is returned.
bool cCommunication: enumdomain (long firstenum, lpctstr Reserve)
{< br> // todo: add your dispatch handler code here
strlasterror = "";
strdomain = "";
netresource; // result content of the network type currently enumerated
handle henum; // enumeration handle
DWORD dreturn; // error return code
If (firstenum = 1) // call 1st times
{
Dreturn = wnetopenenum (resource_globalnet,
Resourcetype_disk,
Resourceusage_container,
Null,
& Henum );
If (dreturn! = No_error)
{
Dreturn =: getlasterror ();
Switch (dreturn)
{
Case error_extended_error:
Strlasterror = "error_extended_error ";
Return false;
Case error_not_container:
Strlasterror = "error_not_container ";
Return false;
Case error_invalid_parameter:
Strlasterror = "error_invalid_parameter ";
Return false;
Case error_no_network:
Strlasterror = "error_no_network ";
Return false;
}
}
DWORD dcount = 0 xfffffff; // maximum number of enumerated items
DWORD dlen = 16384; // The allocated enumerated result Memory Length is 16 K
Lpnetresourcenk = (char *) globalalloc (gptr, dlen );
Dcurneighbornk = 0;
Dneighborcountnk = 0;
Do
{
Dreturn = wnetenumresource (henum, & dcount, lpnetresourcenk, & dlen );
If (dreturn = no_error)
{
Dcurneighbornk = 0;
Dneighborcountnk = dcount;
}
Else if (dreturn! = Error_no_more_items)
{
Wnetcloseenum (henum );
Globalfree (hglobal) lpnetresourcenk );
Lpnetresourcenk = NULL;
Strlasterror = "An error occurred while enumerating the network type ";
Return false;
}
} While (dreturn! = Error_no_more_items );
Wnetcloseenum (henum );
If (dneighborcountnk = 0)
{
Globalfree (hglobal) lpnetresourcenk );
Lpnetresourcenk = NULL;
Return false;
}
Copymemory (& netresource, lpnetresourcenk + sizeof (netresource) * dcurneighbornk, sizeof (netresource ));
Dcurneighbornk ++;
If (enumthedomain (netresource) = false)
Goto goonenum;
Else
Return true;
}
Else
{
Goonenum:
If (lpnetresourcenk = NULL)
{
Strlasterror = "1st calls to this method should set 1st parameters to 1 ";
Return false;
}
// If the current index and total number of the network type are the same and the current index and total number of the domain or workgroup are the same
If (dcurneighbornk = dneighborcountnk & dcurneighbordm = dneighborcountdm)
{
Globalfree (hglobal) lpnetresourcenk );
Lpnetresourcenk = NULL;
Return false;
}
// If the current index and total number of the domain or workgroup are the same, obtain the information of the next network type.
If (dcurneighbordm = dneighborcountdm)
{
Copymemory (& netresource, lpnetresourcenk + sizeof (netresource) * dcurneighbornk, sizeof (netresource ));
Dcurneighbornk ++;
}
If (enumthedomain (netresource) = false)
Goto goonenum;
Else
Return true;
}
}