📄 comlib.cpp
字号:
*/HRESULT SysFreeString ( BSTR bstr /* String to release */ ) { if (bstr) comSysFree (((DWORD*)bstr) - 1); return S_OK; }/**************************************************************************** SysStringByteLen - Calculates the number of bytes in a BSTR.** Public API for finding the number of bytes in a BSTR (not the number of * wide-characters).** RETURNS: Number of bytes in a BSTR, or 0 if the BSTR is NULL.*/DWORD SysStringByteLen ( BSTR bstr /* String to calculate size of */ ) { if (bstr == NULL) return 0; return * (((DWORD*)bstr) - 1); }/**************************************************************************** SysStringLen - Calculate length of BSTR** Public API for finding length of a BSTR. Note that this may differ from * the value returned by comWideStrLen() or wcslen() if the originating * string contained embedded NULLs.** RETURNS: Number of OLECHAR in the string.*/DWORD SysStringLen ( BSTR bstr /* String to calculate length of */ ) { return SysStringByteLen (bstr) / 2; }/**************************************************************************** comStreamCreate - creates a memory-based IStream from raw memory** This function creates an in-memory stream, as an <IStream>* implementation, based on the contents of the initial* memory-block. The stream allocates a new block to hold the contents,* and any changes occur inside the newly-allocated block, not inside* the original block. Its contents can be accessed by rewinding the* stream, finding its length (using SEEK_END) and then Read()ing the* entire stream.** On entry, the arguments 'pMem' and 'len' should describe a block of* memory which is to be copied into the new stream object. The* argument 'ppStream' should point to an 'IStream*' variable which* will be set to point to the resulting stream object upon successful* completion of the function.** RETURNS:* \is* \i S_OK* On Success* \i E_OUTOFMEMORY if it is unable to allocate sufficient memory.* \ie*/HRESULT comStreamCreate ( const void* pMem, /* address of initial memory */ unsigned long len, /* length of initial memory */ IStream** ppStream /* address of output variable */ ) { TRACE_CALL; // Create a memory-stream object (will have 1 ref if successful) ISimpleStream* pStrm; HRESULT hr = VxRWMemStream::CreateInstance (0, IID_ISimpleStream, (void**) &pStrm); if (FAILED (hr)) return hr; // Write data into stream, then rewind it... pStrm->insert (pMem, len); pStrm->locationSet (0); // Hand out a single reference to its IStream interface hr = pStrm->QueryInterface (IID_IStream, (void**) ppStream); // If QI was successful, this will leave the caller with the // only ref, if not it will destroy the object... pStrm->Release (); return hr; }/**************************************************************************** comWideToAscii - convert a wide-string to a narrow (ascii) string** This function simply converts a string consisting of 2-byte* characters to its equivalent ASCII string. No character-set* conversions are performed, the character values are simply narrowed* to 8 bits. On entry, the arguments 'result' and 'maxLen' describe* the output buffer where the resulting 8-bit string will be placed,* and 'wstr' points to a NULL-terminated wide-character string to be* converted.** RETURNS: the number of characters converted.**/int comWideToAscii ( char* result, /* resulting ascii string */ const OLECHAR* wstr, /* input wide-string */ int maxLen /* max length to convert */ ) { TRACE_CALL; return vxcom_wcstombs (result, wstr, maxLen); }/**************************************************************************** comAsciiToWide - convert a narrow (ascii) string to a wide-string** This function simply converts a string consisting of single-byte* ASCII characters to its equivalent wide-string. No character-set* conversions are performed, the character values are simply widened* to 16 bits. On entry, the arguments 'result' and 'maxLen' describe* the output buffer where the resulting double-byte string will be* placed, and 'str' points to a NULL-terminated string to be converted.** RETURNS: the number of characters converted.**/int comAsciiToWide ( OLECHAR* result, /* resulting wide string */ const char* str, /* input string */ int maxLen /* max length to convert */ ) { TRACE_CALL; return vxcom_mbstowcs (result, str, maxLen); }/**************************************************************************** comWideStrLen - find the length of a wide-string** This function returns the length of the NULL-terminated* wide-character string at 'wsz'.** RETURNS: the number of characters in the string.**/size_t comWideStrLen ( const OLECHAR* wsz /* wide string */ ) { TRACE_CALL; return vxcom_wcslen (wsz); }/**************************************************************************** comWideStrCopy - make a copy of a wide-string** This function copies the NULL-terminated wide-character string at* 'wszSrc' to the location pointed to by 'wszDst' which must contain* enough space to hold the resulting string.** RETURNS: the address of the copy of the wide-string**/OLECHAR* comWideStrCopy ( OLECHAR* wszDst, /* destination */ const OLECHAR* wszSrc /* source */ ) { TRACE_CALL; return vxcom_wcscpy (wszDst, wszSrc); }/**************************************************************************** comT2OLEHelper - Helper function for T2OLE macro.** This function is used internaly by the T2OLE macro to convert the* ASCII string into a wide string.** RETURNS: The address of the wide string.**.NOMANUAL*/OLECHAR * comT2OLEHelper ( void * pvWSZ, /* pointer to wide string */ const char * psz /* pointer to ASCII string */ ) { OLECHAR* pwsz = (OLECHAR*) pvWSZ; COM_ASSERT(pvWSZ); comAsciiToWide (pwsz, psz, strlen (psz) + 1); return pwsz; }/**************************************************************************** comOLE2THelper - Helper function for OLE2T macro.** This function is used internaly by the OLE2T macro to convert the* wide string into an ASCII string.** RETURNS: The address of the ASCII string.**.NOMANUAL*/char * comOLE2THelper ( void * pvSZ, /* pointer to ASCII string */ const OLECHAR * pwsz /* pointer to wide string. */ ) { char* psz = (char*) pvSZ; COM_ASSERT(pvSZ); comWideToAscii (psz, pwsz, vxcom_wcslen (pwsz) + 1); return psz; }/* CComBSTR methods -- most are inline, but some are defined here... */CComBSTR& CComBSTR::operator= (const CComBSTR& src) { TRACE_CALL; if (m_str != src.m_str) { if (m_str) ::SysFreeString (m_str); m_str = src.Copy (); } return *this; }CComBSTR& CComBSTR::operator= (LPCOLESTR pwsz) { TRACE_CALL; ::SysFreeString (m_str); m_str = ::SysAllocString (pwsz); return *this; }void CComBSTR::Append (LPCOLESTR pwsz, int nLen) { TRACE_CALL; int myLen = Length (); BSTR bs = ::SysAllocStringLen (0, myLen + nLen); memcpy (bs, m_str, myLen * sizeof (OLECHAR)); memcpy (bs + myLen, pwsz, nLen * sizeof (OLECHAR)); bs [myLen + nLen] = 0; ::SysFreeString (m_str); m_str = bs; }/* VARIANT-related APIs. *//**************************************************************************** VariantInit - Initialize a VARIANT.** Initialize a VARIANT, regardless of its current contents, to be VT_EMPTY.** RETURNS: void**/void VariantInit (VARIANT* v) { if (v == NULL) return; memset (v, 0, sizeof (VARIANT)); v->vt = VT_EMPTY; }/**************************************************************************** VariantClear - clear the contents of a VARIANT.** Clear the contents of a VARIANT back to VT_EMPTY, but taking account of * the current contents value. So, if the VARIANT is currently representing * a IUnknown pointer, that pointer will be released in this function. * Also, BSTRs and SAFEARRAY are freed.** RETURNS:* \is* \i S_OK* On Success.* \i E_INVALIDARG* If v is null or otherwise invalid.* \i DISP_E_BARVARTYPE* The VARIANT is not one of the following allowed types: VT_EMPTY, VT_NULL, * VT_I2, VT_I4, VT_R4, VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_ERROR, VT_BOOL, * VT_UNKNOWN, VT_UI1.* \ie*/HRESULT VariantClear (VARIANT* v) { HRESULT hr = S_OK; // check for NULL variant if (v == NULL) return E_INVALIDARG; // check for types of VARIANTs we can handle // and mask out ARRAY bit. switch (V_VT(v) & 0xDFFF) { case VT_EMPTY: case VT_NULL: case VT_I2: case VT_I4: case VT_R4: case VT_R8: case VT_CY: case VT_DATE: case VT_BSTR: case VT_ERROR: case VT_BOOL: case VT_UNKNOWN: case VT_UI1: break; default: return DISP_E_BADVARTYPE; } if (V_ISARRAY (v)) { hr = SafeArrayDestroy (V_ARRAY(v)); if (SUCCEEDED (hr)) { VariantInit (v); } } else { switch (V_VT(v)) { case VT_BSTR: hr = SysFreeString (V_BSTR(v)); break; case VT_UNKNOWN: if (V_UNKNOWN(v)) V_UNKNOWN(v)->Release (); break; } VariantInit (v); } return hr; }/**************************************************************************** VariantCopy - Copy a VARIANT. ** Frees the destination variant and makes a copy of the source variant.* * If s is a BSTR a copy of the BSTR is made.** Copying SAFEARRAYs isn't supported so E_INVALIDARG is returned if this * is attempted.** RETURNS:* \is* \i S_OK* On success* \i E_INVALIDARG* If source and destination are the same.* \i E_POINTER* One of the VARIANTs is NULL or invalid.* \i E_OUTOFMEMORY* If there isn't enough memory to allocate the new VARIANT.* \ie*/HRESULT VariantCopy ( VARIANT * d, /* Pointer to the VARIANT to receive the copy */ VARIANT * s /* Pointer to the VARIANT to be copied */ ) { HRESULT hr = S_OK; if (s == NULL) { return E_POINTER; } if (d == NULL) { return E_POINTER; } if (V_ISARRAY (s)) { hr = VariantClear (d); if (FAILED (hr)) return hr; if (FAILED (hr)) { return hr; } /* SPR#67806. set VT in dest. */ V_VT(d) = V_VT(s); return SafeArrayCopy (V_ARRAY(s), &(V_ARRAY(d))); } if (d != s) { hr = VariantClear (d); /* SPR#67806. set VT in dest. */ V_VT(d) = V_VT(s); if (SUCCEEDED (hr)) { memcpy (d, s, sizeof(VARIANT)); switch (V_VT (s)) { case VT_BSTR: V_BSTR(d) = SysAllocString (V_BSTR(s)); /* Check to see if the Alloc failed */ if ((V_BSTR (s) != NULL) && (V_BSTR (d) == NULL)) { hr = E_OUTOFMEMORY; } break; case VT_UNKNOWN: if (V_UNKNOWN(s)) { hr = V_UNKNOWN(s)->QueryInterface(IID_IUnknown, (void **)&V_UNKNOWN(d)); } break; } } } else { hr = E_INVALIDARG; } return hr; }/**************************************************************************** GetVariantValue - Get certain VARIANT values as doubles.** This function converts certain VARIANT values into doubles.** RETURNS: The value of the VARIANT as a double or zero.*.NOMANUAL*/static double GetVariantValue ( VARIANT * v ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -