问题
I am developing an application that will access smart cards using the standard PKCS#11. At this moment the application is working very well both on Ubuntu and OS X. Now I am porting it to Windows, but I am getting an "access violation" exception whenever I call functions from the pkcs#11 library, which is linked at runtime.
Below I tried to reproduce a SSCCE of my code (The place where the exception is happening is identified with a comment).
void * libraryHandle = NULL;
CK_RV rv;
CK_C_GetFunctionList pC_GetFunctionList;
CK_FUNCTION_LIST_PTR functions;
libraryHandle = LoadLibrary(L"C:\\WINDOWS\\system32\\pteidpkcs11.dll");
if (libraryHandle == NULL)
{
printf("Library not loaded\n");
exit(1);
}
pC_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress((HMODULE)libraryHandle, "C_GetFunctionList");
if (pC_GetFunctionList == NULL)
{
printf("Function not loaded\n");
FreeLibrary((HMODULE)libraryHandle);
exit(1);
}
rv = (*pC_GetFunctionList) (&functions);
assert(rv == CKR_OK);
printf("Point A\n");
if(functions == NULL)
{
printf("Functions not loaded\n");
FreeLibrary((HMODULE)libraryHandle);
exit(1);
}
printf("%u - %u\n",functions->version.major, functions->version.minor); // Prints without problems
rv = (*functions->C_Initialize) (NULL_PTR); //THIS IS THE PLACE WHERE I AM GETTING THE ACCESS VIOLATION
assert(rv == CKR_OK);
//printf("Point B\n");
FreeLibrary((HMODULE)libraryHandle);
When I debug the application the structure "CK_FUNCTION_LIST_PTR functions" seems to be valid.
Does anyone know what is causing this exception?
I am using Visual Studio 2010 Ultimate and Windows XP SP3.
Thanks!
(PS: I have already tried to load the function "C_Initialize" using "GetProcAddress" from the library, and it worked)
--- Edit
CK_FUNCTION_LIST definition
struct CK_FUNCTION_LIST {
CK_VERSION version; /* Cryptoki version */
/* Pile all the function pointers into the CK_FUNCTION_LIST. */
/* pkcs11f.h has all the information about the Cryptoki
* function prototypes. */
#include "pkcs11f.h"
};
Full headers in: http://www.rsa.com/rsalabs/node.asp?id=2133
回答1:
From that image, it looks like you either have some sort of disagreement on the layout of your CK_FUNCTION_LIST_PTR
structure between the executable and the DLL. Make sure the executable and DLL are both compiled with the same compiler settings etc.
What is the definition (including any surrounding pragmas/macros) of CK_FUNCTION_LIST_PTR
? Is sizeof(CK_FUNCTION_LIST_PTR)
the same if you print out its value from both your executable and from inside the DLL (in, say, C_GetFunctionList()
)?
来源:https://stackoverflow.com/questions/11993429/c-access-violation-calling-function-from-dll