I'm new to attempting to write code in VBA to use WinAPI functions. What encoding does the WinAPI Normalize() function work with? UTF-16 is what I would expect, but the following does not work. The number of characters seems like it's not calculated right, and then the attempt to actually create a normalized string will just crash Access.
'normFormEnum 'not random numbers, but from ... 'https://msdn.microsoft.com/en-us/library/windows/desktop/dd319094(v=vs.85).aspx 'for use in calling the Win API Function NormalizeString() Public Enum normFormEnum normFOther = 0 normFC = 1 'the W3C (Internet) required normalization format normFD = 2 normFKC = 5 normFKD = 6 End Enum 'https://msdn.microsoft.com/en-us/library/windows/desktop/dd319093(v=vs.85).aspx Private Declare Function NormalizeString Lib "Normaliz" ( _ ByVal normForm As normFormEnum, _ ByVal lpSrcString As LongPtr, _ ByVal cwSrcLength As Long, _ ByRef lpDstString As LongPtr, _ ByVal cwDstLength As Long _ ) As Long Public Function stringNormalize( _ ByVal theString As String, _ Optional ByVal normForm As normFormEnum = normFC _ ) As String Dim nChars As Long Dim newString As String nChars = NormalizeString(normForm, StrPtr(theString), Len(theString), 0&, 0) 'prefill the string buffer so it can be altered shortly... newString = String(nChars, " ") Debug.Print nChars 'prints nChars, showing that it 3x the amount of characters. 'The following will crash the application.... ' NormalizeString normForm, StrPtr(theString), Len(theString), StrPtr(newString), nChars stringNormalize = newString End Function
Lenreturns the number of characters in the string; have you triedLenBinstead, which returns the number of bytes in the string? VBA strings use 2 bytes per character.Lenreturns the number of ANSI characters in a string. That this matches UTF-16 code units is a coincidence.String, it's specified to return the number of characters. With examples.