使用Python Ctypes违反访问 [英] Access Violation using Python Ctypes
问题描述
我正在尝试使用Pythons ctypes模块从DLL导入和使用函数,但是我一直收到此错误:
I am trying to import and use a function from a DLL using Pythons ctypes module, but I keep getting this error:
Windows Error: exception: access violation writing 0x0000002C
我在这里已经看过与类似主题有关的其他问题,但是似乎没有人能够提供有效的答案。
我当前的代码如下:
I've had a look at the other questions on similar topics on here, but none seem to be able to provide an answer that works. My current code is as follows:
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)
上下文:I正在研究一种软件的许可技术。上面的函数通过散列参数来生成许可证密钥。
Context: I'm investigating licensing techniques for a piece of software. The above function generates a license key from hashing the parameters.
WLGenLicenseFileKey的最后一个参数是将生成的密钥写入的字符串缓冲区。
The last parameter for the WLGenLicenseFileKey is a string buffer that the generated key is written to.
我尝试使用 mydll.WLGenLicenseFileKey.argtypes = ...
设置该函数的argtypes,但是由于存在
I tried setting the argtypes for the function with mydll.WLGenLicenseFileKey.argtypes = ...
but this won't work as there is not a string buffer ctypes raw type as there is for strings, ints, floats etc.
有人可以告诉我我要去哪里了吗?
Can anybody tell me where I am going wrong?
编辑:
C / C ++函数定义:
The C/C++ function definition:
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
);
这是文档提供的有关函数的所有信息。
That is all the information that the documentation gives on the function.
推荐答案
您的licenseKey缓冲区的长度为1个字节,并且您没有传递Unicode字符串。我不在电脑前,但是如果您的参数正确,那应该就近了。确保调用该函数的W版本。只要它们是整数和指针,就不需要创建确切的类型。
The length of your licenseKey buffer is one byte, and you are not passing Unicode strings. I'm not in front of my PC, but I this should be close assuming your parameters are otherwise correct. Make sure to call the W version of the function. You also don't need to create the exact types as long as they are ints and pointers.
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
,那么这就是您想要的:
If you do want to use argtypes
, then this is what you want:
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
编辑
我找到了一些文档。该函数使用stdcall调用约定,因此请使用:
I found some documentation. The function uses the stdcall calling convention, so use:
mydll = WinDLL(dll)
这篇关于使用Python Ctypes违反访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!