Re: User32 And GDI32 Wrapper Feedback
- Posted by ghaberek (admin) Jul 20, 2012
- 1411 views
With euphoria 4.0, you can simplify this a bit so that you don't have to worry about memory management:
procedure WinHelp(atom handle,sequence str,atom ex,atom ex2) atom pStr c_proc(xWinHelp,{handle,allocate_string(str, 1),ex,ex2}) end procedure
Adding the extra 1 to the allocate_string() call causes the pointer to be freed once there are no more euphoria references to it.
I'm really loving this feature, by the way. Here's a neat trick for passing optional "text" parameters that also accept NULL. I used this a lot when wrapping Newt.
namespace newt public procedure PushHelpLine( object text = NULL ) if sequence( text ) then text = allocate_string( text, 1 ) end if c_proc( x_newtPushHelpLine, {text} ) end procedure
So if you call newt:PushHelpLine() with no parameters, it passes NULL automatically, if you pass an atom (assumed to be a string pointer) it passes it through directly, and if you pass a sequence (a text string) it is allocated into memory, flagged for collection, and then passed to the function.
You could also pre-allocate your strings as constant pointers and let Euphoria clean them up when it's done, although I can't see a direct advantage to this one way or the other (unless you're using a lot of strings on a really really slow machine).
constant helpText = allocate_string( "This is my help text", 1 ) newt:Init(1) newt:PushHelpLine( helpText ) newt:WaitForKey() newt:Finished() -- helpText will be freed automatically when we're done
-Greg