February 18th, 2016

Nasty gotcha: VarCmp vs VariantCompare

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.