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

Comparison Return value
Less than 0 = VARCMP_LT
Equal to 1 = VARCMP_EQ
Greater than 2 = 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

Comparison Return value
Less than −1
Equal to 0
Greater than +1

Yuck.

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

0 comments

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