Hi,
GenerateKeyPair function takes the public and private key template that is used by the underlying PKCS#11 to generate a key pair.
Each PKCS#11 module/token wants a well defined set of attributes in both templates. In the sample code you are using I put some of the attributes that works with most of PKCS#11 tokens, but, as may be in your case, sometime they don't fit the requirements of the underlying PKCS#11 module.
Try to change the attributes in the tamplates and refer to the RSA Secure ID ( SID 800 ) reference manual.
Regards,
Ugo Chirico