简体   繁体   English

通过Windows上的ctypes将文件描述符传递给C库函数

[英]passing a file descriptor to a C library function through ctypes on windows

I am trying to pass a file descriptor through ctypes, to a C function where writes are performed on the fd. 我试图通过ctypes将文件描述符传递给C函数,在C函数中对fd执行写操作。 On linux it works. 在linux上它可以工作。 On windows it doesn't and I don't understand why (I have no experience as a developer on windows) 在Windows上它没有,我不明白为什么(我没有在Windows上作为开发人员的经验)

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

from python (details ommited): 来自python(详情无效):

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 程序在write()s上死亡,失败的断言_osfile(fh)&FOPEN

cl.exe: 16.00.40219.01 for x86 python 2.7.2 msc v.1500 32bit 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. 不,我不想将open()卸载到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. 附加信息,以防万一:库是tinycdb,我将它快速移植到具有短cmake规范的窗口和几个脏补丁,以使getopt和dll导出工作。 the library and the exe tool works as expected (tested). 库和exe工具按预期工作(测试)。 the python ctypes wrappers for tinycdb works on linux as expected. tinycdb的python ctypes包装器可以按预期在linux上运行。 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. 他不会接受fd是一个有效的描述符,即使我在用自己的(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. 当然,如果我打开()/关闭()文件库中的文件但是我无法更改API,一切都有效。

Windows doesn't use file descriptions like Unix does, so I'm hypothesizing that file descriptors are emulated by the C runtime. Windows不像Unix那样使用文件描述,所以我假设文件描述符是由C运行时模拟的。 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. 如果您使用两个不同的C运行时(例如,如果您的EXE和DLL由不同的编译器编译,或者使用相同的编译器但具有不同的选项),则每个运行时将具有其自己的“文件描述符模拟”并且您可以' t将描述符从一个传递给另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM