title: Convert PKCS'11 Object handle to X509Certificate Object in C#
body: <p>How can i convert the Object_Handle which is a ulong returned by C_FindObject to a X509Certificate object in C#.  Here is the code .</p>
  
  <pre><code>ulong[] foundObjectIds = new ulong[10];
  foundObjectIds[0] = CK_INVALID_HANDLE;
  success = PKCS11CsharpWrapper.C_FindObjects(session, foundObjectIds, Convert.ToUInt64(foundObjectIds.Length), ref foundObjectCount);
  </code></pre>
  
  <p>Now i have to convert foundObjectIds[0]  to a X509Certificate object . </p>
  
  <p>I tried the below way and it doesn't work for me .</p>
  
  <pre><code>IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ulong)));
  Marshal.StructureToPtr(foundObjectIds[0], ptr, false);
  IntPtr[] arr = new IntPtr[2];
  Marshal.Copy(ptr, arr, 0, 1);
  X509Certificate2 cert= new X509Certificate2((IntPtr)foundObjectIds[0]);
  </code></pre>