Home > database >  SafeArrayAllocDescriptor API function calls, link mistakes, ask for help!
SafeArrayAllocDescriptor API function calls, link mistakes, ask for help!

Time:09-18

Development platform, labwindows/cvi 8.0

H # include "Windows."
# include "oleauto. H"

Int __stdcall WinMain (HINSTANCE HINSTANCE, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
//I call an API function,
SAFEARRAY * dAry=NULL;
Long aa [16]=,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 {0};
Retrieves the hr=SafeArrayAllocDescriptor (1, & amp; DAry);
DAry - & gt; CbElements=sizeof (CIOread [0]);
DAry - & gt; Rgsabound [0]. CElements=16;
DAry - & gt; Rgsabound [0]. LLbound=0;
SafeArrayAllocData (dAry);
}

Compile the link error after
Undefined symbol '_SafeArrayAllocDescriptor @ 8' referenced in "ZDXLCOMM. C".
Undefined symbol '_SafeArrayAllocData @ 4' referenced in "ZDXLCOMM. C".

Add oleaut32 in engineering. The lib also not line,
Master, please give advice or comments! Thank you

CodePudding user response:

The use of the safeArray


Method one: use SafeArrayAllocDescriptor create a one-dimensional array on the stack



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

Long nData [10]=,2,3,4,5,6,7,8,9,10 {1};

SAFEARRAY * pArray=NULL;

Retrieves the hr=SafeArrayAllocDescriptor (1, & amp; PArray);//create SAFEARRAY structure of object

If (hr!=S_OK)

return;

PArray - & gt; CbElements=sizeof (nData [0]);

PArray - & gt; Rgsabound [0]. CElements=10;

PArray - & gt; Rgsabound [0]. LLbound=0;

PArray - & gt; pvData=https://bbs.csdn.net/topics/nData;

PArray - & gt; FFeatures=FADF_AUTO | FADF_FIXEDSIZE;//FADF_AUTO specified data allocated on the stack, and you can't change size (fixed for 10)

//access SAFEARRAY array

Long * pValue=https://bbs.csdn.net/topics/NULL;

SafeArrayAccessData (pArray, (void * *) & amp; PValue);

for(int i=0; Irgsabound [0]. CElements; I++)

{

Cout

}

SafeArrayUnaccessData (pArray);

SafeArrayDestroy (pArray);

This method of array elements of the space allocated on the stack, namely nData array used in space

Method 2: use SafeArrayAllocDescriptor and SafeArrayAllocData on the heap to create one dimensional array

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

Long nData [10]=,2,3,4,5,6,7,8,9,10 {1};

SAFEARRAY * pArray=NULL;

Retrieves the hr=SafeArrayAllocDescriptor (1, & amp; PArray);//create SAFEARRAY structure of object

If (hr!=S_OK)

return;

PArray - & gt; CbElements=sizeof (nData [0]);

PArray - & gt; Rgsabound [0]. CElements=10;

PArray - & gt; Rgsabound [0]. LLbound=0;

SafeArrayAllocData (pArray);//note: this sentence to write in front to pvData assignment, or used to pArray nData assignment after using SafeArrayAllocData will reset the data

PArray - & gt; pvData=https://bbs.csdn.net/topics/nData;

PArray - & gt; FFeatures=FADF_AUTO | FADF_FIXEDSIZE;//FADF_AUTO specified data allocated on the stack, and you can't change size (fixed for 10)

//access SAFEARRAY array

Long * pValue=https://bbs.csdn.net/topics/NULL;

SafeArrayAccessData (pArray, (void * *) & amp; PValue);

for(int i=0; Irgsabound [0]. CElements; I++)

{

Cout

}

SafeArrayUnaccessData (pArray);

SafeArrayDestroy (pArray);

Method 3: use the SafeArrayAllocDescriptor and SafeArrayAllocData on the heap to create two-dimensional array

//define and initialization setting SafeArray store first line three elements, the second line of two dimensional array of four elements

SAFEARRAY * pArray=NULL;

Retrieves the hr=SafeArrayAllocDescriptor (2, & amp; PArray);

If (hr!=S_OK)

return;

PArray - & gt; CbElements=sizeof (long);

PArray - & gt; Rgsabound [0]. CElements=3;

PArray - & gt; Rgsabound [0]. LLbound=0;

PArray - & gt; Rgsabound [1]. CElements=4;

PArray - & gt; Rgsabound [1]. LLbound=0;

SafeArrayAllocData (pArray);

//for safeArray elements give value

Long lDimension [2]={0};

Long element=1;//safeArray element value

//first line assignment

for(int i=0; I<3; I++)

{

LDimension [0]=I;//column

LDimension [1]=0;//

SafeArrayPutElement (pArray lDimension, (void *) & amp; Element);

Element++;

}

//the second line assignment

for(int i=0; I<4. I++)

{

LDimension [0]=I;//column

LDimension [1]=1;//

SafeArrayPutElement (pArray lDimension, (void *) & amp; Element);

Element++;

}

//get element

for(int i=0; I<3; I++)//first line

{

Long getElem (0);

LDimension [0]=I;

LDimension [1]=0;

SafeArrayGetElement (pArray lDimension, (void *) & amp; GetElem);

Cout

}

Cout

for(int i=0; I<4. I++)//the second line

{

Long getElem (0);

LDimension [0]=I;

LDimension [1]=1;

SafeArrayGetElement (pArray lDimension, (void *) & amp; GetElem);

Cout

}

Cout

When using two-dimensional SAFEARRAY array subscript note, here is the list of main sequence, namely lDimension [1] on behalf of the line, lDimension [0] on behalf of the column,

Method 4: use SafeArrayCreate on the heap to create one dimensional array

Long nData [10]=,2,3,4,5,6,7,8,9,10 {1};

SAFEARRAYBOUND Bound [1]={0};

Bound [0]. LLbound=0;

Bound [0]. CElements=10;

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

PArray - & gt; PvData=https://bbs.csdn.net/topics/nData;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related