Applies To:
  • CitectSCADA 5.xx

Summary:
How do I call the CTAPI from Visual Basic (VB)? 

Solution:
To call an external DLL function you use the VB Declare statement. You will find the declarations for the CTAPI functions for use with Visual Basic below. It assumes that CTAPI.DLL can be found somewhere in your system path (eg. ;C:\Citect\Bin must be added to the end of your Path environment variable in System properties under control panel). Otherwise, the exact location of CTAPI.DLL must be hardcoded in the API function declarations (not a good thing to do).

There are two VB tricks here that apply to all API function calls, not just the CTAPI:

1. To pass a string variable to an API function and expect it's value to be modified, the variable must be declared as fixed length.

For example: Dim sCicodeResult As String * clRESULT_LENGTH

where clRESULT_LENGTH is a local constant.

Also, in the API function declaration, the string variable should be declared as ByVal, NOT ByRef.

2. When passing Null as a parameter to an API function, 0& should be passed instead of Null.

For any API call in VB, I'd suggest writting a wrapper function such as the following.

'----------------------------------------------------------------------------------------------------
'
' Copyright (c) 1998 Ci Technologies Pty Limited ACN 001 158 854
'
' FUNCTION NAME: ExecuteCicode
'
' HISTORY:
' 06/Nov/1998 Rod Meliska Original coding.
'
' DESCRIPTION:
' Wrapper function for calling the ctCicode CTAPI function.
'
' PARAMETERS:
' sCicodeCmd - The cicode command to execute.
' sResult - (RETURN) The result of the cicode command.
'
' RETURNS:
' True if the cicode executed successfully, False if an error occurred.
'
Public Function ExecuteCicode(ByVal sCicodeCmd As String, ByRef sResult As String) As Boolean

Const clRESULT_LENGTH As Long = 255

Dim bSuccess As Boolean
Dim sCicodeResult As String * clRESULT_LENGTH
Dim lResult As Long
Dim lChr0Pos As Long

On Error GoTo ExecuteCicode_ErrH

bSuccess = False
If (ctCicode(mhCTAPI, sCicodeCmd, 0, 2, sCicodeResult, clRESULT_LENGTH, 0&) = 0) Then
' Write some code here to raise an error.
Goto ExecuteCicode_Exit
Else
bSuccess = True
End If

' Find the first trailing Chr(0) character. This marks the end of the string.
lChr0Pos = InStr(sCicodeResult, Chr(0))
If (lChr0Pos > 0) Then
sResult = Left(sCicodeResult, lChr0Pos - 1)
Else
sResult = sCicodeResult
Else

ExecuteCicode_Exit:
ExecuteCicode = bSuccess
Exit Function

ExecuteCicode_ErrH:
bSuccess = False
Resume ExecuteCicode_Exit

End Function

This function returns the result through a ByRef parameter. The function return value is whether ctCicode actually succeeded.

mhCTAPI is a module level variable - Private mhCTAPI As Long. The value of mhCTAPI is assumed to be a CTAPI handle returned from a successful ctOpen call.

' PASTE THIS CODE INTO YOUR VISUAL BASIC PROJECT TO ALLOW CALLING THE CTAPI FUNCTIONS

' ctOpen - Open CTAPI interface.
Public Declare Function ctOpen Lib "ctapi.dll" (ByVal sComputer As String, ByVal sUser As String, ByVal sPassword As String, ByVal nMode As Long) As Long

' ctClose - Close CTAPI interface.
Public Declare Function ctClose Lib "ctapi.dll" (ByVal hCTAPI As Long) As Long

' ctCicode - Execute cicode.
Public Declare Function ctCicode Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal sCmd As String, ByVal hWin As Long, ByVal nMode As Long, ByVal sResult As String, ByVal dwLength As Long, ByVal ctOverlapped As Any) As Long

' ctFindFirst - Initiate a search.
Public Declare Function ctFindFirst Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal szTableName As String, ByVal szFilter As String, ByRef pObjHnd As Long, ByVal dwFlags As Long) As Long

' ctGetProperty - Get a named property.
Public Declare Function ctGetProperty Lib "ctapi.dll" (ByVal hnd As Long, ByVal szName As String, ByVal pData As String, ByVal dwBufferLength As Long, ByRef dwResultLength As Long, ByVal dwType As Long) As Long

' ctFindNext - Get the next search item.
Public Declare Function ctFindNext Lib "ctapi.dll" (ByVal hnd As Long, ByRef pObjHnd As Long) As Long

' ctFindPrev - Get the prev search item.
Public Declare Function ctFindPrev Lib "ctapi.dll" (ByVal hnd As Long, ByRef pObjHnd As Long) As Long

' ctFindClose - Close a search.
Public Declare Function ctFindClose Lib "ctapi.dll" (ByVal hnd As Long) As Long

' ctTagWrite - Write to tag.
Public Declare Function ctTagWrite Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal sTag As String, ByVal sValue As String) As Long

' ctTagRead - Read from tag.
Public Declare Function ctTagRead Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal sTag As String, ByVal sValue As String, ByVal dwLength As Long) As Long

' ctListNew - Create poll list.
Public Declare Function ctListNew Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal dwMode As Long) As Long

' ctListAdd - Add tag to poll list.
Public Declare Function ctListAdd Lib "ctapi.dll" (ByVal hList As Long, ByVal sTag As String) As Long

' ctListDelete - Delete tag from poll list.
Public Declare Function ctListDelete Lib "ctapi.dll" (ByVal hTag) As Long

' ctListRead - Read poll list.
Public Declare Function ctListRead Lib "ctapi.dll" (ByVal hList As Long, ByVal pctOverlapped As Any) As Long

' ctListWrite - Write poll list item.
Public Declare Function ctListWrite Lib "ctapi.dll" (ByVal hTag As Long, ByVal sValue As String, ByVal pctOverlapped As Any) As Long

' ctListData.
Public Declare Function ctListData Lib "ctapi.dll" (ByVal hTag As Long, ByVal pBuffer As Any, ByVal dwLength As Long, ByVal dwMode As Long) As Long

' ctListFree - Free poll list.
Public Declare Function ctListFree Lib "ctapi.dll" (ByVal hList As Long) As Long

' ctTagToPoint - Convert tag into point handle.
Public Declare Function ctTagToPoint Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal sTag As String, ByVal dwLength As Long, ByVal pctOverlapped As Any) As Long

' ctPointRead - Read from point handle.
Public Declare Function ctPointRead Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal hPoint As Long, ByRef pData As Integer, ByVal dwLength As Long, ByVal pctOverlapped As Any) As Long

' ctPointWrite - Write to point handle.
Public Declare Function ctPointWrite Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal hPoint As Long, ByRef pData As Integer, ByVal dwLength As Long, ByVal pctOverlapped As Any) As Long

' ctPointClose - Free a point handle.
Public Declare Function ctPointClose Lib "ctapi.dll" (ByVal hCTAPI As Long, ByVal hPoint As Long) As Long

' ctPointCopy - Copy point handle.
Public Declare Function ctPointCopy Lib "ctapi.dll" (ByVal hPoint As Long) As Long

' ctPointGetProperty - Get point property.
Public Declare Function ctPointGetProperty Lib "ctapi.dll" (ByVal hPoint As Long, ByVal szName As String, ByRef pData As Integer, ByVal dwBufferLength As Long, ByRef dwResultLength As Long, ByVal dwType As Long) As Long

' ctPointDataSize - Size of point data buffer.
Public Declare Function ctPointDataSize Lib "ctapi.dll" (ByVal hPoint As Long) As Long

' ctPointBitShift - calculate bit shift offset
Public Declare Function ctPointBitShift Lib "ctapi.dll" (ByVal hPoint As Long) As Long

' ctPointToStr - Format point data to string.
Public Declare Function ctPointToStr Lib "ctapi.dll" (ByVal hPoint As Long, ByRef pRawData As Byte, ByVal dwOffset As Long, ByRef pEngData As Byte, ByVal dwLength As Long, ByVal dwMode As Long) As Long

' The following ctapi funtions have not yet been declared in VB:
'extern BOOL CTAPICALL ctCancelIO(HANDLE,CTOVERLAPPED*); /* cancel pending I/O */
'extern BOOL CTAPICALL ctStrToPoint(HANDLE,LPCSTR,DWORD,BYTE*,DWORD,DWORD); /* format string data into point*/
'extern BOOL CTAPICALL ctEngToRaw(double*,double,CTSCALE*,DWORD); /* scale from eng to raw */
'extern BOOL CTAPICALL ctRawToEng(double*,double,CTSCALE*,DWORD); /* scale from raw to eng */
'extern BOOL CTAPICALL ctGetOverlappedResult(HANDLE,CTOVERLAPPED*,DWORD*,BOOL); /* get overlapped result */
'extern BOOL CTAPICALL ctEngToRaw(double*,double,CTSCALE*,DWORD); /* scale from eng to raw */
'extern BOOL CTAPICALL ctRawToEng(double*,double,CTSCALE*,DWORD); /* scale from raw to eng */
'extern DWORD CTAPICALL ctFindScroll(HANDLE,DWORD,LONG,HANDLE*); /* scroll to search item */

 

Keywords:
 

Attachments