使用COM提供SafeArray資料,comsafearray資料

來源:互聯網
上載者:User

使用COM提供SafeArray資料,comsafearray資料

    在前一篇博文《讀取SafeArray資料》我們介紹了C#讀取安全陣列;那麼我們的COM怎麼編寫呢?

   1. 定義SAFEARRAY變數

SAFEARRAY *pSArray = NULL;


   2. 釋放此變數

SafeArrayDestroy(pSArray);


   3. 建立向量表

pSArray = SafeArrayCreateVector(VT_UI1, 0, 32);

           向量變數(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 is IDispatch FAR*.

VT_ERROR

Variable type is SCODE.

VT_BOOL

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

VT_VARIANT

Variable type is VARIANT FAR*.

VT_UNKNOWN

Variable type is IUnknown FAR*.

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. 寫入資料

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



      執行個體程式(COM方法):

      AA:屏蔽的數值

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;}




SAFEARRAY類型的資料讀寫的問題(COM)?

SAFEARRAY裡裝的不一定是BSTR.確只有SAFEARRAY不知道它的VAR_TYPE沒用的.
 
教SafeArrayGetElement的用法

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;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.