Use COM to provide SafeArray data and comsafearray data

Source: Internet
Author: User

Use COM to provide SafeArray data and comsafearray data

In the previous blog "read SafeArray data", we introduced C # To Read secure arrays. How can we compile COM?

1. Define the SAFEARRAY variable

SAFEARRAY *pSArray = NULL;


2. Release this variable

SafeArrayDestroy(pSArray);


3. Create a vector table

pSArray = SafeArrayCreateVector(VT_UI1, 0, 32);

Definition of vector variable (VT_UI1): MSDN

VT_EMPTY

Variable type is not specified.

VT_NULL

Variable type is NULL.

VT_I2

Variable type is 2-byte signed INT.

VT_I4

Variable type is 4-byte signed INT.

VT_R4

Variable type is 4-byte real.

VT_R8

Variable type is 8-byte real.

VT_CY

Variable type is currency.

VT_DATE

Variable type is date.

VT_BSTR

Variable type is binary string.

VT_DISPATCH

Variable type isIDispatchFAR *.

VT_ERROR

Variable type is SCODE.

VT_BOOL

Variable type is Boolean; True =-1, False = 0.

VT_VARIANT

Variable type isVARIANTFAR *.

VT_UNKNOWN

Variable type isIUnknownFAR *.

VT_UI1

Variable type is unsigned char.

VT_RESERVED

This element is reserved.

VT_BYREF

Variable type is a pointer to data.

VT_ARRAY

Variable type is a safe array.


4. Write Data

SafeArrayPutElement(pSArray, &l, &inqReport[l]);



Instance Program (COM method ):

AA: Shielded Value

STDMETHODIMP CRepoFmt::getInqRepo(ULONG iStation, SAFEARRAY** ret){SAFEARRAY *pSArray = NULL;unsigned char inqReport[32];unsigned char xorByte = AA;int iStationID = 0;if (iStation < 1) iStationID = 1;elseiStationID = iStation;inqReport[0]  = AA;inqReport[1]  = AA;inqReport[2]  = AA;inqReport[3]  = AA;inqReport[4]  = (iStationID % AA);inqReport[5]  = (iStationID / AA);inqReport[6]  = AA;inqReport[7]  = AA;inqReport[8]  = AA;inqReport[9]  = AA;inqReport[10] = AA;inqReport[11] = AA;for (int i = 1; i < 18; i++)xorByte ^= inqReport[i];inqReport[18] = xorByte;// Fill the inqReport etc.for (int j = 12; j < 18; j++)inqReport[j] = 0;for (int k = 19; k < 32; k++)inqReport[k] = 0;if (!ret)return E_POINTER;if (*ret) {SafeArrayDestroy(pSArray);*ret = NULL;}pSArray = SafeArrayCreateVector(VT_UI1, 0, 32);for (long l = 0; l < 32; l++)SafeArrayPutElement(pSArray, &l, &inqReport[l]);*ret = pSArray;return S_OK;}




Question about reading and writing SAFEARRAY data (COM )?

Not necessarily BSTR is installed in SAFEARRAY. It is useless that SAFEARRAY does not know its VAR_TYPE.

Teach SafeArrayGetElement usage

Example
STDMETHODIMP CEnumPoint: Next (
ULONG celt,
Variant far rgvar [],
ULONG * pceltFetched)
{
Unsigned int I;
Long ix;
HRESULT hresult;

For (I = 0; I <celt; ++ I)
VariantInit (& rgvar [I]);

For (I = 0; I <celt; ++ I ){
If (m_iCurrent = m_celts ){
Hresult = ReportResult (0, S_FALSE, 0, 0 );
Goto LDone;
}

Ix = m_iCurrent ++;
Hresult = SafeArrayGetElement (m_psa, & ix, & rgvar [I]);
If (FAILED (hresult ))
Goto LError0;
}
Hresult = NOERROR;

LDone :;
* PceltFetched = I;
Return hresult;

LError0 :;
For (I = 0; I <celt; ++ I)
VariantClear (& rgvar [I]);
Return hresult;
}

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.