C代码(DLL)
#include <math.h>
#include <wchar.h>
#include <stdlib.h>
struct Doc
{
wchar_t path[512];
int r;
int g;
int b;
};
struct Docs
{
struct Doc docs[1000000];
};
struct Color
{
int r;
int g;
int b;
};
float distance(int a, int b, int c, int d, int e, int f)
{
return sqrt((a-b)*(a-b)+(c-d)*(c-d)+(e-f)*(e-f));
}
struct Doc main(long len, struct Color c, struct Docs ds)
{
long near = 1000000;
long l;
struct Doc f_d;
for (l = 0; l < len; l++){
struct Doc d = ds.docs[l];
float dist = distance(c.r, d.r, c.g, d.g, c.b, d.b);
if (dist < near){
near = dist;
f_d = d;
}
}
return f_d;
};
Python代码
class Doc(ctypes.Structure):
_fields_ = [("path", ctypes.c_wchar_p),
("r", ctypes.c_int),
("g", ctypes.c_int),
("b", ctypes.c_int)]
class Docs(ctypes.Structure):
_fields_ = [("docs", Doc * 1000000)]
class Color(ctypes.Structure):
_fields_ = [("r", ctypes.c_int),
("g", ctypes.c_int),
("b", ctypes.c_int)]
structure = ctypes.CDLL('st.dll').main
structure.restype = Doc
structure.argstypes = (ctypes.c_long, Color(), Docs(),)
从python调用main函数后,它会退出,退出代码为-1073741819
exe
执行后返回"Process finished with exit code -1073741819 (0xC0000005)"
我认为这是因为内存分配或类似的事情。