title: PDF Signing With Pkcs11Interop
body: <p>I try to use Pkcs11Interop library for our institutional project. But problem is, when i try to get value from token card, "attempted to read or write protected memory. This is often an indication that other memory is corrupt" error is getting from Pkcs11Interop. I couldn't find any solution. Please help me, thank you in advance.</p>
  
  <p>Project is windows form application which is written with .Net Framework 4.5</p>
  
  <p><strong>Error:</strong> <code>system.accessviolationexception {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}</code></p>
  
  <p><strong>Error Stack Trace:</strong> </p>
  
  <pre><code> at Net.Pkcs11Interop.HighLevelAPI40.Session.GetAttributeValue(ObjectHandle objectHandle, List`1 attributes)
     at Net.Pkcs11Interop.HighLevelAPI40.Session.GetAttributeValue(ObjectHandle objectHandle, List`1 attributes)
     at EFinImza.Program.Main() in c:\HttpRoot\EFinImza\EFinImza\Program.cs:line 56
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p><strong>Code is like this:</strong></p>
  
  <pre><code>static void Main()
      {
          try
          {
              string pkcs11Library = @"C:\Windows\System32\akisp11.dll";
              using (var pkcs11 = new Net.Pkcs11Interop.HighLevelAPI40.Pkcs11(pkcs11Library, false, false))
              {
                  LibraryInfo info = pkcs11.GetInfo();
                  foreach (Slot slot in pkcs11.GetSlotList(false))
                  {
                      SlotInfo slotInfo = slot.GetSlotInfo();
                      if (slotInfo.SlotFlags.TokenPresent)
                      {
                          TokenInfo tokenInfo = slot.GetTokenInfo();
  
                          Session session = slot.OpenSession(false);
                          String pin = "*****";
                          session.Login(CKU.CKU_USER, pin);
  
                          // get all objects using empty ObjectAttributes list
                          List&lt;ObjectHandle&gt; handles = session.FindAllObjects(new List&lt;ObjectAttribute&gt;());
                          List&lt;CKA&gt; attrs = new List&lt;CKA&gt;();
                          attrs.Add(CKA.CKA_LABEL);
  
                          foreach (ObjectHandle handle in handles)
                          {
                              List&lt;ObjectAttribute&gt; oAttrs = session.GetAttributeValue(handle, attrs);   **//Error is getting here**
                          }
  
                          session.CloseSession();
                      }
                  }
  
                  pkcs11.Dispose();
              }
  
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
          }
          catch (Exception ex)
          {
              throw ex;
          }
  
      }
  </code></pre>