我有这样的事情:
import ctypes
dll = ctypes.CDLL('libc.so.6')
dll.malloc.argtypes = ctypes.c_size_t
dll.malloc.restype = ctypes.c_void_p
ptr: int = dll.malloc(100) # how do i write to this memory?
print(ptr) # gives a memory address
如何为该内存设置值?最好是ctypes.py_object。
我知道在Python中这样做是个坏主意,但我只是在尝试打破规则。
我有以下在linux下工作的代码。我想把它移植到Windows上
但我不知道该去哪里找。
import os
import sys
import ctypes
import ctypes.util
MAX_CHARS = 1000 # maximum number of characters to read
if __name__ == "__main__":
libc = ctypes.CDLL("libc.so.6") # LINUX
fd = libc.open(sys.argv[0], os.O_RDONLY)
buff
我对Linux操作系统没有多少经验。
我设法使用Python中的ctype来访问文件中的函数,使用以下代码:
import ctypes
dpfjDll = ctypes.CDLL("dpfj.dll")
.....
调用函数的测试在我的windows机器上非常有效,但是当我将应用程序部署到我们的linux服务器时,我得到了以下错误:
OSError: dpfj.dll: cannot open shared object file: No such file or directory
知道怎么回事吗?
更新:我尝试测试来临时设置文件的绝对路径,现在我收到了以下错误消息:
OS
我完全是C++的初学者,一直在尝试通过ctype使用DLL从C++中访问Python函数。当我运行我的代码时,我总是得到错误AttributeError: function 'my_function' not found。
Header.h
#pragma once
int my_function(void);
Source.cpp
#include "Header.h"
int my_function(void)
{
return(17); //test
}
ctypesTest.py
import ctypes
if __name__ == &
我制作了一个包含以下代码的dll:
#pragma once
extern double __declspec(dllexport) add(double a, double b);
extern double __declspec(dllexport) dif(double a, double b);
#include "testdll.h"
double add(double a, double b)
{
return a + b;
}
double dif(double a, double b)
{
return a - b;
}
调用add函数
我们希望在新的python应用程序中使用用matlab编写的函数。我们希望使用ctype,因为用户在他的机器上不需要matlab。我们正在测试此方法,但无法使其工作。我们缺乏c语言知识(还有更多……)。这是我们简单的测试matlab函数:
function [ z ] = adding( x,y )
z = x + y;
end
我们用matlab将其编译成一个共享库.dll。在python解释器中,我们有:
import ctypes
dl = ctypes.CDLL('adding.dll')
现在我们被卡住了,因为我们找不到在matlab中访问函数的命令。
我正在尝试编写在python中封装C库的代码。我计划使用CTypes来完成它,我使用visual来编译我的DLL。我从一个简单的函数开始,我在Visual中的一个标头中添加了以下内容,然后构建到一个DLL中
int our_function(int num_numbers, int* numbers) {
int i;
int sum = 0;
for (i = 0; i < num_numbers; i++) {
sum += numbers[i];
}
return sum;
}
我的python包装器如下
import cty