String Arguments

A common problem in calling DLLs from Visual Basic is in the declaration of strings passed to DLL functions. String variables that are passed to the DLL in order to be modified (arguments marked as "returned" or "modified" in the documentation) need to be declared as fixed length strings. The syntax is

Dim mystring as String * 81

This variable could be passed to a function such as pipt_taglong to be filled with a tag name. Failure to dimension strings that are modified leads to memory overwrites resulting in program crashes or improper behavior. This type of error may appear to work correctly in the call where the string variable is passed, but abnormal behavior may be observed in another part of the routine or program.

Strings that are not modified by the DLL (arguments marked only as "passed" in the documentation) do not need to be dimensioned. They are dynamically sized as needed by Visual Basic. For example:

Dim mystring as String

mystring = "myserver"

This variable could be passed to the piut_setservernode function as the servername.

The function declarations in both cases use the ByVal keyword for string arguments. This indicates to Visual Basic that it should translate the string into a null-terminated string (used by C) before calling the function. This differs from the normal use of the ByVal keyword.

Enabling Operational Intelligence