VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

String Compare Logical Demo

Nordin Rahman  (1 Submission)   String Manipulation   Intermediate   Wed 3rd February 2021

The code is used to demonstrate the usage of API function StrCmpLogicalW (Shlwapi.dll). It will help you to sort a string array by comparing them logically. The result will sort the result logically, eg, "music9.dat" is lower than "music10.dat".

Inputs
For StrQSortLogical: the input is a string array=strArray() As String, lower index=lowerB As Long, upper index=upperB As Long) For StrSwap: Both are string variables For StrCmpLogical: Both are string variable

Assumes
User may need to provide a list of string to be sorted before using the function.

Returns
StrQSortLogical: no return value, but the function modify the string array directly StrSwap: no return value, but both strings are swapped. StrCmpLogical return 0 if both strings are equal, -1 if string 1 lower than string 2, 1 if string 1 > string 2

Side Effects
The code is written for VB6 only because it make used of undeocumented function VarPtr and StrPtr function, which is not supported under .NET. The function may malfunction if empty array is passed into it. Be wary in using the API declaration directly, because the function StrCmpLogicalW run by assuming its input as NULL-terminated-UNICODE characters. If you passed VB6 strings (which is UNICODE by nature), VB6 SMARTly convert them into ANSI code, thus the result might be wrong and unpredictable. So, you may used the wrapper function instead.

API Declarations
' to compare two string logically
' return:
' 0 if same
' 1 if string 1 is larger than string 2
' -1 if string 1 is smaller than string 2
' use string pointer to both strings as input
' for example: string1="A1.txt", string2="A10.txt"
' StrCmpLogicalP(ByVal StrPtr(string1), ByVal StrPtr(string2))
' return -1, because "A1.txt" is smaller than "A10.txt" logically
Public Declare Function StrCmpLogicalP Lib "Shlwapi.dll" Alias "StrCmpLogicalW" ( _
ByVal ptr1 As Long, _
ByVal ptr2 As Long _
) As Long
' use in copying variable operation
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long _
)

Rate String Compare Logical Demo (2(2 Vote))
String Compare Logical Demo.bas

String Compare Logical Demo Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters