Sign in to follow this  

Python, Lisp & C++ dev. question

This topic is 4764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this