Last active
October 29, 2018 17:45
-
-
Save jwinett/cec783b5c1a4599431adb38db1841e79 to your computer and use it in GitHub Desktop.
Calling CompareStringEx from Visual Basic 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Private Declare Function CompareStringEx Lib "kernel32.dll" ( _ | |
ByRef lpLocaleName As String, _ | |
ByVal dwCmpFlags As Long, _ | |
ByRef lpString1 As Byte, _ | |
ByVal cchCount1 As Long, _ | |
ByRef lpString2 As Byte, _ | |
ByVal cchCount2 As Long, _ | |
ByVal lpVersionInformation As Long, _ | |
ByVal lpReserved As Long, _ | |
ByVal lParam As Long) As Long | |
' Compares two strings, returning signed result like StrComp does | |
' 2019-10-29 JmW | |
Public Function ApiCompareStringDigitsAsNumbers(String1 As String, String2 As String) As Integer | |
Const LOCALE_NAME_USER_DEFAULT = vbNullString | |
Const SORT_DIGITSASNUMBERS As Long = 8 ' dwCmpFlags | |
If CSW_OK Then ' Earlier check for function in kernel32.dll -- still have XP users | |
Dim lpString1() As Byte | |
Dim lpString2() As Byte | |
Dim result As Long | |
lpString1 = String1 & vbNullChar | |
lpString2 = String2 & vbNullChar | |
result = CompareStringEx( _ | |
LOCALE_NAME_USER_DEFAULT, _ | |
SORT_DIGITSASNUMBERS, _ | |
lpString1(0), -1, _ | |
lpString2(0), -1, _ | |
0, 0, 0) | |
' The function returns 0 if it does not succeed. | |
' Returns one of the following values if successful: | |
' CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN | |
' To maintain the C runtime convention of comparing strings, the value 2 | |
' can be subtracted from a nonzero return value. | |
' Then, the meaning of <0, ==0, and >0 is consistent with the C runtime. | |
If result <> 0 Then | |
ApiCompareStringDigitsAsNumbers = result - 2 | |
Exit Function | |
End If | |
End If | |
' If something dailed or API wasn't actually there, then default to | |
' calling StrComp | |
ApiCompareStringDigitsAsNumbers = StrComp(String1, String2, vbTextCompare) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment