Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Foreign function interface

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Jul 14th, 2018, 11:40pm    Post subject: Foreign function interface Reply with quote

One major feature that has been lacking is a foreign function interface (FFI). This is a way to call functions in a different language. On Windows this is typically done through DLLs.

In Liberty BASIC this is accomplished by the CallDLL statement. This statement has many good points, but also some bad points against it. For one thing, it can become rather hard to get all of the parameters correct, especially for functions with large numbers of parameters, such as CreateWindowEx. More advanced LBers have taken to wrapping their calls in Subs and Functions to gain the greater argument checking this provides at compile-time.

I and a few others find the way that Visual Basic deals with this issue to be quite advantageous. It uses the Declare statement to give the compiler all of the needed information needed in a single line. However, more often than not these are some very long lines. Below is a comparison of a couple of Windows API functions.
Code:
CallDLL #user32, "BringWindowToTop", hWnd As ULong, ret As Long
CallDLL #user32, "SetWindowTextA", hWnd As ULong, "Hello" As Ptr, ret As Long

Code:
Public Declare Function BringWindowToTop Lib "user32"(h As Integer) As Integer
Public Declare Ansi Function SetWindowText Lib "user32" Alias "SetWindowTextA"(h As Integer, s As String) As Integer
' ...
BringWindowToTop(hWnd)
SetWindowText(hWnd, "Hello")

The syntax I came up with for VF resembles VB in several respects, but uses some positional arguments and shorter keywords in its simple form to keep most statements from going off the screen.
Code:
Declare "user32" Function BringWindowToTop(h As handle) As Int
Declare "user32", %ANSI Function SetWindowText Is "SetWindowTextA"(h As handle, s As String) As Int
' …
Call BringWindowToTop hWnd
Call SetWindowText hWnd, "Hello"

However, this can still introduce a good bit of redundancy for large numbers of declarations. Therefore, I also made a multiline form.
Code:
Declare "user32", %ANSI
  Function BringWindowToTop(h As handle) As Int
  Function SetWindowText Is "SetWindowTextA"(h As handle, s As String) As Int
End Declare

You can read more about it at https://www.b6sw.com/ViviFire/docs/Declare_Statement.html.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group