python - ctypes c_char_p 作为 python 函数的输入

标签 python c dll ctypes

我正在为它编写一个 C DLL 和一个 python 脚本,如下所示:

//test.c
__declspec(dllexport) HRESULT datafromfile(char * filename)
{
  HRESULT errorCode = S_FALSE;

    if (pFileName == NULL)
    {
        return E_INVALIDARG;
    }

    FILE *fp = NULL;
    fopen_s(&fp, pFileName, "rb");

    if (fp == NULL)
        return E_FAIL;

some more lines of code are there....

我写的python脚本如下:

//test.py
import sys
import ctypes
from ctypes import *

def datafromfile(self,FileName):
    self.mydll=CDLL('test.dll')
    self.mydll.datafromfile.argtypes = [c_char_p]
    self.mydll.datafromfile.restypes = HRESULT
    self.mydll.datafromfile(FileName)

在调用 main 时,我将文件名指定为:

FileName = 'abcd.EDID'
datafromfile(FileName)

但是代码给出了一个错误,Windows Error: Unspecified Error。 任何人都可以帮助我如何将 c_char_p 传递给上面显示的函数吗?

最佳答案

我犯了一个非常愚蠢的错误,我将 edid 文件保存在其他文件夹中,并从其他文件夹运行脚本。我没有只给出文件名,而是给出了相对路径。

关于python - ctypes c_char_p 作为 python 函数的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39634225/

相关文章:

python - 将图像分组为一张图像

python - PyQt5 - 默认日期时间为 QDateTimeEdit

c - 在 Eclipse CDT 中调试启动时抛出 java.lang.IllegalArgumentException

为 JNI 编译 64 位 DLL

c# - 强烈命名第三方程序集后出现构建错误。

python - 为 Python 2.7 安装 Qt5 和 PyQt5

python - 多处理池示例(并行)比顺序处理慢。尝试理解Python中的池

c - 我无法读取 C 语言的文件 - 我的程序在运行时不断崩溃。有什么解决办法吗?

c# - 将所有 DLL 嵌入 win app 和 C#

c++ - 如何自动从未修饰的stdcall DLL生成导入库?