Nasty gotcha: VarCmp vs VariantCompare

Raymond Chen


There are two functions for comparing VARIANTs. One comes from the COM team, called VarCmp. Assuming a successful comparison, it returns

ComparisonReturn value
Less than0 = VARCMP_LT
Equal to1 = VARCMP_EQ
Greater than2 = VARCMP_GT

This is consistent with the other Var­Xxx functions.

On the other hand, the Variant­Compare function comes from the shell, and it returns

ComparisonReturn value
Less than−1
Equal to0
Greater than+1


So be careful which one you’re calling and how you’re testing the result.


Comments are closed. Login to edit/delete your existing comments