OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
oleutil.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectComponents
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// OLE Utility class implementations
7//----------------------------------------------------------------------------
8#include <ocf/pch.h>
9
10#include <ocf/defs.h>
11#include <ocf/oleutil.h>
12#include <ocf/ocbocole.h>
13#include <owl/string.h>
14#include <owl/private/memory.h>
15
16namespace ocf {
17
18using namespace owl;
19
21
22
23//
24//
25//
26void*
27TOleAllocator::Alloc(unsigned long size)
28{
29 void* blk = Mem->Alloc(size);
30 OLECALL(blk ? 0 : HR_OUTOFMEMORY, _T("IMalloc::Alloc"));
31 return blk;
32}
33
34//
35//
36//
37void
39{
40 Mem->Free(pv);
41}
42
43//----------------------------------------------------------------------------
44// TClassId - GUID/IID/CLSID management implementation
45//
46
47//
48//
49//
51 :Text(0)
52{
54 OLECALL(::CLSIDFromString(OleStr(idtxt), &Guid), _T("Invalid GUID string"));
55}
56
57//
58//
59//
61:
62 Text(0), Guid(guid)
63{
64 Guid.Data1 += (long)offset;
65}
66
67//
68//
69//
71:
72 Text(0)
73{
74 OLECALL(::CoCreateGuid(&Guid), _T("CoCreateGuid"));
75}
76
77//
78//
79//
81{
82 delete[] const_cast<LPTSTR>(Text);
83}
84
85//
86//
87//
88TClassId::operator LPCTSTR()
89{
90 if (!Text) {
91 Text = new TCHAR[38+1];
92 wsprintf(const_cast<LPTSTR>(Text),
93 _T("{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
94 Guid.Data1, Guid.Data2, Guid.Data3,
95 Guid.Data4[0], Guid.Data4[1], Guid.Data4[2], Guid.Data4[3],
96 Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]);
97 }
98 return Text;
99}
100
101//
102//
103//
105:
106 TClassId(guid), MaxAlloc(maxAlloc), Offset(0)
107{
108}
109
110//
111//
112//
114:
115 TClassId(idtxt), MaxAlloc(maxAlloc), Offset(0)
116{
117}
118
119//
120//
121//
123:
124 TClassId(e), MaxAlloc(maxAlloc), Offset(0)
125{
126}
127
128//
129//
130//
132:
133 TClassId(), MaxAlloc(maxAlloc), Offset(0)
134{
135 LPCTSTR str = regInfo["clsid"];
136 if (!str)
137 OLECALL(HR_CLASS_CLASSNOTAVAILABLE, _T("Missing REGDATA clsid"));
138 OLECALL(::CLSIDFromString(OleStr(str), &Guid), _T("Invalid GUID string"));
139}
140
141//
142//
143//
146{
147 if(offset > Offset)
148 OLECALL(HR_CLASS_CLASSNOTAVAILABLE, _T("Unassigned GUID"));
149 return TClassId(Guid, offset);
150}
151
152//
153//
154//
155int
157{
158 ++Offset;
159 if(Offset >= MaxAlloc)
160 OLECALL(HR_CLASS_CLASSNOTAVAILABLE, _T("GUID allocation exhausted"));
161 return Offset;
162}
163
164//
165//
166//
167int
169{
170 unsigned long offset = guid.Data1 - Guid.Data1;
171 if ( offset >= (unsigned long)Offset &&
172 ((long*)&guid)[1] == ((long*)&Guid)[1] &&
173 ((long*)&guid)[2] == ((long*)&Guid)[2] &&
174 ((long*)&guid)[3] == ((long*)&Guid)[3] )
175 return int(offset);
176 else
177 return -1;
178}
179
180//----------------------------------------------------------------------------
181// GUID compare routines use by implementations of IUnknown::QueryInterface
182// the low-order long word is verifed before calling these routines
183// success is indicated by virtue of assigment of the returned interface
184//
185
186//
187//
188//
189void
190CmpGuid12(IUnknown* obj, REFIID req, REFIID ref, void ** pif)
191{
192 if ( ((long *)&req)[1] == ((long *)&ref)[1] &&
193 ((long *)&req)[2] == ((long *)&ref)[2] &&
194 ((long *)&req)[3] == ((long *)&ref)[3] ) {
195 *pif = obj;
196 obj->AddRef();
197 }
198}
199
200//
201//
202//
203void
204CmpGuidOle(IUnknown* obj, REFIID req, void ** pif)
205{
206 if ( ((long*)&req)[1] == 0x00000000L &&
207 ((long*)&req)[2] == 0x000000C0L &&
208 ((long*)&req)[3] == 0x46000000L ) {
209 *pif = obj;
210 obj->AddRef();
211 }
212}
213
214//----------------------------------------------------------------------------
215// TUnknown - standard implementation of IUnknown
216//
217
218//
219// Handy container to help watch COM object lifetime when debugging
220//
221#if (__DEBUG >= 2)
224#endif
225
226//
227//
228//
230 :Outer(&I)
231{
232#if (__DEBUG >= 2)
233 if (!UnknownBag)
235 UnknownBag->Add(this);
236#endif
237}
238
239//
240//
241//
242IUnknown&
244{
245 if (I.Inner)
246 return I.Inner->Aggregate(inner);
247 ((IUnknown&)inner).AddRef();
248 I.Inner = &inner;
249 return *Outer;
250}
251
252//
253//
254//
256{
257#if (__DEBUG >= 2)
258 UnknownBag->DetachItem(this);
259 if (UnknownBag->GetItemsInContainer()==0) {
260 delete UnknownBag;
261 UnknownBag=nullptr;
262 }
263#endif
264 TRACEX(OcfRefCount, 1, "~TUnknown() @" << (void*)this
265 << _T('(') << _OBJ_FULLTYPENAME(this) << _T(')')
266 << ", RefCnt:" << I.RefCnt);
267 WARNX(OcfRefCount, I.RefCnt != 0, 0, "~TUnknown() @ "
268 << (void*)this
269 << _T('(') << _OBJ_FULLTYPENAME(this) << _T(')')
270 << ", RefCnt:" << I.RefCnt);
271
272 // deleted by TUnknownI member
273}
274
275//
276/// QueryObject used when TUnknown is in constructor/destructor
277//
279TUnknown::QueryObject(const GUID & /*iid*/, void * * /*pif*/)
280{
282}
283
284//
285//
286//
288TUnknown::TUnknownI::QueryInterface(const GUID & iid, void * * pif)
289{
290 *pif = 0;
291 if (iid.Data1 == 0) { // IID_IUnknown.Data1
292 CmpGuidOle(this, iid, pif);
293 if (*pif)
294 return NOERROR;
295 }
296 if (Host().QueryObject(iid, pif) == NOERROR)
297 return NOERROR;
298 if (Inner)
299 return Inner->ThisUnknown().QueryInterface(iid, pif);
300 else
302}
303
304//
305//
306//
307unsigned long _IFUNC
308TUnknown::TUnknownI::AddRef()
309{
311 "AddRef on " << _OBJ_FULLTYPENAME(&Host()) << ' ' << (void*)&Host() << " to " << (RefCnt+1));
312 return ++RefCnt;
313}
314
315//
316//
317//
318unsigned long _IFUNC
319TUnknown::TUnknownI::Release()
320{
322 "Release on " << _OBJ_FULLTYPENAME(&Host()) << ' ' << (void*)&Host() << " to " << (RefCnt-1));
323 if (--RefCnt != 0)
324 return RefCnt;
325 if (Inner) {
326 Inner->ThisUnknown().Release();
327 Inner = 0;
328 }
329 delete &Host();
330 return 0;
331}
332
333//
334//
335//
336unsigned long
338{
339 if (Outer == &I)
340 return I.RefCnt;
341 Outer->AddRef();
342 return Outer->Release();
343}
344
345//
346//
347//
348TUnknown::TUnknownI::~TUnknownI()
349{
350 // In case destructor called before RefCnt goes to 0
351 //
352 if (RefCnt)
353 ::CoDisconnectObject(this,0);// OLE will try to remove external references
354 Inner = 0;
355}
356
357//----------------------------------------------------------------------------
358// Pixel <-> HiMetric conversion routines
359//
360static int logPixX = 0;
361static int logPixY = 0;
362
363//
364//
365static void
366GetScreenMetrics(int& logPixX, int& logPixY)
367{
368 HDC hDCScreen = GetDC(0);
369 logPixX = GetDeviceCaps(hDCScreen, LOGPIXELSX);
370 logPixY = GetDeviceCaps(hDCScreen, LOGPIXELSY);
372}
373
374//
375//
376//
377void
379{
380 if (!logPixX || !logPixY)
381 GetScreenMetrics(logPixX, logPixY);
382
383 sizePix.cx = MAP_LOGHIM_TO_PIX(sizeHM.cx, logPixX);
384 sizePix.cy = MAP_LOGHIM_TO_PIX(sizeHM.cy, logPixY);
385}
386
387//
388//
389//
390void
392{
393 if (!logPixX || !logPixY)
394 GetScreenMetrics(logPixX, logPixY);
395
396 sizeHM.cx = MAP_PIX_TO_LOGHIM(sizePix.cx, logPixX);
397 sizeHM.cy = MAP_PIX_TO_LOGHIM(sizePix.cy, logPixY);
398}
399
400
401} // OCF namespace
402
403//==============================================================================
404
#define WARNX(group, condition, level, message)
Definition checks.h:277
#define PRECONDITION(condition)
Definition checks.h:227
#define TRACEX(group, level, message)
Definition checks.h:263
#define DIAG_DEFINE_GROUP_INIT(f, g, e, l)
Definition checks.h:429
int GetOffset(const GUID &guid)
Definition oleutil.cpp:168
TClassId operator[](int offset)
Definition oleutil.cpp:145
TBaseClassId(owl::TRegList &regInfo, int maxAlloc=256)
Definition oleutil.cpp:131
GUID/IID/CLSID management.
Definition oleutil.h:171
LPCTSTR Text
Definition oleutil.h:189
TClassId()
Default constructor - Derived class must set GUID.
Definition oleutil.h:209
void * Alloc(unsigned long size)
Definition oleutil.cpp:27
IMalloc * Mem
Definition oleutil.h:113
void Free(void *pv)
Definition oleutil.cpp:38
Standard implementation of a controlling IUnknown for an object, to be inherited with other COM inter...
Definition oleutil.h:264
IUnknown * Outer
Definition oleutil.h:279
unsigned long GetRefCount()
Definition oleutil.cpp:337
virtual HRESULT QueryObject(const GUID &iid, void **pif)
QueryObject used when TUnknown is in constructor/destructor.
Definition oleutil.cpp:279
virtual ~TUnknown()
Definition oleutil.cpp:255
IUnknown & ThisUnknown()
Definition oleutil.h:278
IUnknown & Aggregate(TUnknown &inner)
Definition oleutil.cpp:243
A registration parameter table, composed of a list of TRegItems.
Definition registry.h:531
#define _T(x)
Definition cygwin.h:51
#define _OBJ_FULLTYPENAME(obj)
Definition objstrm.h:78
Include for OC, gets common headers when precompiled headers are enabled.
Reliable platform independent header for common memory and string functions.
Object Component Framework (COM encapsulation)
Definition appdesc.h:22
void OcHiMetricToPixel(const SIZEL &sizeHM, SIZEL &sizePix)
Definition oleutil.cpp:378
void CmpGuidOle(IUnknown *This, REFIID req, void **pif)
Definition oleutil.cpp:204
const GUID & iid
Definition appdesc.h:328
void CmpGuid12(IUnknown *This, REFIID req, REFIID ref, void **pif)
Definition oleutil.cpp:190
void OcPixelToHiMetric(const SIZEL &sizePix, SIZEL &sizeHM)
Definition oleutil.cpp:391
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
ObjectComponents BOcOle engine linking & embedding interfaces.
#define MAP_LOGHIM_TO_PIX(hiMetric, pixelsPerInch)
Definition ocbocole.h:177
#define MAP_PIX_TO_LOGHIM(pixel, pixelsPerInch)
Definition ocbocole.h:171
ObjectComponents fundamental definitions.
#define HR_CLASS_CLASSNOTAVAILABLE
Definition defs.h:86
#define HR_OUTOFMEMORY
Definition defs.h:77
#define OLECALL(func, msg)
Definition except.h:78
Low level OLE Utility class definitions.
#define _IFUNC
Definition oleutil.h:28
#define OWL_INI
Definition defs.h:170
Definition of class TString, a flexible universal string envelope class.
#define OleStr(s)
Definition string.h:128