Hi,
I've been using cffi on a project called pywincffi. So far, cffi has been awesome and generally speaking has been a nice alternative to building C-extensions or using ctypes. But I recently started trying to implement some functions from the winsock2/ws2_32 library and I'm starting to have some problems with the build. I'm not certain if it's a problem on my part, a bug in cffi or none of the above so I'd like some help. Here's the simplified code that I'm trying to get working:
import cffi
ffi = cffi.FFI()
ffi.set_unicode(True)
ffi.set_source("test", """
#include <winsock2.h>
#include <windows.h>
""", libraries=("ws2_32", ))
ffi.cdef("""
int WSACreateEvent(SOCKET, WSAEVENT, long);
""")
ffi.compile()
This code, which does not use winsock2/ws2_32, will compile:
import cffi
ffi = cffi.FFI()
ffi.set_unicode(True)
ffi.set_source("test", """
#include <windows.h>
""", libraries=("kernel32", ))
ffi.cdef("""
typedef struct _SECURITY_ATTRIBUTES {
DWORD nLength;
LPVOID lpSecurityDescriptor;
BOOL bInheritHandle;
} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
HANDLE CreateEvent(LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCTSTR);
""")
ffi.compile()
To my understanding, ffi.cdef() is called before the compiler so if there's a type cffi can't find and it's not declared with a typedef then it will result in an error at before calling compile(). With this assumption I tried this instead and it works:
import cffi
ffi = cffi.FFI()
ffi.set_unicode(True)
ffi.set_source("test", """
#include <winsock2.h>
#include <windows.h>
""", libraries=("ws2_32", ))
ffi.cdef("""
int WSAEventSelect(unsigned int, HANDLE, long);
""")
ffi.compile()
So my question is, is there any way to get the first example to work or is there something else I'm missing. I could use the code above I suppose but I'd rather make sure there's not something I'm missing first.
Thanks,
Oliver