Please enable JavaScript to view this site.

ESL Documentation

Navigation: » No topics above this level «

Q13 How can I avoid problems when using Strings in a DLL?

Scroll Prev Top Next More

The method for storing a string in ESL is different to that used by 'C'. An ESL string is passed as either a HSTRING of PHSTRING data type, which as they are both 4 byte pointers could be wrongly treated as an address of a NULL terminated string. ESL strings do not have a NULL terminator, therefore the normal error is for the DLL to continue reading the string, which ends in an access violation. Occasionally, by chance, a NULL will be located in memory after the string, which results in the 'C' string handling functions working, such a apparent stroke of good luck must not be used, as later changes to your application or a new version of ESL will change the layout of memory, resulting in an access violation.

There are 5 functions, which must be used to convert between ESL string and 'C' string;

EslQueryStringAddr

EslCreateString

EslQueryStringChars

EslSetStringValue

EslSetStringHandle

Each of the above routines is fully detailed in Chapter 9 "External Routines and Stimulus Libraries" of the Programming Guide for the ESL Language, although as only the 32 bit versions of the routine now exist, the suffix "32" has been removed.

A common error when using EslQueryStringAddr, is to assume that the string can be treated as a normal 'C' string, and passed to standard routines. It is essential that you treat the string returned by EslQueryStringAddr as readonly, therefore you must make a copy of the string, then pass the copy to the standard 'C' routine. Once the standard 'C' routine has returned, you will need to use the 'copy string' to update the ESL string using EslSetStringValue. Below is an example of a wrapper, that demonstrates an external subroutine passing a single string to be processed by the routine "ProcessCString". Notice that a memory allocation of size of string plus 1 is used, to allow for the NULL terminator. Also that the allocated memory is freed after the string has been converted back, since failure to do so will result in a memory leak. As the result code will be placed in the built-in variable 'errorlevel', the constant OK should be defined as zero.

FAQs_CCode_Q13_1

Any attempt to bypass the conversion routines, by for example placing a NULL at the end of an ESL string, or attempting to directly modify the 'C' string returned by EslQueryStringAddress, will result in corruption to the ESL application. Sometimes this corruption is not immediately apparent.