首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ctypes使用HRESULT(python)

ctypes使用HRESULT(python)
EN

Stack Overflow用户
提问于 2016-08-18 21:03:00
回答 1查看 839关注 0票数 0

我正在编写一个使用python脚本调用的DLL,如下所示:

代码语言:javascript
复制
 //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脚本如下所示:

代码语言:javascript
复制
//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脚本之后,我得到的错误是:

代码语言:javascript
复制
Debug Assertion Failed!
Program:C:\Python27\pythonw.exe
File:minkernel\crts\ucrt\src\appcrt\stdio\fgets.cpp
Expression:stream.valid()

有人能帮我解决这个问题吗?还有我写的python脚本,这是正确的编写方式吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-22 04:22:26

根据我的评论,我怀疑fgets()的错误存在于未显示的代码中,但显示的Python和C代码也存在问题。下面是我使用的DLL源代码,确保传递一个指向数据结构的指针:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
information = Mark
error = 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39018998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档