VariantUse Sample: Demonstrates the Use of Variants

The VariantUse sample demonstrates how to change existing data into a variant, and change 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.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

-

### To access samples from Visual Studio

  - On the **Help** menu, click **Samples**.
    
    By default, these samples are installed in drive:\\Program Files\\Microsoft Visual Studio 10.0\\Samples\\.
  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run the VariantUse sample

  1. Open VariantUse.sln.

  2. On the Build menu, click Build.

Run VariantUse.exe in the debugger. Put breakpoints in the code that deals with the type of data you are trying to understand. For example, if you want to see the use of variants with strings, put 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. When a breakpoint is reached, use the single-step feature in the debugger to step through the code that transforms data into and out of a variant.

Keywords

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

Note

This sample may not exactly demonstrate some features of the Visual C++ wizards, libraries, and compiler, but it still shows how to complete the intended task.

See Also

Other Resources

MFC Samples