Returning pointers to LCS

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Returning pointers to LCS

Post by trevordevore » Tue May 15, 2018 2:32 pm

I was wrapping the Windows API call GetActiveWindow() today and wanted to return the value to LCS. GetActiveWindow() returns a HWND which is a pointer. I couldn't find a way to return the integer value (the windowId property of a stack) by working with a Pointer. I then found IntPtr which works for my purposes. Can someone from headquarters confirm that this is the correct approach?

Code: Select all

foreign handler User32GetActiveWindow() returns optional IntPtr binds to "user32>GetActiveWindow"

public handler GetActiveWindow() returns Integer
    variable hWnd as optional Integer

    unsafe
      put User32GetActiveWindow() into hWnd
    end unsafe

    if hWnd is not nothing then
      return hWnd
    else
      return 0
    end if
  end handler
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1206
Joined: Thu Apr 11, 2013 11:27 am

Re: Returning pointers to LCS

Post by LCMark » Thu May 17, 2018 3:49 pm

@trevordevore: Yes - HWND is better typed as UIntPtr - it isn't really a pointer, but a handle which is checked by the OS in all API calls (so you can't break things by adding 1 to an existing one and passing that back - you'll just get an API error).

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Returning pointers to LCS

Post by PaulDaMacMan » Sun May 20, 2018 2:30 pm

It would be nice if there was a chart for all the different types defined in the different LCB modules (Built-in, Java, ObjC, Emscripten JS). :idea:
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”