4 2 LANSA Open and Visual Basic

LANSA Open Guide

4.2 LANSA Open and Visual Basic

When creating applications in Visual Basic with LANSA Open, each of the LANSA Open functions must be defined to your Visual Basic application program. In Visual Basic, the easiest way of doing this is to add the file LCOE1632.BAS to your project using the Add Module option of the Project menu. This file is supplied in the Visual Basic samples that come with LANSA Open.

The LANSA Open functions can also be defined in any other *.BAS  modules of your programs. If you want to define the functions manually, declare them using the following format:

32 bit Visual Basic

Declare Function LceGetSessionId Lib "LCOEW32.DLL" () As Integer

Special Considerations

There are some special considerations you need to take into account when calling the DLL functions from Visual Basic.

These are:

Pre-allocate Strings

When using a string variable to receive data in a DLL function, Visual Basic must be forced to reserve space for it before the call. This is done by re-assigning the variable to spaces or nulls:

dim s as string 

s = Space (SIZE) 

or

s = String (SIZE, chr(0))

Failure to reserve enough space will result in a GPF (General Protection Fault) error.

Testing a function's results

Return Code "True" is defined as -1 in Visual Basic, so this code:

if lceFunction(…) = True then ' is incorrect

if lceFunction(…) = LceTrue then  ' is correct

LceTrue is a constant defined to be 1.

Null terminated strings

In the LANSA Open DLLs, 0 is used as the termination marker for strings. However, Visual Basic does not recognize 0 as the terminating character in a string. Your Visual Basic program will have to detect the 0 character.

In the sample programs supplied with LANSA Open, the sTrim function is used to remove any leading or trailing blanks from a given string. In addition, the sTrim function recognizes the 0 character as the termination character of a string. In the examples, the strings are always pre-allocated with nulls, rather than spaces to make the sTrim function more efficient.

You can copy the sTrim function from the UTIL.BAS module of the Employee sample project supplied with LANSA Open.

Function sTrim(s As String) As String

    ' this function trims a string of right and left spaces

    ' it recognizes 0 as a string terminator

    Dim i As Integer

    i = InStr(s, Chr$(0))

    If (i > 0) Then

        sTrim = Trim(Left(s, i - 1))

    Else

        sTrim = Trim(s)

    End If

End Function

Helpful Visual Basic Routines

Visual Basic provides routines to work with and convert strings and numeric data, such as:

  • Val()
  • Str() and Str$()
  • Left() and Left$()
  • Right() and Right$()