Passing a file descriptor to a C library function via ctypes on windows

I am trying to pass a file descriptor through ctypes to a C function where the entries are done on fd. On linux, this works. In windows, this is not so, and I do not understand why (I have no experience working as a developer on windows)

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

from python (ommites details):

 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(...)) 

the program dies on writing () s with the failed statement _osfile (fh) and FOPEN

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

how should i do this? No, I do not want to unload open () in lib. I want to transfer an already open file descriptor in a secure manner, regardless of platform.


additional information, just in case: the library is tinycdb, I quickly put it in windows with a short specification of cmake and a few dirty patches to export getopt and dll. the library and the exe tool work as expected (verified). python ctypes shells for tinycdb run on Linux as expected. the windows give me eyeballs. he does not recognize that fd is a valid descriptor, even if I pass it after opening it with my own (msvcrt) _open libcall.


Of course, everything works if I open () ing / close () in a file in the library, but I cannot afford to change the API.

+6
source share
1 answer

Windows does not use file descriptions such as Unix, so I assume that the file descriptors are emulated by the C runtime. If you use two different versions of C (for example, if your EXE and DLL are compiled by different compilers or with the same compiler, but with different parameters ), then each runtime will have its own file descriptor emulation, t pass the descriptor from one to the other.

+4
source

Source: https://habr.com/ru/post/907981/


All Articles