Foxpert Software Development & Consulting

Menu

Whitepapers
Downloads
Knowlbits
Guineu

2007-09Sep-01

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.

Similar transparent is passing strings to API functions. Most API functions expect strings to be zero-terminated, that is, end in CHR(0). Because this is so common, Visual FoxPro automatically adds CHR(0) to every string that it passes to an API function.

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.

Previous KnowlBits

RSS

August 2008 (1)

July 2008 (2)

May 2008 (1)

April 2008 (2)

January 2008 (2)

December 2007 (2)

November 2007 (2)

October 2007 (1)

September 2007 (1)

August 2007 (5)

July 2007 (4)

May 2007 (6)

March 2007 (3)

February 2007 (7)

January 2007 (6)

November 2006 (1)

October 2006 (3)

September 2006 (10)

June 2006 (2)

May 2006 (6)

April 2006 (1)

Southwest Fox 2008, Mesa, AZ, October 16 - 19, 2008

Impressum Kontakt Contact