C ++访问违规调用函数从dll [英] C++ Access violation calling function from dll

查看:269
本文介绍了C ++访问违规调用函数从dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用标准PKCS#11访问智能卡的应用程序。在这个时候,应用程序在Ubuntu和OS X上都运行得很好。现在我将其移植到Windows,但是当我从pkcs#11库中调用函数时,我正在获得一个访问冲突异常,该库在运行时链接



下面我试图重现我的代码的SSCCE(异常发生的地方用一个注释标识)。

  void * libraryHandle = NULL; 
CK_RV rv;
CK_C_GetFunctionList pC_GetFunctionList;
CK_FUNCTION_LIST_PTR函数;


libraryHandle = LoadLibrary(LC:\\WINDOWS\\\system32\\\\pteidpkcs11.dll);
if(libraryHandle == NULL)
{
printf(Library not loaded\\\
);
exit(1);
}

pC_GetFunctionList =(CK_C_GetFunctionList)GetProcAddress((HMODULE)libraryHandle,C_GetFunctionList);

if(pC_GetFunctionList == NULL)
{
printf(function not loaded\\\
);
FreeLibrary((HMODULE)libraryHandle);
exit(1);
}

rv =(* pC_GetFunctionList)(& functions);
assert(rv == CKR_OK);
printf(Point A\\\
);

if(functions == NULL)
{
printf(functions not loaded\\\
);
FreeLibrary((HMODULE)libraryHandle);
exit(1);
}

printf(%u - %u\\\
,functions-> version.major,functions-> version.minor); // Prints without problems
rv =(* functions-> C_Initialize)(NULL_PTR); //这是我获得访问权限的地方
assert(rv == CKR_OK);

// printf(Point B\\\
);

FreeLibrary((HMODULE)libraryHandle);

当我调试应用程序时,结构CK_FUNCTION_LIST_PTR函数似乎是有效的。



有谁知道导致这个异常的原因?



我使用的是Visual Studio 2010 Ultimate和Windows XP SP3。

谢谢!



(PS:我已经尝试使用库中的GetProcAddress加载函数C_Initialize并且它的工作)



---编辑



CK_FUNCTION_LIST定义

  struct CK_FUNCTION_LIST {

CK_VERSION version; / * Cryptoki版本* /

/ *将所有函数指针插入到CK_FUNCTION_LIST中。 * /
/ * pkcs11f.h具有关于Cryptoki
*函数原型的所有信息。 * /
#includepkcs11f.h

};

完整标题:
http://www.rsa.com/rsalabs/node.asp?id=2133

解决方案

从该图像中,您可能会对 CK_FUNCTION_LIST_PTR 结构的布局有某种不一致在可执行文件和DLL之间。确保可执行文件和DLL都使用相同的编译器设置等进行编译。



的定义(包括任何周围的pragmas /宏) CK_FUNCTION_LIST_PTR ?如果您从可执行文件和DLL内部打印出它的值(在 C_GetFunctionList() 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆