我目前正在尝试将 ctypes 接口写入 .dll 文件。抱歉,这是我无法访问 dll 源代码的情况之一,但是我在下面包含了制造商提供的头文件和 C 代码中的代码。
我遇到的错误是
Traceback (most recent call last):
File "C:\Users\---\Desktop\LLTF\SDK1.2.1\win64\dllInterface.py", line 81, in <module>
print(GetSystemCount(my_handle))
File "C:\Users\---\Desktop\LLTF\SDK1.2.1\win64\dllInterface.py", line 49, in GetSystemCount
return pe_lib.PE_GetSystemCount(PEHandle)
OSError: exception: access violation reading 0x0000003000350036
当我取出 GetSystemCount(my_handle) 时,没有问题,其他两个函数都没有错误代码返回。对于上下文,我在 64it windows + python 中运行 64 位版本的 dll。
如果您对此有任何建议,我将非常感激,我相信这一定与使用 ctypes 变量存储我的句柄信息有关。我认为
Create
函数填充句柄 - 但可能参数类型错误或 Ctypes 无法处理这种情况?
import ctypes
import traceback
pe_lib = ctypes.cdll.LoadLibrary('exampleDll.dll')
# Create PE handle
pe_lib.PE_Create.argtypes = [c_char_p, POINTER(c_void_p)]
pe_lib.PE_Create.restype = c_int
def Create(configFile, PEHandle):
res = pe_lib.PE_Create(configFile.encode(), PEHandle)
return res
# Destroy PE handle
pe_lib.PE_Destroy.argtypes = [c_void_p]
pe_lib.PE_Destroy.restype = c_int
def Destroy(PEHandle):
return pe_lib.PE_Destroy(PEHandle)
# Get number of connected systems
pe_lib.PE_GetSystemCount.argtypes = [c_void_p]
pe_lib.PE_GetSystemCount.restype = c_int
def GetSystemCount(PEHandle):
return pe_lib.PE_GetSystemCount(PEHandle)
try:
my_handle = c_void_p()
Create('system.xml', byref(my_handle))
GetSystemCount(my_handle)
print(Destroy(my_handle))
except Exception as e:
print(traceback.format_exc())
print(Destroy(my_handle))
typedef void* PE_HANDLE;
typedef const void* CPE_HANDLE;
#ifdef __cplusplus
extern "C" {
#endif
//=============================================================================
// MANAGEMENT FUNCTIONS
//=============================================================================
//! Creates filter resource with configuration file but do not connect to filter.
//! @param[in] conffile Full path to the XML configuration file
//! @param[out] peHandle Handle to the resource created
//! @return Error code
PEFILTERSDK_API PE_STATUS PE_Create(const char* conffile, PE_HANDLE* peHandle);
//! Destroys filter resource previously created with PE_Create().
//! @param[in] peHandle Handle to the resource
//! @return Error code
PEFILTERSDK_API PE_STATUS PE_Destroy(PE_HANDLE peHandle);
//! Gets the number of systems available in the configuration file.
//! @param[in] peHandle Handle to the resource
//! @return Number of configured systems
PEFILTERSDK_API int PE_GetSystemCount(CPE_HANDLE peHandle);
// EXAMPLE C CODE FOR RUNNING SYSTEM
PE_HANDLE handle = NULL;
char systemName[256];
/* Please note that error checks are omitted for clarity reasons */
PE_Create("system.xml", &handle);
/* Retrieve the number of systems available */
PE_GetSystemCount(handle);