title: Help with CredEnumerate
body: <p>As a follow-up to <a href="https://stackoverflow.com/questions/199518/how-to-programatically-add-mapped-network-passwords-winxp">this</a> question I am hoping someone can help with the <a href="http://msdn.microsoft.com/en-us/library/aa374794(VS.85).aspx" rel="nofollow noreferrer">CredEnumerate</a> API.  </p>
  
  <p>As I understand from the documentation the PCREDENTIALS out parameter is a "pointer to an array of pointers to credentials". I am able to successfully call the CredEnumerate API using C# but I am not sure of how to convert the PCREDENTIALS into something useful (like a list of credentials).</p>
  
  <p>Edit: Here's the code I am using:</p>
  
  <pre><code>        int count = 0;
          IntPtr pCredentials = IntPtr.Zero;
          bool ret = false;
          ret = CredEnumerate(null, 0, out count, out pCredentials);
          if (ret != false)
          {
              IntPtr[] credentials = new IntPtr[count];
              IntPtr p = pCredentials;
              for (int i = 0; i &lt; count; i++)
              {
                  p = new IntPtr(p.ToInt32() + i);
                  credentials[i] = Marshal.ReadIntPtr(p);
              }
              List&lt;Credential&gt; creds = new List&lt;Credential&gt;(credentials.Length);
              foreach (IntPtr ptr in credentials)
              {
                  creds.Add((Credential)Marshal.PtrToStructure(ptr, typeof(Credential)));
              }
          }
  </code></pre>
  
  <p>Unfortunately, while this works for the first credential in the array—it gets generated and added to the list correctly—subsequent array items bomb at Marshal.PtrToStructure with the following error:</p>
  
  <p><em>Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</em></p>
  
  <p>Any ideas? Anyone? Bueller?</p>