ctypes using HRESULT(python)
ctypes using HRESULT(python)
我正在编写一个使用python脚本调用的DLL,如下所示:
//sample.h
#include<stdio.h>
typedef struct _data
{
char * name;
}data,*xdata;
__declspec(dllexport) void getinfo(data xdata,HRESULT *error);
//sample.c
#include<stdio.h>
#include"sample.h"
void get(data xdata,HRESULT *error)
{
//something is being done here
}
现在,用于调用上述函数的python脚本如下所示:
//sample.py
import ctypes
import sys
from ctypes import *
mydll=CDLL('sample.dll')
class data(Structure):
_fields_ = [('name',c_char_p)]
def get():
xdata=data()
error=HRESULT()
mydll=CDLL('sample.dll')
mydll.get.argtypes=[POINTER(data),POINTER(HRESULT)]
mydll.get.restype = None
mydll.get(xdata,error)
return xdata.value,error.value
xdata=get()
error=get()
print "information=",xdata.value
print "error=", error.value
但是我在运行python脚本后得到的错误是:
Debug Assertion Failed!
Program:C:Python27pythonw.exe
File:minkernelcrtsucrtsrcappcrtstdiofgets.cpp
Expression:stream.valid()
谁能帮我解决这个问题?我写的python脚本,这样写对吗? 根据我的评论,我怀疑fgets()
的错误是在未显示的代码中,但所显示的Python和C代码也存在问题。下面是我使用的DLL源代码,确保传递了指向数据结构的指针:
typedef long HRESULT;
typedef struct _data {
char * name;
} data;
// Make sure to pass a pointer to data.
__declspec(dllexport) void getinfo(data* pdata, HRESULT *error)
{
pdata->name = "Mark";
*error = 0;
}
下面是修改后的Python代码:
from ctypes import *
class data(Structure):
_fields_ = [('name',c_char_p)]
def get():
xdata=data()
error=HRESULT()
mydll=CDLL('sample.dll')
mydll.getinfo.argtypes=[POINTER(data),POINTER(HRESULT)]
mydll.getinfo.restype = None
mydll.getinfo(xdata,error)
return xdata,error
# Correction in next two lines
xdata,error = get()
print "information =",xdata.name
print "error =", error.value
输出:information = Mark
error = 0
相关文章:
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 使用Pybind11向Python公开Eigen::张量
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 如何在C++中使用pybind11加载一个pickle python列表
- 如何在c++中使用system()来运行包含空格的python脚本
- python集合的C++等价物是什么.计数器
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 从python调用openMP共享库时,未定义opnMP函数
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 如何将真正的字符串从python c-api转换为python脚本
- ctypes using HRESULT(python)