Python, Lisp & C++ dev. question

Started by
6 comments, last by Fruny 19 years, 4 months ago
I have a seemingly simple question. If I was to be programming Python, and I created extension modules for Python in C, could I use the functionality of, say the win32 API, without having to create glue code for all the functions contained therein? For example say I wanted to create a window (CreateWindow(int x, int y)), would I have to translate the win32 API to do this? I'm trying to do the same thing with Lisp(calling C++ from Lisp), would I have to create glue code for the Win32 API here as well? Any help would be much appreciated!
Advertisement
No, you wouldn't need to wrap the whole API (although I'm sure you can find it's already done anyway). You can happily make a C function which contains your window startup code and can just call it to create your window (or whatever).

You just decide what interface you'd like to the underlying API, perhaps something like:

Init() // init the OS stuff

Window OpenWindow(x,y) // create a window

ResizeWindow(Window,w,h) // resize a given window

Then just write the required C code underneath.
Quote:Original post by JuNC
No, you wouldn't need to wrap the whole API (although I'm sure you can find it's already done anyway). You can happily make a C function which contains your window startup code and can just call it to create your window (or whatever).

You just decide what interface you'd like to the underlying API, perhaps something like:

Init() // init the OS stuff

Window OpenWindow(x,y) // create a window

ResizeWindow(Window,w,h) // resize a given window

Then just write the required C code underneath.


So if I created this hypothetical interface, I would still have to convert many datatypes used by the Win32 API anyway wouldn't I? Some of the return datatypes are _stdcall, I'm not sure how this kind of datatype could be converted?
This does facilitate what I'm trying to do though, thank you!
Well you could just invent your own abstract datatypes (as I did there with 'Window') and use them in your interface. You only need to expose in the C interface what you're going to use in the main program.

You don't need to worry about the calling convention since the wrapper will take care of that. In C you'd probably have something like this (hypothetical FFI, similar to many languages):

struct{  HWND hWnd;} Window;void __os_Init(){   // do some stuff we need to do to create windows, maybe registering window classes or something} LANG_PTR __os_CreateWindow( LANG_INT x, LANG_INT y ){  struct Window* window;  window = (struct Window*)malloc(sizeof(struct Window));  window->hWnd = CreateWindow(...);  return LANG_PTR_MAKE(window);}void __os_ResizeWindow( LANG_PTR window, LANG_INT w, LANG_INT h){  struct Window* pWindow = (struct Window*)window;  ResizeWindow( pWindow->hWnd, w, h );}


This example is obviously not exact code for any particular FFI but should be fairly common usage. The actual wrapping of the __os_ functions (or whatever you want to name them) and the various used types highly depends on the language, for instance in OCaml you could pretty much just compile the above and add an interface file then just use the various functions directly.
Quote:Original post by malune
I have a seemingly simple question. If I was to be programming Python, and I created extension modules for Python in C, could I use the functionality of, say the win32 API, without having to create glue code for all the functions contained therein?
For example say I wanted to create a window (CreateWindow(int x, int y)), would I have to translate the win32 API to do this?
I'm trying to do the same thing with Lisp(calling C++ from Lisp), would I have to create glue code for the Win32 API here as well?
Any help would be much appreciated!


This is probably not very helpful but I'm curious: why do you even want to wrap the Win32 API? You'll lose the portability and writing a wrapper will take a lot of time.. Why don't you use wxPython or something similiar if you want GUI stuff?
Ad: Ancamnia
Thanks JuNC, this all seems to be getting alot clearer. :)

Quote:Original post by tentoid
This is probably not very helpful but I'm curious: why do you even want to wrap the Win32 API? You'll lose the portability and writing a wrapper will take a lot of time.. Why don't you use wxPython or something similiar if you want GUI stuff?


What I'm basically trying to do is code a portable window framework that I can use with Lisp. I want it to interact directly with X and the Win32 API, for educational purposes. I'm prototyping the idea in Python at the moment to see if it's feasible for me to implement it in Lisp.
I'm not exactly making life easy for myself, but this is so that I learn more about the underlying structure of the Win32 API/X. :)
Quote:Original post by malune
What I'm basically trying to do is code a portable window framework that I can use with Lisp. I want it to interact directly with X and the Win32 API, for educational purposes. I'm prototyping the idea in Python at the moment to see if it's feasible for me to implement it in Lisp.
I'm not exactly making life easy for myself, but this is so that I learn more about the underlying structure of the Win32 API/X. :)


Seems like a huge task! I would just write a wrapper for wxWidgets or something similiar :) I think even that would take some time. But good luck for your project.
Ad: Ancamnia
If you want access to the Win32 API from python, just use pywin32.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement