12

对ctypes不太了解,最近才开始使用它。

我在类 C 的 dll 中有一个简单的函数,它返回指向动态生成的字符串的指针。
它工作正常,但是,因为我为字符串手动分配了内存,我应该在使用后释放它。

我有这样的事情:

extern "C" char* DLL_EXPORT func(const char* str1, const char* str2)
{
    return getSomeString(str1, str2);
}

// Goal is to call this function correctly from Python.    
extern "C" void DLL_EXPORT freeMem(void *mem)
    {
        if(mem!=NULL)
            delete mem;
    }

但我不知道,如何将收到的指针传回以在 Python 中删除?

4

3 回答 3

13

通常,您使用的每个函数ctypes都应声明其参数和返回类型,以便 Python 可以检查参数的正确数量和类型,并将 Python 对象参数转换为正确的 C 数据对象。不幸的是,在这种情况下,正常的返回值func应该是c_char_p,但会ctypes尝试提供帮助并将c_char_p返回值转换为 Python 字符串,从而无法访问原始 C 指针值。相反,您可以将返回类型声明为POINTER(c_char)并用于cast检索字符串值,从而使返回值成为LP_c_char可以释放的对象。

这是一个例子。请注意,声明正确.restype对于 64 位 Python 尤其重要,因为默认返回类型是c_int(32 位)并且可能会截断 64 位指针。此代码使用 32 位和 64 位版本进行了测试。

测试.c

#include <string.h>
#include <stdlib.h>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

API char* func(const char* str1, const char* str2) {
    size_t len = strlen(str1) + strlen(str2) + 1;
    char* tmp = malloc(len);
    strcpy_s(tmp, len, str1);
    strcat_s(tmp, len, str2);
    return tmp;
}

API void freeMem(void *mem) {
    free(mem);
}

测试.py

import ctypes as ct

dll = ct.CDLL('./test')
dll.func.argtypes = ct.c_char_p,ct.c_char_p
dll.func.restype = ct.POINTER(ct.c_char)
dll.freeMem.argtypes = ct.c_void_p,
dll.freeMem.restype = None

# Helper function to extract the return value as a Python object
# and always free the pointer.
def freeMem(a,b):
    p = dll.func(b'abcdef', b'ghijkl')
    print(p)
    s = ct.cast(p, ct.c_char_p).value
    dll.freeMem(p)
    return s

print(freeMem(b'abcdef', b'ghijkl'))

输出:

<ctypes.LP_c_char object at 0x00000279D7959DC0>
b'abcdefghijkl'
于 2012-12-17T06:46:55.037 回答
10

你在正确的轨道上。

// TestDLL.cpp
#include <string.h> // strcpy

extern "C" __declspec(dllexport) char* stringdup(const char* str) {
    char* p = new char[strlen(str)+1];
    strcpy(p,str);
    return p;
}

// if you have no good reason to use void*, use the type
// you've allocated. while it usually works for built-in
// types, it wouldn't work for classes (it wouldn't call
// the destructor)
extern "C" __declspec(dllexport) void stringfree(char* ptr) {
    // you don't need to check for 0 before you delete it,
    // but if you allocate with new[], free with delete[] !
    delete [] ptr; 
}

在python中:

# Test.py
import ctypes

lib = ctypes.cdll.TestDLL

# this creates a c-style char pointer, initialized with a string whose
# memory is managed by PYTHON! do not attempt to free it through the DLL!
cstr = ctypes.c_char_p("hello ctypes")

# call the dll function that returns a char pointer 
# whose memory is managed by the DLL.
p = lib.stringdup(cstr)

# p is just an integer containing the memory address of the 
# char array. therefore, this just prints the address:
print p

# this prints the actual string
print ctypes.c_char_p(p).value

# free the memory through the DLL
lib.stringfree(p)
于 2012-12-17T06:21:45.970 回答
0

为了我

#cdll.execute.restype = ctypes.c_char_p               # failed to free
cdll.execute.restype = ctypes.POINTER(ctypes.c_char)  # worked

p = cdll.execute('...')
print(
    ctypes.cast(p, ctypes.c_char_p).value.decode('utf-8')
)

cdll.free_pointer(p)
于 2021-02-18T09:35:46.033 回答