如何枚举当计算机的所有网络连接名,以及其状态???????????????
比如在WIN2K的下有两个网络连接,"本地连接"与"拨号连接",如何通过程序枚举这两个的连接的状态

解决方案 »

  1.   

    NetEnumSession函数
    ----------------
    #ifndef UNICODE
    #define UNICODE
    #endif#include <stdio.h>
    #include <assert.h>
    #include <windows.h> 
    #include <lm.h>
    #pragma comment(lib,"Netapi32.lib")int wmain(int argc, wchar_t *argv[])
    {
       LPSESSION_INFO_10 pBuf = NULL;
       LPSESSION_INFO_10 pTmpBuf;
       DWORD dwLevel = 10;
       DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
       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;
    }