Data Types

Because various languages handle data types in different ways, the SocketTools libraries have been designed to primarily use basic data types such as integers and strings. The following is a list of numeric data types that are used, along with their C and Visual Basic equivalents.

Description Size Range C / C++ VB 6 VB.NET
Byte 1 byte 0 to 255 BYTE Byte Byte
Boolean 4 bytes 0 is False, 1 is True BOOL Long Integer
Handle 4 bytes 0 to 4,294,967,295 HCLIENT Long Integer
Integer 4 bytes -2,147,483,648 to 2,147,483,647 INT Long Integer
Integer 4 bytes 0 to 4,294,967,295 UINT Long Integer
Short Integer 2 bytes -32,768 to 32,767 SHORT Integer Short
Short Integer 2 bytes 0 to 65,535 WORD Integer Short
Long Integer 4 bytes -2,147,483,648 to 2,147,483,647 LONG Long Integer
Long Integer 4 bytes 0 to 4,294,967,295 DWORD Long Integer

One problem that is frequently encountered when converting function definitions from C or C++ to other languages is the size of the integer data type. For example, default integer size for Visual Basic 6 is 16-bits on 32-bit platforms. However, in Visual Basic.NET, as well as languages like Visual C++, the default integer size is 32-bits. Also, some languages do not support unsigned integer types. In this case, as with Visual Basic, the signed type should be used instead.

Boolean Data

Boolean parameters present a special problem for two reasons. Firstly, the data types used to represent boolean values frequently vary between languages. Secondly, different languages represent the values "true" and "false" differently. Boolean parameters (declared as BOOL in the function prototypes) should always be passed as 32-bit signed integers.

If you are passing a boolean parameter to a function, then "false" should be represented by a value of zero and "true" as a non-zero value, typically a value of one. When writing code that checks a boolean flag, or tests a boolean return value from a function, it is recommended that you test against a value of zero. For example, consider the following code in Visual Basic 6.0:

Dim bConnected As Long
bConnected = InetIsConnected(hSocket)
If bConnected = True Then
    ' The socket is connected to a remote host
Else
    ' The socket is not connected, or the socket handle
    ' may be invalid; call InetGetLastError to check the
    ' error code
End If

In this example, even if the function InetIsConnected was successful, the program would always believe that it failed because of the explicit test against the value True. This is because the function returns a value of 1 to indicate success, but Visual Basic defines True as -1. Instead, the code should be written as:

Dim bConnected As Long
bConnected = InetIsConnected(hSocket)
If bConnected <> 0 Then
    ' The socket is connected to a remote host
Else
    ' The socket is not connected, or the socket handle
    ' may be invalid; call InetGetLastError to check the
    ' error code
End If

In summary, the rule of thumb for dealing with boolean parameters is that they should always be 32-bit integer values, and you should always compare the boolean against a value of zero, never against a predefined constant like True.

String Data

String parameters can also present a problem when calling functions from languages other than C and C++. Different languages tend to have different internal representations of how string data is stored. The convention used by the SocketTools libraries is that a string is an array of characters, terminated by a null character (a character with an ASCII value of zero). The length of the string is not stored in the string data itself, and by definition, a string cannot contain embedded nulls.

To use functions which require string parameters, the language must be capable of converting between its native string data type and the null-terminated character array expected by the SocketTools functions. For example, Object Pascal provides the StrPCopy function. Note that Visual Basic provides implicit conversion between its native string type and null-terminated strings when a string is passed by value (ByVal) instead of by reference.

If you are unsure of how your language handles null-terminated strings, we recommend that you review the language’s technical reference for information on how to call native Windows API functions. Since the Windows API also uses null-terminated strings, that same information can be used to determine how to call functions in the SocketTools libraries.


Copyright © 2008 Catalyst Development Corporation. All rights reserved.