Tutorials to .com

Tutorials to .com » Software » C » SAFEARRAY using examples

SAFEARRAY using examples

Print View , by: iSee ,Total views: 119 ,Word Count: 2025 ,Date: Tue, 30 Jun 2009 Time: 5:29 AM

SAFEARRAY using examples

Contents:

SAFEARRAY using examples ... 1

Contents: ... 1

Introduction: ... 1

What is a SAFEARRAY: ... 1

Create a SAFEARRAY: 2

Method one: Use SafeArrayAllocDescriptor in the stack to create one-dimensional array ... 2

Method 2: Use SafeArrayAllocData in the pile SafeArrayAllocDescriptor and create one-dimensional array ... 2

Method three: Use SafeArrayAllocDescriptor and SafeArrayAllocData create two-dimensional array in the heap ... 3

Method IV: Using SafeArrayCreate created in the heap of one-dimensional array ... 4

Method five: Use SafeArrayCreate to create two-dimensional array in the heap ... 5

Method six: Use SafeArrayCreateEx create a structure that contains one-dimensional array ... 5

Visit SAFEARRAY: 7

Method one: Use SafeArrayAccessData methods ... 7

Method 2: Use SafeArrayGetElement and SafeArrayPutElement 7

Components / client in the principle of passing SAFEARRAY: ... 8

Preface:

SAFEARRAY always plagued the use of a lot of people, in order to understand the problem say, I put my knowledge to be a current summary of

What is a SAFEARRAY:

SAFEARRAY is actually a structure on this part can refer to MSDN.

ms-help: / / MS.MSDNQTR.2003FEB.2052/automat/htm/chap7_9ntx.htm

We do not need 16-bit operating system concerned with the definition, because of our team for more than just a platform developed under WIN2000.

Create a SAFEARRAY:

Method one: Use SafeArrayAllocDescriptor in the stack to create one-dimensional array

/ / Create a SAFEARRAY array, each element for the long type, the array is one-dimensional array

long nData [10] = (1,2,3,4,5,6,7,8,9,10);

SAFEARRAY * pArray = NULL;

HRESULT hr = SafeArrayAllocDescriptor (1, & pArray); / / create a SAFEARRAY structure of the object

pArray-> cbElements = sizeof (nData [0]);

pArray-> rgsabound [0]. cElements = 10;

pArray-> rgsabound [0]. lLbound = 0;

pArray-> pvData = nData;

pArray-> fFeatures = FADF_AUTO | FADF_FIXEDSIZE; / / FADF_AUTO specify the distribution of data in the stack, and the size can not be changed (fixed to 10)

/ / access array SAFEARRAY

long * pValue = NULL;

SafeArrayAccessData (pArray, (void **) & pValue);

long Low (0), High (0);

hr = SafeArrayGetLBound (pArray, 1, & Low); / / dimension of the index starting at 1

hr = SafeArrayGetUBound (pArray, 1, & High); / / dimension of the index starting at 1

SafeArrayUnaccessData (pArray);

SafeArrayDestroy (pArray);

This approach in the allocation of stack space occupied by the array element, that is used in an array nData space

Method 2: Use SafeArrayAllocDescriptor and SafeArrayAllocData in the heap to create one-dimensional array

/ / Create a SAFEARRAY array, each element for the long type, the array is one-dimensional array

long nData [10] = (1,2,3,4,5,6,7,8,9,10);

SAFEARRAY * pArray = NULL;

HRESULT hr = SafeArrayAllocDescriptor (1, & pArray); / / create a SAFEARRAY structure of the object

pArray-> cbElements = sizeof (nData [0]);

pArray-> rgsabound [0]. cElements = 10;

pArray-> rgsabound [0]. lLbound = 0;

SafeArrayAllocData (pArray);

long * pData = NULL;

SafeArrayAccessData (pArray, (void **) & pData);

long l (0), h (0);

SafeArrayGetLBound (pArray, 1, & l);

SafeArrayGetUBound (pArray, 1, & h);

long Size = h-l +1;

SafeArrayAccessData (pArray, (void **) & pData);

for (long Idx = l; Idx <Size; + + Idx)

(

pData [Idx] = nData [Idx];

)

SafeArrayUnaccessData (pArray);

/ / Access array SAFEARRAY

long * pValue = NULL;

SafeArrayAccessData (pArray, (void **) & pValue);

long Low (0), High (0);

hr = SafeArrayGetLBound (pArray, 1, & Low); / / dimension of the index starting at 1

hr = SafeArrayGetUBound (pArray, 1, & High); / / dimension of the index starting at 1

SafeArrayUnaccessData (pArray);

SafeArrayDestroy (pArray);

Method three: Use SafeArrayAllocDescriptor and SafeArrayAllocData create two-dimensional array in the heap

SAFEARRAY * pArray = NULL;

HRESULT hr = SafeArrayAllocDescriptor (2, & pArray);

pArray-> rgsabound [0]. lLbound = 0;

pArray-> rgsabound [0]. cElements = 3;

pArray-> rgsabound [1]. lLbound = 0;

pArray-> rgsabound [1]. cElements = 3;

pArray-> cbElements = sizeof (long);

hr = SafeArrayAllocData (pArray);

long lDimension [2];

long x = 1;

/ / For the first line of the assignment

for (long i = 0; i <3; + + i)

(

lDimension [1] = 0; / / line

lDimension [0] = i; / / out

SafeArrayPutElement (pArray, lDimension, & x);

x + +;

)

/ / For the second line assignment

for (long i = 0; i <3; + + i)

(

lDimension [1] = 1; / / line

lDimension [0] = i; / / out

SafeArrayPutElement (pArray, lDimension, & x);

x + +;

)

/ / Read the second line in the third SafeArray-disaggregated data

long y (0);

lDimension [1] = 1;

lDimension [0] = 2;

SafeArrayGetElement (pArray, lDimension, & y);

SafeArrayDestroy (pArray);

The use of two-dimensional SAFEARRAY array subscript when it must be noted that here is based on the way out the main sequence, that is, lDimension [1] representatives, lDimension [0] on behalf of out.

Method IV: Using SafeArrayCreate in the heap to create one-dimensional array

SAFEARRAYBOUND Bound [1];

Bound [0]. LLbound = 0;

Bound [0]. CElements = 10;

SAFEARRAY * pArray = SafeArrayCreate (VT_I4, 1, Bound);

long * pData = NULL;

HRESULT hr = SafeArrayAccessData (pArray, (void **) & pData);

long Low (0), High (0);

SafeArrayGetLBound (pArray, 1, & Low);

SafeArrayGetUBound (pArray, 1, & High);

long Size = High-Low +1;

for (long Idx = Low; Idx <Size; + + Idx)

(

pData [Idx] = Idx;

cout <<pData [Idx] <<endl;

)

SafeArrayUnaccessData (pArray);

SafeArrayDestroy (pArray);

Method five: Use SafeArrayCreate to create two-dimensional array in the heap

SAFEARRAYBOUND Bound [2];

Bound [0]. LLbound = 0;

Bound [0]. CElements = 3;

Bound [1]. LLbound = 0;

Bound [1]. CElements = 3;

SAFEARRAY * pArray = SafeArrayCreate (VT_I4, 2, Bound);

long Demen [2];

for (long i = 0; i <3; + + i)

(

for (long j = 0; j <3; + + j)

(

Demen [1] = i;

Demen [0] = j;

long x = i * j;

SafeArrayPutElement (pArray, Demen, & x);

)

)

/ / Visit the two-dimensional array

for (long i = 0; i <3; + + i)

(

for (long j = 0; j <3; + + j)

(

Demen [1] = i;

Demen [0] = j;

long x (0);

SafeArrayGetElement (pArray, Demen, & x);

cout <<"("<< i <<","<< j << ")" <<x <<endl;

)

)

SafeArrayDestroy (pArray);

Method six: Use SafeArrayCreateEx create a structure that contains one-dimensional array

SAFEARRAY transmission using UDT (since the definition of structure) is a commonly used technology, MSDN document describes a relatively complete, it is necessary to note that, since the definition of the structure requires its own GUID, which must be defined in the IDL file. At the same time must also be IRecordInfo to use interface that will be passed along with the array, IRecordInfo interface UDT within the description of records information.

IDL file:

[uuid (810930AA-9229-46e7-B20C-41F6218D0B1A)]

struct _BookMarkSchema

(

BSTR Name;

BSTR Context;

BSTR Time;

);

...

interface IShape: IDispatch

(

[id (6), helpstring ( "Access is the name of a user's bookmark list")] HRESULT GetBookMarkName ([in] BSTR UserID, [out] SAFEARRAY (struct _BookMarkSchema) * pBookMarkNames);

)

library SarstShapeLib

(

importlib ( "stdole2.tlb");

[

uuid (DBDCC0F1-38F3-4EB4-A5BD-79A3707BDE9C),

helpstring ( "Shape Class")

]

coclass Shape

(

[default] interface IShape;

);

struct _BookMarkSchema;

);

Methods to achieve as follows:

STDMETHODIMP CShape:: GetBookMarkName (BSTR UserID, SAFEARRAY ** pBookMarkNames)

(

/ / Access to GIS information database

CSarstConfigure Configure;

string Flag ( "GIS");

string IP, Database, UserName, Key, Context;

Configure.GetDatabaseInfo (Flag, IP, Database, UserName, Key, Context);

/ / Read attribute data layers

USES_CONVERSION;

string user (CString (UserID). GetBuffer ());

string sql ( "SELECT book signatures, bookmarks description, time FROM users where users bookmark Table ID =''''"+ user +"'''' order by time desc");

FBData data (IP, Database, UserName, Key);

table t = data.GetTable (sql);

if (t.empty ())

(

return S_FALSE;

)

/ / Create SafeArray

IRecordInfo * pRecordInfo = NULL;

HRESULT hr =:: GetRecordInfoFromGuids (LIBID_SarstShapeLib, 1,0, GetUserDefaultLCID (), IID_STRUCT_BookMarkSchema, & pRecordInfo);

if (FAILED (hr))

return E_FAIL;

* pBookMarkNames =:: SafeArrayCreateVectorEx (VT_RECORD, 0, long (t.size () -1), (void *) pRecordInfo);

_BookMarkSchema * PData = NULL;

hr =:: SafeArrayAccessData (* pBookMarkNames, (void **) & pData);

for (int i = 0; i <int (t.size () -1); i + +)

(

t [i +1]. at (0). CopyTo (& pData [i]. Name);

t [i +1]. at (1). CopyTo (& pData [i]. Context);

t [i +1]. at (2). ChangeType (VT_BSTR);

t [i +1]. at (2). CopyTo (& pData [i]. Time);

)

:: SafeArrayUnaccessData (* pBookMarkNames);

pRecordInfo-> Release ();

return S_OK;

)

Visit SAFEARRAY:

Method one: Use SafeArrayAccessData method

This method can be see on the way to create a SAFEARRAY

Please note that after the visit SafeArrayUnaccessData method to call and call to destroy the array SafeArrayDestroy

This approach is usually used to access an array

Method 2: Use SafeArrayGetElement and SafeArrayPutElement

This method can be see on the way to create a SAFEARRAY of five

This multi-dimensional array in a time when the visit very useful

Component / customers pass SAFEARRAY principles:

1) in the pile to create the array SAFEARRAY

2) a party to create, one recovery

3) the receiving party can not amend the SAFEARRAY data can only be read or destruction of



C/C++ Tutorial Articles


Can't Find What You're Looking For?


Rating: 5.0

Comments

No comments posted.