问题描述
我正在开发一个使用标准PKCS#11访问智能卡的应用程序。在这个时候,应用程序在Ubuntu和OS X上都运行得很好。现在我将其移植到Windows,但是当我从pkcs#11库中调用函数时,我正在获得一个访问冲突异常,该库在运行时链接
下面我试图重现我的代码的SSCCE(异常发生的地方用一个注释标识)。
c> sizeof(CK_FUNCTION_LIST_PTR) / code>)?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
解决方案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())?
这篇关于C ++访问违规调用函数从dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!