Search code examples
c#.netvisual-studiopkcs11interop

pkcs11interop Get error code from exception


I have two cards with different types of tokens. When calling GetTokenInfo() I get CKR_TOKEN_NOT_RECOGNIZED on some tokens. My question is, how do I retrieve this error code in a clean manner?

Snapshot of exception

HResult just indicates General Error. I could check for the exception in the Message string. The RV parameter is not part of the Exception class, not sure which derivative class to use to get this.


Solution

  • I had forgotten to add a using Net.Pkcs11Interop.Common;

    Now I am catching Pkcs11Exception and retrieving the error code through the RV parameter, and comparing it against PKCS#11 error code.

    Hope this can help others.