通过使用 ctypes 调用从 C 函数返回的意外整数
unexpected integer returned from a C function by calling it using ctypes
我编写了以下 C 函数:
unsigned long long int myfunction(unsigned long long int number)
{
return number;
}
创建共享库后,我想从 python:
调用它
from pathlib import Path
from ctypes import CDLL, c_ulonglong
cwd = Path.cwd()
so_file = Path.joinpath(cwd, Path("mylib.so"))
c_lib = CDLL(so_file)
c_func = c_lib.myfunction
if __name__ == '__main__':
my_number = 844952973115541
my_c_number = c_ulonglong(my_number)
ans = c_ulonglong(c_func(my_c_number))
print(ans.value)
我希望取回我给的号码,但是下面的号码returns:
18446744073471557781
他们python代码假定c函数returns是一个int。您可以通过添加来修复它:
c_func.restype = c_ulonglong
我编写了以下 C 函数:
unsigned long long int myfunction(unsigned long long int number)
{
return number;
}
创建共享库后,我想从 python:
调用它from pathlib import Path
from ctypes import CDLL, c_ulonglong
cwd = Path.cwd()
so_file = Path.joinpath(cwd, Path("mylib.so"))
c_lib = CDLL(so_file)
c_func = c_lib.myfunction
if __name__ == '__main__':
my_number = 844952973115541
my_c_number = c_ulonglong(my_number)
ans = c_ulonglong(c_func(my_c_number))
print(ans.value)
我希望取回我给的号码,但是下面的号码returns:
18446744073471557781
他们python代码假定c函数returns是一个int。您可以通过添加来修复它:
c_func.restype = c_ulonglong