title: wtsapi32.dll : WTSEnumerateSessions API sets 'pCount' to 0 intermittently
body: <p>I am using <code>WTSEnumerateSessions</code> API to get User sessions on RDP. </p>
  
  <p>I am taking reference of <a href="https://www.pinvoke.net/default.aspx/wtsapi32.wtsenumeratesessions" rel="nofollow noreferrer">this</a> and code looks like below,</p>
  
  <pre><code>    IntPtr ppSessionInfo = IntPtr.Zero;
  
      Int32 pCount = 0;
      Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref pCount);
      Log.Write(pCount.ToString());
      Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
  
      Int64 current = (int)ppSessionInfo;
  
      if (retval != 0)
      {
          for (int i = 0; i &lt; pCount; i++)
          {
             WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
             current += dataSize;
  
             ret.Add(si.SessionID + " " + si.State + " " + si.pWinStationName);
          }
  
          WTSFreeMemory(ppSessionInfo);
      }
  </code></pre>
  
  <p>It works fine in most of the case, but on specific machine, logic fails because it gets <code>pCount</code> 0. Here my question is, In which scenarios <code>WTSEnumerateSessions</code> sets <code>pCount</code> to 0?</p>
  
  <p>Note: this code runs into System user and while running machine can be locked or disconnected or logged off.</p>
  
  <p>I have explicitly added logs to get the value of count, and it was 0. </p>