📄 comlib.cpp
字号:
pMalloc->Release (); }/**************************************************************************** CoCreateGuid - create a GUID** This routine creates a new GUID and copies it into the GUID pointed to* by the <pGuid> argument if successful. If the routine encounters an* error creating the GUID, the GUID pointed to by <pGuid> will be left* unchanged.** RETURNS:* \is* \i S_OK* If successful* \i RPC_S_UUID_NO_ADDRESS* If a hardware ethernet address cannot be obtained* \i E_POINTER* If pGuid is NULL* \i RPC_S_INTERNAL_ERROR* For all other errors.* \ie*/HRESULT CoCreateGuid ( GUID* pGuid /* ptr to GUID where result will be returned */ ) { if (pGuid == NULL) { return E_POINTER; } comSysGuidCreate (pGuid); return S_OK; }/**************************************************************************** WriteClassStm - Writes a CLSID to a stream.** This function writes a CLSID to a stream.** RETURNS:* \is* \i S_OK* On Success* \i E_FAIL* On failure* \ie*/HRESULT WriteClassStm ( IStream * pStm, /* IStream to store in. */ REFCLSID rclsid /* CLSID to be stored in stream */ ) { TRACE_CALL; ULONG nb; return pStm->Write (&rclsid, sizeof (CLSID), &nb); }/**************************************************************************** ReadClassStm - Reads a CLSID from a stream.** This function reads a CLSID from a stream.** RETURNS:* \is* \i S_OK* On success* \i E_FAIL* On failure* \ie*/HRESULT ReadClassStm ( IStream * pStm, /* stream holding the CLSID */ CLSID * pclsid /* output CLSID */ ) { TRACE_CALL; ULONG nb; return pStm->Read (pclsid, sizeof (CLSID), &nb); }/**************************************************************************** CLSIDFromString - Convert a wide string-format CLSID to a CLSID structure.** This function converts a wide string-format CLSID to a CLSID structure.* The string must be of the following format * {ABCDEFGH-IJKL-MNOP-QRST-UVWXYZ123456}.** This function differs from the standard OLE function in that it doesn't* check that the CLSID is in the registry.** RETURNS:* \is* \i S_OK* On success* \i CO_E_CLASSSTRING* If the string is in the wrong format* \i E_POINTER* If a NULL pointer is given for a parameter.* \ie*/HRESULT CLSIDFromString ( LPCOLESTR wszClsid, /* Wide string to convert */ LPCLSID pClsid /* Pointer to CLSID which will contain result */ ) { TRACE_CALL; char s [GUID_STRING_LEN]; if (wszClsid == NULL) { return E_POINTER; } // convert to ascii from wide-string vxcom_wcstombs (s, wszClsid, sizeof (s)); // use utility function to get conversion return CLSIDFromAscii (s, pClsid); }/**************************************************************************** vxcomGUID2String - Helper function that converts a GUID to string format.** Helper function that converts a GUID to string format.** RETURNS: The converted GUID as an ASCII string.**.NOMANUAL*/const char * vxcomGUID2String ( REFGUID guid /* GUID to convert */ ) { static char s [GUID_STRING_LEN]; comCoreGUID2String (&guid, s); return s; }/**************************************************************************** StringFromGUID2 - basic GUID -> string conversion utility.** This function converts a GUID structure into a wide string representation.* lpsz must point to a valid wide string buffer allocated by CoTaskMemAlloc.** RETURNS: The length of the resultant string or 0 is the buffer is to small * to hold the resultant string.*/int StringFromGUID2 ( REFGUID rguid, /* IID to be converted */ LPOLESTR lpsz, /* resulting string */ int cbMax /* max size of returned string */ ) { TRACE_CALL; char sGuid [GUID_STRING_LEN]; if (lpsz == NULL) return 0; strcpy (sGuid, (vxcomGUID2String (rguid))); // Check that result will fit. if (cbMax < (strlen (sGuid) + 1)) return 0; // Trim to match available output space if ((size_t)cbMax < sizeof (sGuid)) sGuid [cbMax] = 0; // Convert to wide-char... vxcom_mbstowcs (lpsz, sGuid, strlen (sGuid) + 1); // Return num chars in string... return strlen (sGuid) + 1; }/**************************************************************************** StringFromCLSID - converts a CLSID to a wide-string format.* * This function converts a CLSID to a wide-string format. This routine * allocates the correct amount of memory and returns it via ppsz. This* memory must be released using CoTaskMemFree.** RETURNS:* \is* \i S_OK* On success* \i E_OUTOFMEMORY* If not enough memory could be allocated to hold the string.* \ie*/HRESULT StringFromCLSID ( REFCLSID rclsid, /* CLSID to be converted */ LPOLESTR* ppsz /* output var to receive string */ ) { TRACE_CALL; /* Check for NULL parameter */ if (ppsz == NULL) return E_POINTER; *ppsz = (LPOLESTR) CoTaskMemAlloc (GUID_STRING_LEN * sizeof (OLECHAR)); if (*ppsz == NULL) { return E_OUTOFMEMORY; } StringFromGUID2 (rclsid, *ppsz, GUID_STRING_LEN); return S_OK; }/**************************************************************************** StringFromIID - converts a IID to a wide-string format.* * This function converts a IID to a wide-string format. This routine * allocates the correct amount of memory and returns it via ppsz. This* memory must be released using CoTaskMemFree.** RETURNS:* \is* \i S_OK* On success* \i E_OUTOFMEMORY* If not enough memory could be allocated to hold the string.* \ie*/HRESULT StringFromIID ( REFIID riid, /* IID to be converted */ LPOLESTR* ppsz /* output var to receive string */ ) { TRACE_CALL; *ppsz = (LPOLESTR) CoTaskMemAlloc (GUID_STRING_LEN * sizeof (OLECHAR)); if (*ppsz == NULL) { return E_OUTOFMEMORY; } StringFromGUID2 (riid, *ppsz, GUID_STRING_LEN); return S_OK; }/**************************************************************************** IIDFromString - Convert a wide string-format IID to a IID structure.** This function converts a wide string-format IID to a IID structure.* The string must be of the following format * {ABCDEFGH-IJKL-MNOP-QRST-UVWXYZ123456}.** RETURNS:* \is* \i S_OK* On success.* \i E_INVALIDARG* If the string is in the wrong format or the return pointer is invalid.* \ie*/HRESULT IIDFromString ( LPCOLESTR lpsz, /* string representation of IID */ LPIID piid /* pointer to IID */ ) { TRACE_CALL; HRESULT hr; hr = CLSIDFromString (lpsz, piid); if (hr == CO_E_CLASSSTRING) return E_INVALIDARG; return hr; }/**************************************************************************** IsEqualGUID - Tests if two GUIDs are equivalent.** This function tests if two GUIDs are equivalent.** RETURNS:* \is* \i S_OK* On success* \i CO_E_CLASSSTRING* If the string is in the wrong format.* \ie*/BOOL IsEqualGUID ( REFGUID guid1, /* GUID to compare to guid2 */ REFGUID guid2 /* GUID to compare to guid1 */ ) { TRACE_CALL; return (guid1 == guid2) ? TRUE : FALSE; }/**************************************************************************** IsEqualCLSID - Tests if two CLSIDs are equivalent.** This function tests if two CLSIDs are equivalent.** RETURNS:* \is* \i S_OK* On success* \i CO_E_CLASSSTRING* If the string is in the wrong format.* \ie*/BOOL IsEqualCLSID ( REFCLSID clsid1, /* CLSID to compare to clsid2 */ REFCLSID clsid2 /* CLSID to compare to clsid1 */ ) { TRACE_CALL; return IsEqualGUID (clsid1, clsid2); }/**************************************************************************** IsEqualIID - Tests if two IIDs are equivalent.** This function tests if two IIDs are equivalent.** RETURNS:* \is* \i S_OK* On success* \i CO_E_CLASSSTRING* If the string is in the wrong format.* \ie*/BOOL IsEqualIID ( REFIID iid1, /* IID to compare to iid2 */ REFIID iid2 /* IID to compare to iid1 */ ) { TRACE_CALL; return IsEqualGUID (iid1, iid2); }/*** This section implements the BSTR data type, named after the 'Basic* String' type from MSVisual Basic. It is a counted wide-string, but* with the byte-count stored before the character array, and the* address of a BSTR is considered to be the address of the character* array. Hence, the count-word is 'in front of' the pointer that* points to it:-** DWORD wchar wchar wchar wchar wchar* ^* BSTR points here...**//**************************************************************************** SysAllocString - Allocates a new string and copies the existing string into it.** This function allocates a new string and copies the existing string to* it.** RETURNS: A pointer to the new string or NULL if memory can't be * allocated or psz was NULL.*/BSTR SysAllocString ( const OLECHAR* psz /* String to copy into new string */ ) { if (psz == NULL) return NULL; // Count the characters, not incl terminating NULL... const OLECHAR* pChar = psz; size_t len=0; while (*pChar++) ++len; // Get a string BSTR bstr = SysAllocStringByteLen (0, (len+1) * sizeof (OLECHAR)); // Copy the string size_t n; for (n=0; n < len; ++n) bstr [n] = psz [n]; bstr [n] = 0; return bstr; }/**************************************************************************** SysAllocStringLen - Create a string of the given length and initialize it from the passed string.** Public API for creating a BSTR of the given length. After allocation, * 'nLen' characters are copied from 'psz' and a NULL character is appended.* If 'psz' includes NULL characters they will be copied also.** RETURNS: A pointer to the new string or NULL if memory can't be allocated.*/BSTR SysAllocStringLen ( const OLECHAR* psz, /* String to initialize new string from */ unsigned long nLen /* Length of new string */ ) { // Get a string BSTR bstr = SysAllocStringByteLen (0, nLen * sizeof (OLECHAR)); // Copy the string, if supplied... if (psz) { for (size_t n=0; n < nLen; ++n) bstr [n] = psz [n]; } bstr [nLen] = 0; return bstr; }/**************************************************************************** SysAllocStringByteLen - Create a string of the given length and initialize it from the passed string.** Public API for creating a BSTR containing 8-bit data, of the given length. * The input arg 'psz' may be NULL in which case the resulting BSTR has * uninitialized data. The argument 'bytes' indicates the number of bytes * to copy from 'psz'. Two NULL characters, or a single NULL OLECHAR, are * placed afterwards, allocating a total of (bytes + 2) bytes.** RETURNS: A pointer to the new string or NULL if memory can't be allocated.*/BSTR SysAllocStringByteLen ( const char* psz, /* String to initialize new string from */ unsigned long nBytes /* number of bytes in new string */ ) { // Allocate the string void* pMem = comSysAlloc (sizeof (DWORD) + nBytes + sizeof (OLECHAR)); if (! pMem) return 0; * ((DWORD*) pMem) = nBytes; BSTR bstr = (BSTR) (((DWORD*) pMem) + 1); if (psz) { memcpy (bstr, psz, nBytes); bstr [nBytes / sizeof (OLECHAR)] = 0; } return bstr; }/**************************************************************************** SysFreeString - Releases the memory allocated to a BSTR.** Public API for freeing BSTRs. The input 'bstr' may be NULL.** RETURNS: S_OK.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -