📄 comlib.cpp
字号:
/* comLib.cpp - COM library (VxDCOM) *//* Copyright (c) 1999 Wind River Systems, Inc. *//*modification history--------------------03f,03jan02,nel Remove refs to T2OLE and OLE2T.03e,17dec01,nel Add include symbol for diab build.03d,02nov01,nel Fix docs build.03c,19oct01,nel SPR#71077. Fix error code returned by IIDFromString.03b,19oct01,nel SPR#71068. Fix StringFromCLSID parameter checking.03a,17sep01,nel SPR#70351. Check input parameters on StringFromGUID2.02z,17sep01,nel SPR#70350. Correct length count given by StringFromGUID2 to include null terminator.02y,13sep01,nel SPR#70299. Add checking to VariantCopy on source parameter for invalid VARIANT.02x,12sep01,nel SPR#70277. Fix parameter checking on CLSIDFromAscii.02w,14aug01,nel SPR#69615. CoCreateGUID check for NULL parameter.02v,14aug01,nel SPR#69609. SysAllocString should return NULL when passed a NULL BSTR as input.02u,08aug01,nel SPR#69514. Correct SysAllocStringLen so that it doesn't add two extra nulls.02t,07aug01,dbs use multi-qi in comCoreLib funcs02s,06aug01,dbs simplify vxcomGUID2String02r,02aug01,nel SPR#69373. Add extra checking to VariantInit and VariantClear.02q,30jul01,nel SPR#69346. Add check to VariantClear for VT_IUNKNOWN.02p,26jul01,nel SPR#68698. Add numeric-to-string, string-to-numeric capability to VariantChangeType.02o,26jul01,nel SPR#69289. Fix memory leak in SafeArrayDestroy.02n,23jul01,nel Correct return codes for CoCreateInstance docs.02m,16jul01,dbs correct definition of comLibInit func02l,13jul01,dbs fix up includes02k,03jul01,nel Remove IID_IMalloc because it's now defined in comCoreTypes.idl02j,27jun01,nel Add in extra SAFEARRAY API.02i,27jun01,dbs fix include paths and names02h,20jun01,nel Remove dependencies on rpcDceTypes.h.02g,02may01,nel SPR#66227. Correct format of comments for refgen.02f,08feb01,nel SPR#63885. SAFEARRAYs added. 02e,30may00,nel Add more variant support02d,06apr00,nel use kernel level routine to access TCB structure for task02c,01mar00,nel Correct vxdcomGUID2String so that it can correctly convert signed int to unsigned ints02b,11feb00,dbs IIDs moved to idl directory02a,02feb00,dbs add some Variant-related APIs01z,07jan00,nel Remove dependency on taskTcb to conform to new protection scheme01y,16nov99,nel Corrected new name of pComLocal in WIND_TCB01x,13oct99,dbs add manpage/reference material01w,19aug99,aim change assert to VXDCOM_ASSERT01v,13aug99,aim moved globals to vxdcomGlobals.h01u,28jul99,drm Adding g_defaultServerPriority global.01t,21jul99,dbs fix string-length in OLE2T helper01s,16jul99,aim changed Free to Dealloc01r,15jul99,dbs add assertion-checking to T2OLE support macros01q,17jun99,dbs change argtypes of comMemAlloc01p,04jun99,dbs remove public registry APIs01o,03jun99,dbs fix CoCreateGuid() for non-VxWorks builds01n,03jun99,dbs remove reliance on TLS01m,28may99,dbs simplify allocator strategy01l,28may99,aim fixed scoping on array access in SysAllocString01k,24may99,dbs add BSTR marshaling policy variable01j,20may99,dbs add SysAllocStringByteLen() API01i,10may99,drm removed code in CoCreateGuid() which sets guid to GUID_NULL01h,07may99,drm documentation updates02g,03may99,drm adding preliminary priority scheme support02f,29apr99,dbs fix warnings under -Wall02e,29apr99,dbs must include MemPool.h02d,28apr99,dbs add more mem-alloc funcs02c,27apr99,dbs allocate all BSTRs from same pool02b,27apr99,dbs add alloc helper funcs02a,26apr99,aim added TRACE_CALL01z,23apr99,dbs use streams properly01y,15apr99,dbs improve hard-coded arrays01x,14apr99,dbs export registry-instance function to comShow01w,14apr99,dbs add return-value to GuidMap::erase()01v,14apr99,dbs use GuidMap's internal mutex01u,14apr99,dbs add check for T2OLE macro01t,13apr99,dbs add VxMutex into GuidMap class01s,13apr99,dbs replace std-lib with comUtilLib containers01r,13apr99,dbs fix Win32 compatibility in CoCreateGuid, fix indent style, add semi-colons to VXDCOM_DEBUG usage01q,08apr99,drm adding diagnostic output using VXDCOM_DEBUG ;01p,01apr99,dbs add CComBSTR methods01o,24mar99,drm removing show routine01n,19mar99,drm added CoCreateGuid()01m,09mar99,dbs remove use of std::string01l,03mar99,dbs fix GuidMap to use correct find() method01k,01mar99,dbs add GUID_NULL definition01j,19feb99,dbs add more wide-char support01i,11feb99,dbs ensure all memory is allocated through task-allocator01h,10feb99,dbs tidy up per-task data items01g,04feb99,dbs change wchar_t to OLECHAR01f,03feb99,dbs add wide/ascii converters01e,20jan99,dbs fix lib-init function name01d,20jan99,dbs rename and move to main VOBs01c,11jan99,dbs reduce STL usage01b,08jan99,dbs Alloc registry from heap so it always works at startup.01a,18dec98,dbs created*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "comLib.h"#include "private/comMisc.h"#include "private/comSysLib.h"#include "MemoryStream.h"#include "TaskAllocator.h"#include "TraceCall.h"#include "comCoreLib.h"/*DESCRIPTIONThis library provides a subset of the Win32 COM API calls, in order tosupport the implementation of COM in the VxWorks environment.*//* Include symbol for diab */extern "C" int include_vxcom_comLib (void) { return 0; }/* Well-known GUIDs that are defined by COM... */const GUID GUID_NULL = {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};/**************************************************************************** comLibInit - VxWorks COM library init function** This function initialises the VxWorks enhanced COM library.** RETURNS:* \is* \i OK* On success* \i ERROR* If some failure occurs* \ie*/int comLibInit () { return 0; }/**************************************************************************** CoCreateInstance - create an in-process instance of an object class** This function is used to create an instance of a local object (an* <in-proc server>) under VxDCOM. The only valid value of* 'dwClsContext' is CLSCTX_INPROC_SERVER.** RETURNS:* \is* \i S_OK* On success* \i E_NOINTERFACE* If the class does not support the requested interface* \i REG_E_CLASSNOTREG* If 'dwClsContext' is invalid or the class isn't registered in the registry.* \ie*/HRESULT CoCreateInstance ( REFCLSID rclsid, /* CLSID of the object */ IUnknown* pUnkOuter, /* pointer to aggregating object */ DWORD dwClsContext, /* context */ REFIID riid, /* IID of desired interface */ void** ppv /* variable to receive interface ptr */ ) { if (ppv == NULL) return E_POINTER; MULTI_QI mqi[] = { { &riid, 0, S_OK } }; // Create instance... HRESULT hr = comInstanceCreate (rclsid, pUnkOuter, dwClsContext, 0, 1, mqi); if (FAILED (hr)) return hr; // Get resulting interface pointer out... if (ppv) *ppv = mqi[0].pItf; return mqi[0].hr; }/**************************************************************************** CoInitialize - initialize COM runtime support to APARTMENTTHREADED** This function is provided for compatibility with the COM standard. Since* VxDCOM does not support the apartment-threading model this function always * returns E_INVALIDARG.** RETURNS: E_INVALIDARG under all conditions*/HRESULT CoInitialize ( void* pv /* Reserved. Must be NULL */ ) { return CoInitializeEx (pv, COINIT_APARTMENTTHREADED); }/**************************************************************************** CoInitializeEx - initialize COM runtime support** This function must be called with the 'dwCoInit' argument set to* COINIT_MULTITHREADED, as there is no apartment-threading model in* the VxWorks COM implementation.** RETURNS:* \is* \i S_OK* On success* \i E_INVALIDARG* If the caller asks for apartment threading.* \ie*/HRESULT CoInitializeEx ( void* pv, /* Reserved, must be NULL */ DWORD dwCoInit /* Must be CoINIT_MULTITHREADED */ ) { // Must be multi-threaded initialization... if (dwCoInit & COINIT_APARTMENTTHREADED) return E_INVALIDARG; return S_OK; }/**************************************************************************** CoUninitialize - un-initialize the COM runtime for this task.** Provided for compatibility with COM standard only.** RETURNS: None*/void CoUninitialize () { }/**************************************************************************** CoGetCurrentProcess - Returns a value that is unique to the current thread.** Returns a value that is unique to the current thread.** RETURNS: A unique DWORD identifying the current thread.*/DWORD CoGetCurrentProcess () { static DWORD s_nextTaskId = 0; if (comSysLocalGet () == 0) { comSysLocalSet (++s_nextTaskId); } return (DWORD) comSysLocalGet (); }/**************************************************************************** CLSIDFromAscii - Convert an ASCII string-format CLSID to a real CLSID structure.** This function converts an ASCII string-format CLSID to a real CLSID * structure. The ASCII string must be of the following format* {ABCDEFGH-IJKL-MNOP-QRST-UVWXYZ123456}.** RETURNS:* \is* \i S_OK* On success* \i CO_E_CLASSSTRING* If the string is of the wrong format* \i E_POINTER* If a NULL pointer is given for a parameter.* \ie*/HRESULT CLSIDFromAscii ( const char* s, /* The string to convert. */ LPCLSID pClsid /* The resultant CLSID structure. */ ) { TRACE_CALL; CLSID clsid; char cs [GUID_STRING_LEN]; int i,j; if (s == NULL) { return E_POINTER; } if (pClsid == NULL) { return E_POINTER; } if (strlen (s) != 38) { return CO_E_CLASSSTRING; } if ((s [0] == '{') && (s [37] == '}')) strncpy (cs, s + 1, 36); else return CO_E_CLASSSTRING; // read string-format of CLSID into structure char * pCheck = NULL; cs [8] = 0; clsid.Data1 = strtoul (cs, &pCheck, 16); if (*pCheck) { return CO_E_CLASSSTRING; } cs [13] = 0; clsid.Data2 = strtoul (cs + 9, &pCheck, 16); if (*pCheck) { return CO_E_CLASSSTRING; } cs [18] = 0; clsid.Data3 = strtoul (cs + 14, &pCheck, 16); if (*pCheck) { return CO_E_CLASSSTRING; } for (i = 34, j = 7; i >= 24; i -= 2) { cs [i + 2] = 0; clsid.Data4 [j--] = strtoul (cs + i, &pCheck, 16); if (*pCheck) { return CO_E_CLASSSTRING; } } for (i = 21; i >= 19; i -= 2) { cs [i + 2] = 0; clsid.Data4 [j--] = strtoul (cs + i, &pCheck, 16); if (*pCheck) { return CO_E_CLASSSTRING; } } *pClsid = clsid; return S_OK; }/**************************************************************************** CoGetMalloc - Gets a pointer to the task-allocator for this task.** Gets a pointer to the task-allocator for the current task, which is always* the system allocator. If there is already one then it simply adds a* reference to it, if not then it creates a new one.** RETURNS: * \is* \iE_INVALIDARG* dwMemContext != 1.* \iE_OUTOFMEMORY* This isn't enough free memory to create the task-allocator.* \iS_OK* Success* \ie*/HRESULT CoGetMalloc ( DWORD dwMemContext, /* Must be 1 */ IMalloc** ppMalloc /* Interface pointer to task-allocator */ ) { return comCoGetMalloc (dwMemContext, ppMalloc); }/**************************************************************************** CoTaskMemAlloc - Allocates a block of memory for use by VxDCOM functions** Allocates a block of memory for use by VxDCOM functions. The initial * contents of the memory block are undefined. CoGetMalloc need not be* called prior to calling this function.** RETURNS: pointer to block of memory or NULL on error.*/void* CoTaskMemAlloc ( ULONG cb /* Size in bytes. */ ) { IMalloc* pMalloc; void* pv; if (FAILED (CoGetMalloc (1, &pMalloc))) return 0; pv = pMalloc->Alloc (cb); pMalloc->Release (); return pv; }/**************************************************************************** CoTaskMemRealloc - Changes the size of a memory block.* * Changes the size of a memory block previously allocated using * CoTaskMemAlloc.* * RETURNS: Pointer to reallocated memory block or NULL on error.*/void* CoTaskMemRealloc ( LPVOID pv, /* Pointer to memory block. */ ULONG cb /* New size of memory block in bytes. */ ) { IMalloc* pMalloc; void* ptr; if (FAILED (CoGetMalloc (1, &pMalloc))) return 0; ptr = pMalloc->Realloc (pv, cb); pMalloc->Release (); return ptr; }/* resolve clashes in networking headers */#ifdef Free#undef Free#endif/**************************************************************************** CoTaskMemFree - Frees a block of task memory.* * This function frees a block of memory previously allocated using * CoTaskMemAlloc.** RETURNS: None.*/void CoTaskMemFree ( LPVOID pv /* Pointer to memory block */ ) { IMalloc* pMalloc; if (pv == 0) return; if (FAILED (CoGetMalloc (1, &pMalloc))) return; pMalloc->Free (pv);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -