|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
VariantUse Sample: Demonstrates the Use of Variants
The VariantUse sample demonstrates changing existing data into a variant, and changing a variant into other types of data. Many COM objects accept variants as function parameters. The goal of this sample is to help you change standard C data types into variants.
This sample covers the use of currency, dates, SAFEARRAYs, multidimensional arrays, strings, chars, shorts, and longs.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
To build and run the VariantUse sample
Open the solution VariantUse.sln.
On the Build menu, click Build.
Run VariantUse.exe in the debugger. Place breakpoints in the code dealing with the type of data you are trying to understand. For example, if you want to see the use of variants with strings, place breakpoints in the OnString function. Run the sample in the debugger and click the Strings button. Clicking the buttons on the main dialog box will have no apparent effect unless there are breakpoints in the associated code. Once a breakpoint is reached, use the debugger's single-step feature to step through the code that transforms data into and out of a variant.
This sample uses the following keywords:
_bstr_t; CComBSTR; CComBSTR::Append; COleSafeArray; COeSafeArray::AccessData; COleSafeArray::CreateOneDim; COleSafeArray::UnaccessData; CString; CURRENCY; SAFEARRAY; SafeArrayAccessData; SAFEARRAYBOUND; SafeArrayCreate; SafeArrayCreateVector; SafeArrayDestroy; SafeArrayGetElement; SafeArrayUnaccessData; SysAllocString; SysFreeString; T2COLE; USES_CONVERSION; VARIANT; VariantChangeType; VariantCopy
Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.