Passing Unicode strings to API functions

Virtually all older API functions exist in two versions: An ANSI version ending in "A" and a Unicode version ending in "W". If you specify an API function without either of them, Visual FoxPro automatically adds "A" at the end and uses the ANSI version. All of this happens automatically, nothing to spend any thought on.
Life was good until Microsoft kept publishing new APIs that only accept Unicode parameters. To convert a String to Unicode, you can use the following line:


STRCONV( lcString, 5 )

When you call API functions, you have to take care of one more detail. A Unicode string doesn't require only one CHR(0) at the end, but two of them. One is added by VFP, but it's your responsibility to add the second one:


STRCONV( lcString, 5 ) + CHR(0)

Failing to do so results in API calls that work sometimes, and sometimes not. A typically pattern is that the function call works the first time, but no more after that. The first call was done with new, clean memory. Any further call reuses memory which has been filled by subsequent operations.

Another sign for forgetting CHR(0) is when your code works fine when you slowly step through it, but not if you run it normally. Visual FoxPro performs garbage collections regularly when debugging slowly. This vastly increases the likelihood that you get clean memory.