1. cffi
- Posted by petelomax Aug 31, 2015
- 2294 views
- Last edited Sep 02, 2015
I've written a simple cffi implementation.
The intro reads:
-- -- cffi.e -- ====== -- -- Eliminates most of the byte-counting needed when interfacing to C routines. -- The motivation behind this was that while I had many of the windows API -- structures laboriously translated to offsets for 32-bit from many years -- ago, they would all have otherwise needed re-doing for 64-bit. -- -- I must stress that any savings are limited to converting C header (.h) files, -- or more accurately fragments copied verbatim from said, which would otherwise -- have to be done line-by-line manually. There is no new fancy slick syntax to -- help with the code that follows, or anything remotely like that. Although it -- knows the names, offsets and sizes of fields, that does not mean that you no -- longer have to fill them in! But there is less chance of being out-by-2 or -- whetever on the 4th field and therefore all the rest. -- -- Structures: -- =========== -- id = define_struct(s) parses C struct definitions into sizes/offsets etc. -- pStruct = allocate_struct(id) -- (invoke free(pStruct) when done) -- set_struct_field(id,pStruct,"name",value) -- value = get_struct_field(id,pStruct,"name") -- {offset,size,sign} = get_field_details(id,"name") -- size = get_struct_size(id) -- -- Routine definition: -- =================== -- integer rid = define_cfunc(object lib, string cdef) -- integer rid = define_cproc(object lib, string cdef)The full source can be grabbed from http://phix.is-great.org/cffi.e
A little test, works on Phix and OE 4.1.0.
include builtins\cffi.e constant tMB=""" int WINAPI MessageBox( _In_opt_ HWND hWnd, _In_opt_ LPCTSTR lpText, _In_opt_ LPCTSTR lpCaption, _In_ UINT uType ); """ set_unicode(0) constant xMessageBox = define_cfunc("user32.dll",tMB,0) --?c_func(xMessageBox,{0,"text","caption",0}) atom pText = allocate_string("text"), pCaption = allocate_string("caption") if 01 then ?c_func(xMessageBox,{0,pText,pCaption,0}) end if constant tMBP=""" typedef struct { UINT cbSize; HWND hwndOwner; HINSTANCE hInstance; LPCTSTR lpszText; LPCTSTR lpszCaption; DWORD dwStyle; LPCTSTR lpszIcon; DWORD_PTR dwContextHelpId; MSGBOXCALLBACK lpfnMsgBoxCallback; DWORD dwLanguageId; } MSGBOXPARAMS, *PMSGBOXPARAMS; """ constant tMBI = """ int WINAPI MessageBoxIndirect( _In_ const LPMSGBOXPARAMS lpMsgBoxParams ); """ integer idMBP = define_struct(tMBP) integer xMBI = define_cfunc("user32.dll",tMBI) atom pMBP = allocate_struct(idMBP) set_struct_field(idMBP,pMBP,"cbSize",get_struct_size(idMBP)) set_struct_field(idMBP,pMBP,"lpszText",pText) set_struct_field(idMBP,pMBP,"lpszCaption",pCaption) ?c_func(xMBI,{pMBP})
Just in case you need it you can get the current version of pcolumn.e from http://openeuphoria.org/pastey/274.wc
I even got a version to work on RDS Eu 2.4, but it turned out a bit grubby.
I am thinking that things like MAX_PATH are probably best handled manually, unless someone has a better idea.
Let me know what you think,
Pete
PS The triple-quoted constants do not seem to be working.
2. Re: cffi
- Posted by Euman Sep 07, 2015
- 2072 views
Thank you, this is what I've been needing. Would you please place the file on your server instead of it reading as one line html in my editor?
3. Re: cffi
- Posted by petelomax Sep 07, 2015
- 2095 views
Thank you, this is what I've been needing.
You're welcome.
Would you please place the file on your server instead of it reading as one line html in my editor?
Ah, http://phix.is-great.org/cffi.e works fine in Opera and Chrome, but is garbled in Internet Explorer. You can try View/Source, that worked for me. Not sure how I would tell IE "please don't mangle this plain text file"...
Otherwise you might be able to find a copy on https://bitbucket.org/petelomax/phix, in the builtins folder, though a) you might not be allowed access, and b) I am struggling with all this repository stuff to understand when and why "yes, I did that" actually means "I haven't changed anything, yet".
Regards,
Pete
4. Re: cffi
- Posted by ghaberek (admin) Sep 08, 2015
- 2046 views
Ah, http://phix.is-great.org/cffi.e works fine in Opera and Chrome, but is garbled in Internet Explorer. You can try View/Source, that worked for me. Not sure how I would tell IE "please don't mangle this plain text file"...
Otherwise you might be able to find a copy on https://bitbucket.org/petelomax/phix, in the builtins folder, though a) you might not be allowed access, and b) I am struggling with all this repository stuff to understand when and why "yes, I did that" actually means "I haven't changed anything, yet".
This link works fine for me: Phix > Source > builtins > cffi.e > Raw
https://bitbucket.org/petelomax/phix/raw/3062e63eef5ee53b600ed52bcbec1a9a40304dad/builtins/cffi.e
-Greg
6. Re: cffi
- Posted by petelomax Sep 08, 2015
- 2009 views
Good to know, thanks. I've also figured out the difference between commit and push!
Pete
7. Re: cffi
- Posted by petelomax Sep 13, 2015
- 2137 views
I got it thank you...
Any joy yet?