【发布时间】:2016-08-18 13:02:59
【问题描述】:
我正在编写一个 DLL,该 DLL 使用如下 Python 脚本调用:
//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:\Python27\pythonw.exe
File:minkernel\crts\ucrt\src\appcrt\stdio\fgets.cpp
Expression:stream.valid()
谁能帮我解决这个问题?还有我写的那个python脚本,是不是正确的写法?
【问题讨论】:
-
该错误表明使用
pythonw.exe不会创建控制台。fgets()未显示在您的代码中,但如果它试图从stdin读取,那将是一个没有控制台的无效流。 -
我注意到的另一件事是
data xdata是getinfo的参数。该类型在 Python 中不是POINTER(data),而只是data。 -
您还从 Python
get()返回了两个值,所以xdata,error = get()而不是xdata=get()和error=get()。