Python Ctypes:WindowsError:exception:调用C+时读取0x0000000000000400的访问冲突+;功能
在我的cpp文件中Python Ctypes:WindowsError:exception:调用C+时读取0x0000000000000400的访问冲突+;功能,python,c++,ctypes,Python,C++,Ctypes,在我的cpp文件中 extern "C" { Password obj; _declspec(dllexport) BOOL decrypt(const char *encryptedPassword, char *password, size_t *sizeOfThePasswordPtr) { return obj.decrypt(encryptedPassword, password, sizeOfThePasswordPtr); } }
extern "C" {
Password obj;
_declspec(dllexport) BOOL decrypt(const char *encryptedPassword, char *password, size_t *sizeOfThePasswordPtr)
{
return obj.decrypt(encryptedPassword, password, sizeOfThePasswordPtr);
}
}
在我的python文件中:
lib = ctypes.WinDLL(os.path.join(baseDir, "basicLib.dll"))
encryptedValue = ctypes.c_char_p('absfdxfd')
decryptedValue = ctypes.c_char_p()
size = ctypes.c_size_t(1024)
lib.decrypt.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t]
lib.decrypt(encryptedValue, decryptedValue, size)
调用函数时,我收到了以下错误:异常:访问冲突读取0x0000000000000400。问题是因为encryptedValue
参数
仅当我设置了
encryptedValue=ctypes.c\u char\u p()
时,它才起作用,但如果传入某个值,则会出现异常。请告诉我原因 您似乎没有分配任何内存来保存解密的值。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。
# WinDLL is for __stdcall functions. Use CDLL.
# This is most likely the cause of your exception because the parameters
# are marshalled on the stack incorrectly.
lib = ctypes.CDLL(os.path.join(baseDir, "basicLib.dll"))
# No need to explicitly create c_char_p objects if you declare argtypes,
# but make sure it is a byte string if using Python 3
encryptedValue = b'absfdxfd'
# Create a writable buffer for the output.
decryptedValue = ctypes.create_string_buffer(1024)
# Good
lib.decrypt.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t]
# No need to declare explicit c_size_t() object either.
lib.decrypt(encryptedValue, decryptedValue, len(decryptedValue))