COM编程--字符串结构体转换
2014-03-17 10:16
239 查看
1、将结构体保存到VARIANT中
调用:
解析:
// @desc : 转为安全数组 HRESULT Hr; SAFEARRAY * pSArray; CComQIPtr<IRecordInfo> spRecordInfo; Hr = ::GetRecordInfoFromGuids(LIBID_VT_CommUtiLib, 1, 0, LOCALE_USER_DEFAULT, __uuidof(TAvailableRoom), &spRecordInfo); if( S_OK != Hr ) { return; } pSArray = ::SafeArrayCreateVectorEx( VT_RECORD, 0, nSize, (void*)spRecordInfo ); TAvailableRoom* pData = NULL; Hr = ::SafeArrayAccessData(pSArray,(void**)&pData); TAvailableRoom *pDElement; for( LONG n = 0; n < nSize; n++ ) { // @desc : SM_MEETINGINFO& SElementRef = vtMeetingRoom ; pDElement = &pData ; // @desc : Copy pDElement->nConfID = SElementRef.nConfID; pDElement->bstrConfName = CComBSTR( SElementRef.szConfName ).Detach(); pDElement->nMaxCount = SElementRef.wMaxCount; pDElement->nCurCount = SElementRef.wCurCount; } ::SafeArrayUnaccessData( pSArray ); V_ARRAY( pVal ) = pSArray; V_VT( pVal ) = VT_ARRAY | VT_RECORD;
调用:
VARIANT varArray; GetAvailableRooms(&varArray); GetAvailableRooms( VARIANT * pVal, LONG *pnErrCode)
解析:
CSafeArrayWarper arrConf; arrConf.Attach(variant.parray); CString sVal; LONG nMaxSize = arrConf.GetCount(); if (nMaxSize) { TVTRoomAvailable* pData = NULL; TVTRoomAvailable* pEle = NULL; HRESULT hr = arrConf.AccessData((void**)&pData); ASSERT(SUCCEEDED(hr)); for (LONG i = arrConf.GetLowerBound(); i < nMaxSize; i++) { pEle = &pData[i]; sVal = pEle->bstrConfName; SM_MEETINGINFO* pInfo = new SM_MEETINGINFO; if (pInfo) { _stprintf_s(pInfo->szConfName, _countof(pInfo->szConfName), _T("%s"), pEle->bstrConfName); pInfo->nConfID = pEle->nConfID; pInfo->wMaxCount = pEle->nMaxCount; pInfo->wCurCount = pEle->nCurCount; m_vecMeetingInfo.push_back(pInfo); } } arrConf.UnAccessData(); } arrConf.Detach();
#pragma once class CSafeArrayWarper { public: CSafeArrayWarper(void) : m_psa(NULL) {}; ~CSafeArrayWarper(void) { HRESULT hRes = Destroy(); ATLASSERT(SUCCEEDED(hRes)); }; public: HRESULT Attach(const SAFEARRAY *psaSrc) { HRESULT hRes = S_OK; m_psa = const_cast<LPSAFEARRAY>(psaSrc); hRes = Lock(); return hRes; } LPSAFEARRAY Detach() { Unlock(); LPSAFEARRAY pTemp = m_psa; m_psa = NULL; return pTemp; } HRESULT Destroy() { HRESULT hRes = S_OK; if (m_psa != NULL) { hRes = Unlock(); if (SUCCEEDED(hRes)) { hRes = SafeArrayDestroy(m_psa); if (SUCCEEDED(hRes)) m_psa = NULL; } } return hRes; } public: LPSAFEARRAY* GetSafeArrayPtr() throw() { return &m_psa; } HRESULT GetSafeArrayData(void HUGEP** ppvData) throw() { HRESULT hRes = S_OK; if (m_psa != NULL) { hRes = ::SafeArrayAccessData(m_psa, ppvData); } return hRes; } LONG GetLowerBound(UINT uDim = 0) const { ATLASSUME(m_psa != NULL); LONG lLBound = 0; HRESULT hRes = SafeArrayGetLBound(m_psa, uDim+1, &lLBound); if (FAILED(hRes)) AtlThrow(hRes); return lLBound; } LONG GetUpperBound(UINT uDim = 0) const { ATLASSUME(m_psa != NULL); LONG lUBound = 0; HRESULT hRes = SafeArrayGetUBound(m_psa, uDim+1, &lUBound); if (FAILED(hRes)) AtlThrow(hRes); return lUBound; } ULONG GetCount(UINT uDim = 0) const { ATLASSUME(m_psa != NULL); ATLASSERT(uDim < GetDimensions()); LONG lLBound, lUBound; HRESULT hRes = SafeArrayGetLBound(m_psa, uDim+1, &lLBound); ATLASSERT(SUCCEEDED(hRes)); if(FAILED(hRes)) AtlThrow(hRes); hRes = SafeArrayGetUBound(m_psa, uDim+1, &lUBound); ATLASSERT(SUCCEEDED(hRes)); if(FAILED(hRes)) AtlThrow(hRes); return (lUBound - lLBound + 1); } UINT GetDimensions() const { ATLASSUME(m_psa != NULL); return SafeArrayGetDim(m_psa); } bool IsSizable() const { ATLASSUME(m_psa != NULL); if(m_psa == NULL) AtlThrow(E_FAIL); return (m_psa->fFeatures & FADF_FIXEDSIZE) ? false : true; } public: // void HUGEP* GetAt(LONG lIndex) const // { // ATLASSUME(m_psa != NULL); // if(m_psa == NULL) // AtlThrow(E_FAIL); // // LONG lLBound = GetLowerBound(); // ATLASSERT(lIndex >= lLBound); // ATLASSERT(lIndex <= GetUpperBound()); // if ((lIndex < lLBound) || (lIndex > GetUpperBound())) // AtlThrow(E_INVALIDARG); // // return ((_ATL_AutomationType<T>::_typewrapper*)m_psa->pvData)[lIndex-lLBound]; // } // // typename _ATL_AutomationType<T>::_typewrapper& GetAt(LONG lIndex) // { // ATLASSUME(m_psa != NULL); // if(m_psa == NULL) // AtlThrow(E_FAIL); // // LONG lLBound = GetLowerBound(); // ATLASSERT(lIndex >= lLBound); // ATLASSERT(lIndex <= GetUpperBound()); // if ((lIndex < lLBound) || (lIndex > GetUpperBound())) // AtlThrow(E_INVALIDARG); // // return ((_ATL_AutomationType<T>::_typewrapper*)m_psa->pvData)[lIndex-lLBound]; // } protected: HRESULT Lock() { return S_OK; // ATLASSUME(m_psa != NULL); // return SafeArrayLock(m_psa); } HRESULT Unlock() { return S_OK; // ATLASSUME(m_psa != NULL); // return SafeArrayUnlock(m_psa); } private: SAFEARRAY* m_psa; };
相关文章推荐
- iOS开发-CGRect,CGSize结构体与NSString字符串互相转换
- 结构体/字符串之间的转换
- 字符串转换整形数据+结构体数组排序
- Golang 父子对象与JSON互相转换以及struct结构体内部的反引号该如何定义字段编码生成json字符串的写法
- 字符串转换整形数,结构体数组排序
- 将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回
- iOS开发-CGRect,CGSize结构体与NSString字符串互相转换
- 结构体指针与字符串转换
- 字符串转换整形数据+结构体数组排序
- 字符串转换整形数据+结构体数组排序
- oc中将CGRect、CGSize、CGPoint等结构体转换为字符串
- 使用Tuxedo将C语言中的结构体转换成Xml格式的字符串
- 使用Tuxedo将C语言中的结构体转换成Xml格式的字符串
- 将time_t格式的字符串转换成具体的时间, 存放在SystemTime结构体中
- oc中将CGRect、CGSize、CGPoint等结构体转换为字符串
- json字符串与json对象的相互转换
- json字符串转换为JSONObject和JSONArray
- 自定义对象属性为空自动转换为字符串
- 关于URL截取?后面的字符串并转换成对象的问题简述
- Java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json