用netshareenum()#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>void wmain( int argc, TCHAR *lpszArgv[ ])
{
   PSHARE_INFO_502 BufPtr,p;
   NET_API_STATUS res;
   LPTSTR   lpszServer = NULL;
   DWORD er=0,tr=0,resume=0, i;   switch(argc)
   {
   case 2:
      lpszServer = lpszArgv[1];
      break;
   default:
      printf("Usage: NetShareEnum <servername>\n");
      return;
   }
   //
   // Print a report header.
   //
   printf("Share:              Local Path:                   Uses:   Descriptor:\n");
   printf("---------------------------------------------------------------------\n");
   //
   // Call the NetShareEnum function; specify level 502.
   //
   do // begin do
   {
      res = NetShareEnum (lpszServer, 502, (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%-30S%-8u",p->shi502_netname, p->shi502_path, p->shi502_current_uses);
            //
            // Validate the value of the 
            //  shi502_security_descriptor member.
            //
            if (IsValidSecurityDescriptor(p->shi502_security_descriptor))
               printf("Yes\n");
            else
               printf("No\n");
            p++;
         }
         //
         // Free the allocated buffer.
         //
         NetApiBufferFree(BufPtr);
      }
      else 
         printf("Error: %ld\n",res);
   }
   // Continue to call NetShareEnum while 
   //  there are more entries. 
   // 
   while (res==ERROR_MORE_DATA); // end do
   return;
}

解决方案 »

  1.   

    还要用到NetSessionEnum()#ifndef UNICODE
    #define UNICODE
    #endif#include <stdio.h>
    #include <assert.h>
    #include <windows.h> 
    #include <lm.h>int wmain(int argc, wchar_t *argv[])
    {
       LPSESSION_INFO_10 pBuf = NULL;
       LPSESSION_INFO_10 pTmpBuf;
       DWORD dwLevel = 10;
       DWORD dwPrefMaxLen = -1;
       DWORD dwEntriesRead = 0;
       DWORD dwTotalEntries = 0;
       DWORD dwResumeHandle = 0;
       DWORD i;
       DWORD dwTotalCount = 0;
       LPTSTR pszServerName = NULL;
       LPTSTR pszClientName = NULL;
       LPTSTR pszUserName = NULL;
       NET_API_STATUS nStatus;
       //
       // Check command line arguments.
       //
       if (argc > 4)
       {
          wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n", argv[0]);
          exit(1);
       }   if (argc >= 2)
          pszServerName = argv[1];   if (argc >= 3)
          pszClientName = argv[2];   if (argc == 4)
          pszUserName = argv[3];
       //
       // Call the NetSessionEnum function, specifying level 10.
       //
       do // begin do
       {
          nStatus = NetSessionEnum(pszServerName,
                                   pszClientName,
                                   pszUserName,
                                   dwLevel,
                                   (LPBYTE*)&pBuf,
                                   dwPrefMaxLen,
                                   &dwEntriesRead,
                                   &dwTotalEntries,
                                   &dwResumeHandle);
          //
          // If the call succeeds,
          //
          if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
          {
             if ((pTmpBuf = pBuf) != NULL)
             {
                //
                // Loop through the entries.
                //
                for (i = 0; (i < dwEntriesRead); i++)
                {
                   assert(pTmpBuf != NULL);               if (pTmpBuf == NULL)
                   {
                      fprintf(stderr, "An access violation has occurred\n");
                      break;
                   }
                   //
                   // Print the retrieved data. 
                   //
                   wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname);
                   wprintf(L"\tUser:   %s\n", pTmpBuf->sesi10_username);
                   printf("\tActive: %d\n", pTmpBuf->sesi10_time);
                   printf("\tIdle:   %d\n", pTmpBuf->sesi10_idle_time);               pTmpBuf++;
                   dwTotalCount++;
                }
             }
          }
          //
          // Otherwise, indicate a system error.
          //
          else
             fprintf(stderr, "A system error has occurred: %d\n", nStatus);
          //
          // Free the allocated memory.
          //
          if (pBuf != NULL)
          {
             NetApiBufferFree(pBuf);
             pBuf = NULL;
          }
       }
       // 
       // Continue to call NetSessionEnum while 
       //  there are more entries. 
       // 
       while (nStatus == ERROR_MORE_DATA); // end do   // Check again for an allocated buffer.
       //
       if (pBuf != NULL)
          NetApiBufferFree(pBuf);
       //
       // Print the final count of sessions enumerated.
       //
       fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);   return 0;
    }