【问题标题】:Access Violation using Python Ctypes使用 Python Ctypes 的访问冲突
【发布时间】:2012-07-18 10:18:53
【问题描述】:

我正在尝试使用 Pythons ctypes 模块从 DLL 导入和使用函数,但我不断收到此错误:

Windows Error: exception: access violation writing 0x0000002C

我已经查看了关于此处类似主题的其他问题,但似乎没有一个能够提供有效的答案。 我目前的代码如下:

from ctypes import *

dll = "./WinlicenseSDK/WinlicenseSDK.dll"

mydll = cdll.LoadLibrary(dll)

name = c_char_p("A.N. Body")
org = c_char_p("ACME")
pcID = c_char_p("APC44567")
zero = c_int(0)
licenseKey = create_string_buffer("")    

mydll.WLGenLicenseFileKey(HASH, name, org, pcID, zero, zero, zero, zero, zero, licenseKey)

背景:我正在研究一款软件的许可技术。上述函数通过散列参数生成许可证密钥。

WLGenLicenseFileKey 的最后一个参数是生成的密钥写入的字符串缓冲区。

我尝试使用 mydll.WLGenLicenseFileKey.argtypes = ... 设置函数的 argtypes 但这不起作用,因为没有字符串缓冲区 ctypes 原始类型,因为没有字符串、整数、浮点数等。

谁能告诉我哪里出错了?

编辑:

C/C++函数定义:

int WLGenLicenseFileKeyW(    
wchar_t* pLicenseHash,     
wchar_t* pUserName,     
wchar_t* pOrganization,    
wchar_t* pCustomData,     
wchar_t* pMachineID,   
int NumDays,    
int NumExec,    
SYSTEMTIME* pExpirationDate,     
int CountryId,     
int Runtime,     
int GlobalTime,    
char* pBufferOut    
);

这就是文档提供的有关函数的所有信息。

【问题讨论】:

    标签: python ctypes


    【解决方案1】:

    您的 licenseKey 缓冲区的长度是一个字节,并且您没有传递 Unicode 字符串。我不在我的电脑前,但如果你的参数是正确的,我应该很接近。确保调用函数的 W 版本。您也不需要创建确切的类型,只要它们是整数和指针。

    buffer = create_string_buffer(REQUIRED_BUFSIZE)
    mydll.WLGenLicenseKeyW(u"A.N. Body", u"ACME", u"APC44567", None, None, 0, 0, None, 0, 0, 0, buffer)
    

    如果你确实想使用argtypes,那么这就是你想要的:

    mydll.WLGenLicenseKeyW.argtypes = [c_wchar_t,c_wchar_t,c_wchar_t,c_wchar_t,c_wchar_t,c_int,c_int,c_void_p,c_int,c_int,c_int,c_char_p]
    

    SYSTEMTIME 如果你想传递 NULL 以外的东西,也需要定义。

    编辑

    我找到了一些documentation。该函数使用 stdcall 调用约定,因此使用:

    mydll = WinDLL(dll)
    

    【讨论】:

    • 这个看起来不错。为什么要赏金?它有什么问题?
    • 悬赏后已将答案编辑为此表单,希望有机会尽快尝试此解决方案。
    猜你喜欢
    • 2012-05-02
    • 2012-01-05
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多