我有一个程序可以从linux加载.so文件,并且可以正常运行。 现在,我正在尝试使程序跨平台。经过一段时间的努力后,我设法编译了一个dll文件以支持Windows,但是当我尝试从ctypes加载时,出现此错误:
“ WindowsError:异常:写入0x0000000000000000的访问冲突”
似乎甚至无法正确地将参数传递给我的c函数。我想我在将C代码转换为Windows dll时可能犯了一些错误,或者我的python代码可能需要更多工作才能正确加载dll并在Windows中使用它。我熟悉python,但对ctypes和 C 都是新手。我试图搜索丢失的内容,但找不到答案。 :(
我已经尝试了几件事,并且发现了错误发生的地方,但是仍然不知道如何解决。因此,当dll函数尝试调用另一个dll函数时,会发生我的问题。我已经更新了代码以包含该部分。
我已经检查了c代码内的另一个dll(“ mylib.dll”)调用是否可以通过在主要函数内调用initfunc正常工作(在另一个具有相同调用约定的c代码内)。 .dll”没有问题。我想如果要从dll函数内部调用dll函数,可能还要做更多的事情?
下面是我的Linux和Windows的C代码以及如何在python中调用它们。
我已经编辑了我的代码,因此这将是Antti建议的最小,完整和可验证的示例。我对Stack Overflow还是很陌生,一开始不了解“最小,完整和可验证的示例”的含义。感谢您的建议,对我的无知表示歉意。现在,我可以在下面的代码中重现相同的问题。
//header param_header.h
typedef struct MYSTRUCT MYSTRUCT;
struct MYSTRUCT
{
double param1;
double param2;
};
//mylib.c this was compiled as an .so(gcc mylib.c -fPIC -shared -o mylib.so) and .dll
#include "param_header.h"
#include <stdio.h>
#ifdef __linux__
int update_param(char *pstruct, char *paramname, double param)
#else
__declspec(dllexport) int update_param(char *pstruct, char *paramname, double param)
#endif
{
printf("Print this if function runs");
return 0;
}
//my_c_code.c --> this compiled again an .so & .dll and called by python ctypes
#include "param_header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __linux__
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#ifdef __linux__
MYSTRUCT *initfunc(flag, number, params, paramnames)
#else
__declspec(dllexport) MYSTRUCT *initfunc(flag, number, params, paramnames)
#endif
int flag;
int number;
double params[100];
char *paramnames[100];
{
int index;
int check;
MYSTRUCT *pstruct=(MYSTRUCT *)malloc(sizeof(MYSTRUCT));
memset(pstruct,0,sizeof(MYSTRUCT));
#ifdef __linux__
void *pHandle;
pHandle=dlopen("./mylib.so",RTLD_LAZY);
int(*update_param)(char*, char*, double) = dlsym(pHandle, "update_param");
#else
HINSTANCE pHandle;
pHandle=LoadLibrary("./mylib.dll");
int(__cdecl *update_param)(char*,char*, double);
FARPROC updateparam = GetProcAddress(pHandle, "update_param");
if (!updateparam)
{
check = GetLastError();
printf("%d\n", check);
}
update_param = (int(__cdecl *)(char*, char*, double))updateparam;
#endif
for (index=0;index < number;index++) {
(*update_param)((char*)pstruct, paramnames[index],params[index]); // <--this line fails only for the windows.
}
return pstruct;
}
下面是我访问该函数的python代码。
//mystruct.py
from ctypes import *
class MYSTRUCT(Structure):
_fields_ = [("param1",c_double),
("param2",c_double)]
//mypython code
from ctypes import *
from mystruct import *
mydll=cdll.LoadLibrary("./my_c_code.so")#"./my_c_code.dll" for windows.
libhandle=mydll._handle
c_initfunc=mydll.initfunc
c_initfunc.restype=POINTER(MYSTRUCT)
c_initfunc.argtypes=[c_int,c_int,c_double*100,c_char_p*100]
import numpy as np
param_dict={"a":1,"b":2}
params=(c_double * 100)(*np.float_(param_dict.values()))
paramnames=(c_char_p * 100)(*param_dict.keys())
flag=c_int(1)
number=c_int(len(param_dict.values()))
out=c_initfunc(flag, number, params, paramnames) <-- Error here.
我不确定这是否足够调试...但是结合了以上python代码和Linux c代码编译的“ .so”文件。我没有任何问题..但是我得到了dll大小写的错误。任何想法将不胜感激。
答案 0 :(得分:1)
修复了( Python )代码中的2个错误后,我能够成功运行它。与其猜测可能是什么错误(我仍然认为是 .dll 的问题,可能是由于命名错误),我还是采用了另一种方法来重构您的代码。
我想指出的一件事是 ctypes 页面:[Python 3]: ctypes - A foreign function library for Python。
header.h :
#if defined(_WIN32)
#define GENERIC_API __declspec(dllexport)
#else
#define GENERIC_API
#endif
#define PRINT_MSG_0() printf("From C - [%s] (%d) - [%s]\n", __FILE__, __LINE__, __FUNCTION__)
typedef struct STRUCT_ {
double param1;
double param2;
} STRUCT;
dll0.c :
#include "header.h"
#include <stdio.h>
#define DLL0_API GENERIC_API
DLL0_API int updateParam(char *pstruct, char *paramname, double param) {
PRINT_MSG_0();
return 0;
}
dll1.c :
#include "header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#define DLL1_API GENERIC_API
#define UPDATE_PARAM_FUNC_NAME "updateParam"
typedef int(__cdecl *UpdateParamFuncPtr)(char*, char*, double);
DLL1_API STRUCT *initFunc(flag, number, params, paramnames)
int flag;
int number;
double params[100];
char *paramnames[100];
{
int index = 0;
UpdateParamFuncPtr updateParam = NULL;
STRUCT *pStruct = (STRUCT*)malloc(sizeof(STRUCT));
memset(pStruct, 0, sizeof(STRUCT));
#if defined(_WIN32)
HMODULE pHandle = LoadLibrary("./dll0.dll");
if (!pHandle) {
printf("LoadLibrary failed: %d\n", GetLastError());
return NULL;
}
updateParam = (UpdateParamFuncPtr)GetProcAddress(pHandle, UPDATE_PARAM_FUNC_NAME);
if (!updateParam) {
printf("GetProcAddress failed: %d\n", GetLastError());
FreeLibrary(pHandle);
return NULL;
}
#else
void *pHandle = dlopen("./dll0.so", RTLD_LAZY);
if (!pHandle) {
printf("dlopen failed: %s\n", dlerror());
return NULL;
}
updateParam = dlsym(pHandle, UPDATE_PARAM_FUNC_NAME);
if (!updateParam) {
printf("dlsym failed: %s\n", dlerror());
dlclose(pHandle);
return NULL;
}
#endif
PRINT_MSG_0();
for (index = 0; index < number; index++) {
(*updateParam)((char*)pStruct, paramnames[index], params[index]);
}
#if defined(_WIN32)
FreeLibrary(pHandle);
#else
dlclose(pHandle);
#endif
return pStruct;
}
DLL1_API void freeStruct(STRUCT *pStruct) {
free(pStruct);
}
code.py :
#!/usr/bin/env python3
import sys
import traceback
from ctypes import c_int, c_double, c_char_p, \
Structure, CDLL, POINTER
class Struct(Structure):
_fields_ = [
("param1", c_double),
("param2", c_double),
]
StructPtr = POINTER(Struct)
DoubleArray100 = c_double * 100
CharPArray100 = c_char_p * 100
def main():
dll1_dll = CDLL("./dll1.dll")
init_func_func = dll1_dll.initFunc
init_func_func.argtypes = [c_int, c_int, DoubleArray100, CharPArray100]
init_func_func.restype = StructPtr
free_struct_func = dll1_dll.freeStruct
free_struct_func.argtypes = [StructPtr]
param_dict = {
b"a": 1,
b"b": 2,
}
params = DoubleArray100(*param_dict.values())
paramnames = CharPArray100(*param_dict.keys())
flag = 1
number = len(param_dict)
out = init_func_func(flag, number, params, paramnames)
print(out)
try:
struct_obj = out.contents
for field_name, _ in struct_obj._fields_:
print(" {:s}: {:}".format(field_name, getattr(struct_obj, field_name)))
except:
traceback.print_exc()
finally:
free_struct_func(out)
print("Done.")
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
注释:
ctypes.c_char_p
(因为我使用的是 Python > 3 )输出:
(py35x64_test) e:\Work\Dev\StackOverflow\q053909121>"c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64 (py35x64_test) e:\Work\Dev\StackOverflow\q053909121>dir /b code.py dll0.c dll1.c header.h original_code_dir (py35x64_test) e:\Work\Dev\StackOverflow\q053909121>cl /nologo /DDLL /MD dll0.c /link /NOLOGO /DLL /OUT:dll0.dll dll0.c Creating library dll0.lib and object dll0.exp (py35x64_test) e:\Work\Dev\StackOverflow\q053909121>cl /nologo /DDLL /MD dll1.c /link /NOLOGO /DLL /OUT:dll1.dll dll1.c Creating library dll1.lib and object dll1.exp (py35x64_test) e:\Work\Dev\StackOverflow\q053909121>dir /b code.py dll0.c dll0.dll dll0.exp dll0.lib dll0.obj dll1.c dll1.dll dll1.exp dll1.lib dll1.obj header.h original_code_dir (py35x64_test) e:\Work\Dev\StackOverflow\q053909121>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 From C - [dll1.c] (56) - [initFunc] From C - [dll0.c] (9) - [updateParam] From C - [dll0.c] (9) - [updateParam] <__main__.LP_STRUCT object at 0x000001B2D3AA80C8> param1: 0.0 param2: 0.0 Done.
Ho!哈! ! :)