Questions tagged [variant]

A variant data type is a tagged union that holds other data types. It is a standard data type in [ocaml], and typically used for interop calls between languages ([c++] and [vb6]) in classic Microsoft Windows [com] programming. It also exists in other languages using other names, such as [discriminated-unions] or the more general concept of [algebraic-data-types]

In , the variant type is a basic building block of the language. Custom variants are defined with a set of constructors that restrict which types it can hold, and are used to inspect it in and to warn if a pattern match does not cover all constructors.

Questions that should have this tag would include ones involving how to pass data between Windows processes or within the same process using and classic function calls. Often the calls occur between languages (e.g. to ), but they are also used in single language programs (e.g. ) to achieve a plug-in, modular architecture with DLL files.

A variant is technically a structure holding a union of data types along with a VARTYPE that indicates which member of the structure is valid. It can hold any other data type, including primitive types (BYTE, LONG), pointers to primitive types (LONG*, FLOAT*), IDispatch pointers, and even pointers to other variants.

There are a couple of helper classes Microsoft has created to help with the details of dealing with raw variants:

These classes relieve much of the grunt work of interop programming with variants such as doing deep destroys on contained SAFEARRAYs.

Definitions

  • Tagged union: A data structure that can be used to hold a number of different data types, only one of which is valid at a time. There is a field (VARTYPE) which indicates the valid data member (https://en.wikipedia.org/wiki/Tagged_union)

Examples

OCaml

(* Definition of the built-in option type *)
type 'a option =
| Some of 'a
| None

(* Construction *)
let var = Some "hello"

(* Inspection through pattern matching *)
let greeting =
  match var with
  | Some str -> str
  | None -> "howdy"

Visual Basic

Dim var as Variant

var = "hello"   'Variant holds a string

var = 7         'Variant holds an Integer

C++

_variant_t var;

var = "hello";  //Variant holds a string

var = 7;        //Variant holds an int

Resources

Related tags

1152 questions
0
votes
2 answers

Casting the value of a DBus Variant to the correct type at runtime in java

I've been bashing my head on this problem for a while now. I'm dealing with properties setting using the DBus-java bindings for DBus. When Set is called, the value to set is wrapped in a org.freedesktop.types.Variant object from which I have to…
darkhelmet
  • 123
  • 1
  • 2
  • 7
0
votes
1 answer

Is it safe to pass boost::variant to (from) dll?

Consider this code typedef boost::variant var_t; // this interface class will be implemented in DLL class producer { public: virtual var_t produce() = 0; }; // this class will be implemented by DLL user and // pointer to…
Alexander
  • 779
  • 8
  • 17
0
votes
2 answers

Extracting Data from Excel Sheet to VBA: Empty Variant/Array, but UBound Returns Number

I am trying to extract text data from an Excel sheet into an array (defined as a variant in this case). The below code does not return desirable results: When I try to access an element in the SearchItems variant array, an error pops up saying…
msmf14
  • 1,449
  • 4
  • 13
  • 19
0
votes
1 answer

What are the possibilities for VariantInit Crash in C++?

I have variantinit immediately to variantClear like below, VariantClear(&var); VariantInit(&var); VariantInit is crashing in few places and few other places it's not crashing even though I have same sequence in both cases. What could be the reason…
srajeshnkl
  • 883
  • 3
  • 16
  • 49
0
votes
0 answers

fFeatures needs to equal 2194?

Currently, I am trying hard to get a safearray of variants to work in my c++ code below. As you can see I call QueueInputReport which has a signature of (SAFEARRAY * psainputreport, UNIT timeoutduration): CComSafeArray savt; …
starman
  • 1
  • 4
0
votes
2 answers

C++ short* to VARIANT_BOOL*

I have a function that expects a short* as an argument. I would need to convert this to a VARIANT_BOOL*. Can anybody tell me a reliable way to do this? I can even pass the VARIANT_BOOL* to the function, but then the VARIANT_BOOL* is not filled with…
tmighty
  • 10,734
  • 21
  • 104
  • 218
0
votes
2 answers

variable array name on output (c#)

I am looking for the proper way to have a variable property name on serialization. Example outputs: { "header": { "total": 3, "page": 1, "pagesize": 30 }, "cats": [ .. ] } and { "header": { …
0
votes
2 answers

Variant for mpl types sequences as a class member - how?

Please advice, how I can solve the following problem – I want to have class member variable boost::variant type which has a type from defined mpl type sequence. After, in main, I want to call a method which calls method for every class method…
LowCoder
  • 71
  • 3
0
votes
2 answers

How to build this c++ typelist into a variant?

Here, how do I fix this c++ typelist template compile error? we built a typelist, using the code from modern c++ design. Question is now -- how do I take this and built it into a variant class?
anon
  • 41,035
  • 53
  • 197
  • 293
0
votes
1 answer

Http live streaming segment number

Using HLS to deliver vod content. An HLS variants must have the same count of segments between them or the number of segment may vary from variant to variant?
billfox
  • 1
  • 1
0
votes
1 answer

C++ and .NET - Converting from 'System::String ^' to '_variant_t'

I'm working on a managed C++ application that utilizes a C# library to populate a field in an ADO Recordset: recordset->Fields->GetItem(L"Id")->Value = _variant_t(Library::IdGenerator->GenerateNewId()); However, I'm encountering an error converting…
miguelarcilla
  • 1,426
  • 1
  • 20
  • 38
0
votes
0 answers

difficulty passing Integer Array as VARIANT*

EDIT: My post yesterday has been modified. EDIT2: I've tried using Marshal.GetNativeVariantForObject but I still get same error. See modified code below... From VB.NET code, I am calling a method of a COM object and get DISP_E_TYPEMISMATCH Dim…
ryrich
  • 2,164
  • 16
  • 24
0
votes
1 answer

Read and display data from Variant VT_ARRAY | VT_BSTR?

I want to use an instrument that has a COM object interface. Unfortunately there is no C++ example in the SDK, only C#. After creating the wrapper class, I can successfully read the instrument's serial number into a VARIANT: VARIANT snumbers; …
0
votes
1 answer

display the value from generic pointer

I have a void pointer which contains the address of object but I do not which types of this object. My code is like VARIANT vtProp; Now getting the value in vtProp using some method. It's have successfully some values. Now I have assign the value…
user2499879
  • 673
  • 3
  • 10
  • 16
0
votes
1 answer

variant data type dependencies

I am using variant data type in my small VBA app to use both string and integer data's under same function. However, only my Integer calculations are seem to work and the string related parts just gives #value! error. In my app, I am using both…
Fenixs
  • 377
  • 1
  • 4
  • 20