通过Windows上的ctypes将文件描述符传递给C库函数 [英] passing a file descriptor to a C library function through ctypes on windows

查看:98
本文介绍了通过Windows上的ctypes将文件描述符传递给C库函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文件描述符通过ctypes传递给C函数,在f函数上执行写操作。在linux上它可以工作。在Windows上没有,我也不知道为什么(我没有在Windows上作为开发人员的经验)

  // C函数签名:
void fun(struct bah * opaque,int fd)

来自python (省略详细信息):

  mylib.fun.argtypes = [POINTER(bah),c_int] 
fh =打开(文件名,'wb')
#在Windows上不起作用,在linux / unix上工作
mylib.fun(some_ctypes_struct,fh.fileno())
#不起作用Windows
mylib.fun(bah_struct,ctypes.cdll.msvcrt._open(文件名,_O_FLAGS_MASK,ACCMASK)
#不起作用
mylib.fun(bah_struct,os.open(.. ))

程序在断言_osfile(fh)和FOPEN失败的write()上死掉



cl.exe:x86
python 2.7.2 msc v.1500 32bit的16.00.40219.01



我应该怎么做呢?不,我不想将open()卸载到lib中,我想以一种安全的方式传递已经打开的文件描述符,与平台无关。






其他信息,以防万一:
该库是tinycdb,我将其快速移植到具有简短cmake规范的Windows上使getopt和dll导出工作的肮脏补丁。该库和exe工具按预期方式工作(经过测试)。 tinycdb的python ctypes包装器可以在Linux上正常工作。窗户让我眼前一亮。即使我用自己的(msvcrt)_open libcall打开它之后,他也不会接受fd是有效的描述符。






当然,如果我在库中打开(打开)/关闭(关闭)文件,则一切正常,但是我无力更改API。


I am trying to pass a file descriptor through ctypes, to a C function where writes are performed on the fd. On linux it works. On windows it doesn't and I don't understand why (I have no experience as a developer on windows)

//C func signature: 
void fun(struct bah *opaque, int fd)

from python (details ommited):

mylib.fun.argtypes = [POINTER(bah), c_int]
fh = open(filename,'wb')
#doesn't work on windows, works on linux/unix
mylib.fun(some_ctypes_struct, fh.fileno())
#doesn't work on windows
mylib.fun(bah_struct, ctypes.cdll.msvcrt._open(filename,_O_FLAGS_MASK, ACCMASK)
#doesn't work
mylib.fun(bah_struct, os.open(...))

program dies on write()s with a failed assertion _osfile(fh) & FOPEN

cl.exe: 16.00.40219.01 for x86 python 2.7.2 msc v.1500 32bit

how am I supposed to do it? no, I don't want to offload open() to lib. I want to pass an already open file descriptor in a safe manner, platform independent.


additional information, just in case: the library is tinycdb, I ported it quickly to windows with a short cmake spec and few dirty patches to make getopt and dll exports work. the library and the exe tool works as expected (tested). the python ctypes wrappers for tinycdb works on linux as expected. windows gives me eyeballs. he wont accept the fd is a valid descriptor even if I'm passing it after opening it with its own (msvcrt) _open libcall.


ofcourse, everything works if I'm open()ing/close()ing the file within the library but I can't afford to change the API.

解决方案

Windows doesn't use file descriptions like Unix does, so I'm hypothesizing that file descriptors are emulated by the C runtime. If you are using two different C runtimes (for example, if your EXE and DLL are compiled by different compilers, or with the same compiler but with different options), then each runtime will have its own "file descriptor emulation" and you can't pass a descriptor from one to the other.

这篇关于通过Windows上的ctypes将文件描述符传递给C库函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆