下面是Msdn的一个程序,用来枚举会话信息
他的ServerName是参数argv[1]输入的,我现在只要枚举本机的信息,"\\127.0.0.1",所以不需要这个输入参数了,但是我在函数里面直接赋值,老是出错,编译只出一个警告,但是运行出错,请教大虾们怎么才能实现?#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 = 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))
      {
  char s[200];
  fprintf(stderr, "count:");
  fprintf(stderr,itoa(dwEntriesRead,s,100));
  fprintf(stderr, "status:");
  fprintf(stderr,itoa(nStatus,s,100));
         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;
}

解决方案 »

  1.   

    pszServerName = L"\\127.0.0.1";
      

  2.   

    警告是没了,但是还是出错阿,而我直击用 xxx \\127.0.0.1就可以,很奇怪
      

  3.   

    pszServerName = _T("\\127.0.0.1");
      

  4.   

    那你需要pszServerName = _T("\\\\127.0.0.1");\是转义字符,看看对不对?
      

  5.   

    應該試試: pszServerName = _T("\\\\127.0.0.1");
      

  6.   

    if (argc == 1)
      pszServerName = _T("\\127.0.0.1");
    if (argc >= 2)
      pszServerName = argv[1];
    if (argc >= 3)
      pszClientName = argv[2];
    if (argc == 4)
      pszUserName = argv[3];
      

  7.   

    不对啊,如果用xx \\127.0.0.1返回正确的值,如果采用_T,总是返回A system error has occurred: 53
      

  8.   

    出错阿
     error C2440: '=' : cannot convert from 'unsigned short [11]' to 'char *'
    好像两个都不行啊
    需要Include什么东西吗?
      

  9.   

    #ifndef UNICODE
    #define UNICODE
    #endif
    这个预编译头你有吗?