当前位置: 首页 > 面试题库 >

如何将指针传回ctypes?

曾阳飙
2023-03-14
问题内容

对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中删除?


问题答案:

您走在正确的轨道上。

// 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)


 类似资料:
  • 我正在尝试编写一个类模板,在内部它使用一个函数(BFGS优化的实现,由环境提供),接口如下: 其中< code>fn和< code>gr是类型的函数指针 和 分别是。我的< code>C 类模板如下所示: 这样,它可以由任何具有两个指定成员函数的类进行实例化,例如: 这可能吗?或者也许有更好的方法来做到这一点 - 将两个成员函数分别作为函数指针传递?(如果目标函数和相应的梯度很简单,那么编写两个函

  • 问题内容: 如果我连续两次使用上述代码,则该行不会被打印两次。 如何重置? 问题答案: 使用mysql_data_seek。此功能将允许您移动内部结果指针。

  • 问题内容: 背景:使用cgo从Golang调用C函数。 我想使用具有以下签名的C函数:。它会修改的数据和,这就是为什么它使用指针,它们的原因。的值是的长度;是一个字符串数组;返回值只是一个(布尔)指示符,用于指示是否存在错误。 在golang中,我可以使用来成功传递和修改。通过使用。示例代码如下: 如您所见,C函数当前为,但我想要的是。 这就是说:我真正想要的是在C中启用对字符串数组的修改(例如,

  • 问题内容: 在客户端,用户使用RecordRTC录制短视频。当用户按下上载时,我将使用来获取视频的数据,并将其上载到我的服务器(使用Rails和回形针处理文件上载)。 首先,我想将字段值更改为数据。事实证明,为了确保浏览器的安全性,我无法使用javascript对其进行更改。 然后,我尝试使用AJAX: 但是,它不起作用。 在档案上,我将得到以下内容,无法处理: 如果我正常使用提交,则参数将如下所

  • 当我构建这个应用程序时,我得到错误: 我也试过: 这将错误更改为: 下面是调用这个例程的类和我传递的回调函数的示例:

  • C ++允许您传递指向函数的指针。 为此,只需将函数参数声明为指针类型即可。 下面是一个简单的例子,我们将一个无符号长指针传递给一个函数并更改函数内部的值,该函数反映在调用函数中 - #include <iostream> #include <ctime> using namespace std; void getSeconds(unsigned long *par); int main () {

  • 指针可以指向一份普通类型的数据,例如 int、double、char 等,也可以指向一份指针类型的数据,例如 int *、double *、char * 等。 如果一个指针指向的是另外一个指针,我们就称它为 二级指针,或者 指向指针的指针。 假设有一个 int 类型的变量 a,p1是指向 a 的指针变量,p2 又是指向 p1 的指针变量,它们的关系如下图所示: 将这种关系转换为C语言代码: 指针变

  • 问题内容: 在Go中,如何将函数调用返回的值分配给指针? 考虑下面的示例,注意返回一个值(不是指针): 这些都失败了: 是否确实需要局部变量?那不会招致不必要的复制吗? 问题答案: 根据规范,必须使用局部变量。 要获取值的地址,调用函数必须将返回值复制到可寻址的内存中。有副本,但这不是多余的。 Go程序通常使用值。 有时在应用程序想要区分无值和其他时间值的情况下使用A。在SQL NULL和有效时间