SAFEARRAY (C++)

From Appmethod Code Examples
Jump to: navigation, search

Description

The following example is a console application that:

  • Creates a SAFEARRAY.
  • Refers a SAFEARRAY element.
  • Destroys a SAFEARRAY.

Code

#pragma hdrstop

#include <tchar.h>
#include <System.hpp>
#include <stdint.h>
// ---------------------------------------------------------------------------

#pragma argsused

int _tmain(int argc, _TCHAR* argv[]) {
	// Specify the bounds:
	// -- dim. count = 2
	// -- element count = 8 for each dimension
	// -- low bound = 0 for each dimension
	SAFEARRAYBOUND mySafeArrayBounds[2] = { {8, 0}, {8, 0}};

	// Create mySafeArray
	SAFEARRAY *mySafeArray =
		SafeArrayCreate(VT_I2 /* var. type: 2 byte signed int */ ,
		2 /* dim. count */ , mySafeArrayBounds);

	// Set value 100 to element [3][7]
	int16_t *elementPtr = ((int16_t*)mySafeArray->pvData) +
		3 * mySafeArray->rgsabound[0].cElements + 7;
	*elementPtr = 100;

	// Destroy mySafeArray
	SafeArrayDestroy(mySafeArray);

	return 0;
}

Uses