SAFEARRAY Use instances

Source: Internet
Author: User
Tags arrays uuid
SAFEARRAY Use instancesCategory: VC 2013-08-21 13:09 35 people Read reviews (0) Favorite reports

from:http://blog.csdn.net/csfreebird/article/details/234547

Directory:safearray using the instance directory: Preface: What is SAFEARRAY: Creating SAFEARRAY: Method One: Using Safearrayallocdescriptor to create a one-dimensional array on the stack Method Two: Create a one-dimensional array on the heap using Safearrayallocdescriptor and Safearrayallocdata Method Three: Use Safearrayallocdescriptor and Safearrayallocdata to create a two-dimensional array on the heap method four: Using SafeArrayCreate to create a one-dimensional array on the heap Method Five: Use SafeArrayCreate to create a two-dimensional array on the heap method VI: Use Safearraycreateex to create a one-dimensional array that contains the structure Access SAFEARRAY: Method One: Use the Safearrayaccessdata method Method two: Using Safearraygetelement and SafeArrayPutElement CComSafeArray class Introduction: Basic Introductory Example: NOTE: Preface:SAFEARRAY use always bothers a lot of people, in order to make this problem clear, I put my current knowledge to make a summaryWhat is SAFEARRAY:SAFEARRAY is actually a structure that you can refer to MSDN for this section. Ms-help://ms.   Msdnqtr.2003feb.2052/automat/htm/chap7_9ntx.htm we do not need to care about the definitions under 16-bit operating systems, as our team is only developed under the WIN2000 platform. Create SAFEARRAY: method One: Use Safearrayallocdescriptor to create a one-dimensional array on the stack     //Create SAFEARRAY array, each element is long, and the array is a 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 objects for SAFEARRAY structures       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 Specifies that the data is allocated on the stack, and the size cannot be changed (fixed to ten)          // Access SAFEARRAY arrays      long* pvalue=null;      Safearrayaccessdata (Parray, (void**) &pvalue);      long Low (0), high (0);      Hr=safearraygetlbound (Parray,1,&low);//Dimension index starting from 1      hr= Safearraygetubound (Parray,1,&high);//Dimension index starting from 1        safearrayunaccessdata (Parray);      SafeArrayDestroy (Parray); This method allocates the space occupied by the array elements on the stack, that is, the space used by the ndata array    method Two: Create a one-dimensional array on the heap using Safearrayallocdescriptor and Safearrayallocdata     //Create SAFEARRAY array, each element is long, and the array is a 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 objects for SAFEARRAY structures       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,&AMP;H);      long size=h-l+1;      Safearrayaccessdata (Parray, (void**) &pdata);      for (long Idx=l;idx<size;++idx)      {         pdata[idx]=ndata[idx];     }       Safearrayunaccessdata (Parray);       //Access SAFEARRAY arrays      long* pvalue=null;      Safearrayaccessdata (Parray, (void**) &pvalue);      long Low (0), high (0);      Hr=safearraygetlbound (Parray,1,&low);//Dimension index starting from 1      hr= Safearraygetubound (Parray,1,&high);//Dimension index starting from 1        safearrayunaccessdata (Parray);      SafeArrayDestroy (Parray);    method Three: Create a two-dimensional array on the heap using Safearrayallocdescriptor and Safearrayallocdata        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;     //assignment for the first line      for (long I=0;i<3;++i)      {          ldimension[1]=0;//Line          ldimension[0]=i;//column          safearrayputelement (parray,ldimension,& x);          x + +;     }     //assignment for the second row      for (long I=0;i<3;++i)       {         ldimension[1]=1;//lines           ldimension[0]=i;//Columns          safearrayputelement (PARRAY,LDIMENSION,&AMP;X);          x + +;     }          //Read data from the second row of the third column in SafeArray       long y (0);      ldimension[1]=1;      ldimension[0]=2;      safearraygetelement (parray,ldimension,&y);        SafeArrayDestroy (Parray);   Two-dimensional SAFEARRAY array use the subscript to note that here is the method of column main order, that is, ldimension[1] represents the row, Ldimension[0] represents the column.      method Four: Use SafeArrayCreate to create a one-dimensional array on the heap      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: Create a two-dimensional array on the heap using SafeArrayCreate      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);         }     }       //access to two-dimensional arrays      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 to create a one-dimensional array containing the structureThe passing of a UDT using SAFEARRAY (custom structure) is a common technique, and the MSDN documentation is well-documented, and one thing to note is that the custom structure requires its own GUID, which must be defined in the IDL file. You must also use the IRecordInfo interface, which is passed in and out with the array, and the IRecordInfo interface records the description information for the UDT. IDL file: [UUID (810930AA-9229-46E7-B20C-41F6218D0B1A)] struct _bookmarkschema {     BSTR Name;      BSTR Context;      BSTR time; };   ... interface ishape:idispatch {[ID (6), helpstring ("Get a list of bookmark names belonging to a user")] HRESULT getbookmarkname ([in] BSTR userid,[o UT] SAFEARRAY (struct _bookmarkschema) * pbookmarknames); }     Library sarstshapelib {          importlib ("Stdole2.tlb"); nbsp;    [         uuid ( dbdcc0f1-38f3-4eb4-a5bd-79a3707bde9c),          helpstring ("Shape Class")     ]      coclass Shape      {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.