Nasty gotcha: VarCmp vs VariantCompare

Raymond Chen

Raymond

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

Yuck.

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

Raymond Chen
Raymond Chen

Follow Raymond   

0 comments

Comments are closed.