来自:http://go5.163.com/nowcan/tech/netshare.htm
#include <windows.h>
#include <stdio.h>
#include <lm.h>
void ShowError(DWORD Code)
{
printf("/nError: %ld----",Code);
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
Code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
//MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONINFORMATION );
printf("%S/n",(LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}
void EnumGroup(TCHAR *pszServerName)
{
NET_API_STATUS nStatus,mRet;
DWORD dwLevel = 1;
PLOCALGROUP_INFO_1 pBuf=NULL,p;
DWORD Read=0,Entry=0,Resume=0;
PLOCALGROUP_MEMBERS_INFO_2 mBuf=NULL,m;
DWORD mRead=0,mEntry=0,mResume=0;
printf("-------------GROUP INFORMATION-------------/n");
nStatus=NetLocalGroupEnum(pszServerName,dwLevel,(LPBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&Read, &Entry, &Resume);
if(nStatus == NERR_Success)
{
p=pBuf;
for(DWORD i=0;i<Read;i++)
{
printf("Group: %-30S",p->lgrpi1_name);
printf("Comment: %-30S/n",p->lgrpi1_comment);
mRet = NetLocalGroupGetMembers(pszServerName,p->lgrpi1_name, 2, (LPBYTE *)&mBuf, MAX_PREFERRED_LENGTH, &mRead, &mEntry, &mResume);
if(mRet==NERR_Success)
{
m=mBuf;
printf("-----Members-----/n");
for(DWORD j=0;j<mRead;j++)
{
printf("%S/n",m->lgrmi2_domainandname);
m++;
}
printf("/n");
}
else
{
ShowError(mRet);
}
NetApiBufferFree(mBuf);
p++;
}
}
else
{
ShowError(nStatus);
}
NetApiBufferFree (pBuf);
printf("/n/n");
}
int DispServer(TCHAR *pszServerName)
{
DWORD dwLevel = 101;
LPSERVER_INFO_101 pBuf = NULL;
NET_API_STATUS nStatus;
//
// Call the NetServerGetInfo function, specifying level 101.
//
nStatus = NetServerGetInfo(pszServerName,
dwLevel,
(LPBYTE *)&pBuf);
//
// If the call succeeds,
//
if (nStatus == NERR_Success)
{
//
// Check for the type of server.
//
if ((pBuf->sv101_type & SV_TYPE_DOMAIN_CTRL) ||
(pBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) ||
(pBuf->sv101_type & SV_TYPE_SERVER_NT))
printf("Type: Server/n");
else
printf("Type: Workstation/n");
printf("Name: %S/n",pBuf->sv101_name);
printf("OS: ");
switch(pBuf->sv101_platform_id)
{
case PLATFORM_ID_DOS:
printf("DOS/n");
break;
case PLATFORM_ID_OS2:
printf("OS2/n");
break;
case PLATFORM_ID_NT:
printf("NT/n");
break;
case PLATFORM_ID_OSF:
printf("OSF/n");
break;
case PLATFORM_ID_VMS:
printf("VMS/n");
break;
default:
printf("Unknown/n");
break;
}
printf("Version: %d.%d/n",pBuf->sv101_version_major,pBuf->sv101_version_minor);
printf("Comment: %S/n",pBuf->sv101_comment);
}
//
// Otherwise, print the system error.
//
//else
// printf("A system error has occurred: %d/n", nStatus);
//
// Free the allocated memory.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);
printf("/n/n");
return 0;
}
void EnumShare(TCHAR *lpszServer)
{
PSHARE_INFO_1 BufPtr,p;
NET_API_STATUS res;
DWORD er=0,tr=0,resume=0, i;
//
// Print a report header.
//
printf("Share Share Type: /n");
printf("--------------------------------------------------/n");
//
// Call the NetShareEnum function; specify level 1.
//
do // begin do
{
res = NetShareEnum (lpszServer, 1, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
//
// If the call succeeds,
//
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
{
p=BufPtr;
//
// Loop through the entries;
// print retrieved data.
//
for(i=1;i<=er;i++)
{
printf("%-20S",p->shi1_netname);
switch(p->shi1_type)
{
case STYPE_DISKTREE:
printf("%-30s/n","Disk driver");
break;
case STYPE_PRINTQ:
printf("%-30s/n","Printe queue");
break;
case STYPE_DEVICE:
printf("%-30s/n","Communication device");
break;
case STYPE_IPC:
printf("%-30s/n","Interprocess communication (IPC)");
break;
case STYPE_SPECIAL:
printf("%-30s/n","Special share");
break;
default:
printf("%-30s/n","Unknown share");
break;
}
p++;
}
//
// Free the allocated buffer.
//
NetApiBufferFree(BufPtr);
}
else
{
ShowError(res);
}
// Continue to call NetShareEnum while
// there are more entries.
//
}while (res==ERROR_MORE_DATA); // end do
return;
}
void DisplayStruct(LPNETRESOURCE lpnr)
{
switch(lpnr->dwDisplayType)
{
case RESOURCEDISPLAYTYPE_DOMAIN:
printf("--------Domain--%S--------/n",lpnr->lpRemoteName);
break;
case RESOURCEDISPLAYTYPE_SERVER:
{
TCHAR IPC[MAX_PATH];
NETRESOURCE netr;
printf("--------Server--%S--------/n",lpnr->lpRemoteName);
wsprintf(IPC,L"%s//IPC$",lpnr->lpRemoteName);
netr.dwScope = RESOURCE_GLOBALNET;
netr.dwType = RESOURCETYPE_ANY;
netr.lpLocalName = L"";
netr.lpRemoteName = IPC;
netr.lpProvider = NULL;
WNetAddConnection2(&netr,L"",L"",NULL);
DispServer(lpnr->lpRemoteName);
EnumShare(lpnr->lpRemoteName);
WNetCancelConnection2(IPC,CONNECT_UPDATE_PROFILE,TRUE);
printf("/n/n");
}
break;
// case RESOURCEDISPLAYTYPE_SHARE:
// printf("Share:%S/n",lpnr->lpRemoteName);
// break;
default:
// printf("Unknown:%S/n",lpnr->lpRemoteName);
break;
}
}
BOOL WINAPI EnumerateFunc(LPNETRESOURCE lpnr)
{
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
DWORD i;
//
// Call the WNetOpenEnum function to begin the enumeration.
//
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_ANY, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
&hEnum); // handle to the resource
if (dwResult != NO_ERROR)
{
return FALSE;
}
//
// Call the GlobalAlloc function to allocate resources.
//
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
do
{
//
// Initialize the buffer.
//
ZeroMemory(lpnrLocal, cbBuffer);
//
// Call the WNetEnumResource function to continue
// the enumeration.
//
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&cEntries, // defined locally as -1
lpnrLocal, // LPNETRESOURCE
&cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
//
if (dwResultEnum == NO_ERROR)
{
for(i = 0; i < cEntries; i++)
{
// Call an application-defined function to
// display the contents of the NETRESOURCE structures.
//
DisplayStruct(&lpnrLocal);
// If the NETRESOURCE structure represents a container resource,
// call the EnumerateFunc function recursively.
if(RESOURCEUSAGE_CONTAINER == (lpnrLocal.dwUsage & RESOURCEUSAGE_CONTAINER))
EnumerateFunc(&lpnrLocal);
}
}
// Process errors.
//
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
break;
}
}while(dwResultEnum != ERROR_NO_MORE_ITEMS);
//
// Call the GlobalFree function to free the memory.
//
GlobalFree((HGLOBAL)lpnrLocal);
//
// Call WNetCloseEnum to end the enumeration.
//
dwResult = WNetCloseEnum(hEnum);
if(dwResult != NO_ERROR)
{
return FALSE;
}
return TRUE;
}
int wmain(int argc,TCHAR **argv)
{
if(argc==2)
{
TCHAR IPC[MAX_PATH];
NETRESOURCE netr;
wsprintf(IPC,L"%s//IPC$",argv[1]);
netr.dwScope = RESOURCE_GLOBALNET;
netr.dwType = RESOURCETYPE_ANY;
netr.lpLocalName = L"";
netr.lpRemoteName = IPC;
netr.lpProvider = NULL;
WNetAddConnection2(&netr,L"",L"",NULL);
DispServer(argv[1]);
EnumGroup(argv[1]);
EnumShare(argv[1]);
WNetCancelConnection2(IPC,CONNECT_UPDATE_PROFILE,TRUE);
printf("/n/n");
}
else
{
EnumerateFunc(NULL);
}
}
实际上效果不会比
http://delphi.mychangshu.com/dispdoc.asp?id=377
http://delphi.mychangshu.com/dispdoc.asp?id=868
好。