]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/oleutils.cpp
fix memory leak (coverity checker CID 53)
[wxWidgets.git] / src / msw / ole / oleutils.cpp
CommitLineData
bbf1f0e5 1///////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/msw/ole/oleutils.cpp
bbf1f0e5
KB
3// Purpose: implementation of OLE helper functions
4// Author: Vadim Zeitlin
3f4a0c5b 5// Modified by:
bbf1f0e5
KB
6// Created: 19.02.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
bbf1f0e5
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// Declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
bbf1f0e5 20// For compilers that support precompilation, includes "wx.h".
bbf1f0e5
KB
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
24#pragma hdrstop
25#endif
26
3096bd2f 27#include "wx/log.h"
bbf1f0e5 28
21709999
JS
29#if wxUSE_OLE
30
ae090fdb
JS
31#ifndef __CYGWIN10__
32
4676948b
JS
33#include "wx/msw/private.h"
34
35#ifdef __WXWINCE__
36 #include <winreg.h>
37 #include <ole2.h>
38
39 #define GUID_DEFINED
40 #define UUID_DEFINED
41#endif
bbf1f0e5
KB
42
43// OLE
4676948b 44#ifndef __WXWINCE__
3096bd2f 45#include "wx/msw/ole/uuid.h"
4676948b
JS
46#endif
47
3096bd2f 48#include "wx/msw/ole/oleutils.h"
bbf1f0e5 49
3f4a0c5b
VZ
50#if defined(__VISUALC__) && (__VISUALC__ > 1000)
51 #include <docobj.h>
bbf1f0e5
KB
52#endif
53
54// ============================================================================
55// Implementation
56// ============================================================================
57
0a0e6a5b 58// return true if the iid is in the array
bbf1f0e5
KB
59bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount)
60{
61 for ( size_t i = 0; i < nCount; i++ ) {
62 if ( riid == *aIids[i] )
0a0e6a5b 63 return true;
bbf1f0e5
KB
64 }
65
0a0e6a5b 66 return false;
bbf1f0e5
KB
67}
68
e2898549 69WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str)
94113cc5
JS
70{
71/*
72 unsigned int len = strlen((const char*) str);
73 unsigned short* s = new unsigned short[len*2+2];
74 unsigned int i;
75 memset(s, 0, len*2+2);
76 for (i=0; i < len; i++)
77 s[i*2] = str[i];
78*/
79 wxBasicString bstr(str.mb_str());
80 return bstr.Get();
81}
82
e2898549 83WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
94113cc5
JS
84{
85#if wxUSE_UNICODE
86 wxString str(bStr);
87#else
88 int len = SysStringLen(bStr) + 1;
89 char *buf = new char[len];
90 (void)wcstombs( buf, bStr, len);
91 wxString str(buf);
92 delete[] buf;
93#endif
94 return str;
95}
96
97// ----------------------------------------------------------------------------
98// wxBasicString
99// ----------------------------------------------------------------------------
100
101// ctor takes an ANSI string and transforms it to Unicode
102wxBasicString::wxBasicString(const char *sz)
103{
104 Init(sz);
105}
106
107// ctor takes an ANSI or Unicode string and transforms it to Unicode
108wxBasicString::wxBasicString(const wxString& str)
109{
110#if wxUSE_UNICODE
521bf4ff
WS
111 m_wzBuf = new OLECHAR[str.length() + 1];
112 memcpy(m_wzBuf, str.c_str(), str.length()*2);
113 m_wzBuf[str.length()] = L'\0';
94113cc5
JS
114#else
115 Init(str.c_str());
116#endif
117}
118
119// Takes an ANSI string and transforms it to Unicode
120void wxBasicString::Init(const char *sz)
121{
122 // get the size of required buffer
123 UINT lenAnsi = strlen(sz);
124#ifdef __MWERKS__
125 UINT lenWide = lenAnsi * 2 ;
126#else
127 UINT lenWide = mbstowcs(NULL, sz, lenAnsi);
128#endif
129
130 if ( lenWide > 0 ) {
131 m_wzBuf = new OLECHAR[lenWide + 1];
132 mbstowcs(m_wzBuf, sz, lenAnsi);
133 m_wzBuf[lenWide] = L'\0';
134 }
135 else {
136 m_wzBuf = NULL;
137 }
138}
139
140// dtor frees memory
141wxBasicString::~wxBasicString()
142{
143 delete [] m_wzBuf;
144}
145
8c18da2e 146#if wxUSE_DATAOBJ
c740f496 147
bbf1f0e5
KB
148// ----------------------------------------------------------------------------
149// Debug support
150// ----------------------------------------------------------------------------
151
ba14d986 152#if defined(__WXDEBUG__) && ( ( defined(__VISUALC__) && (__VISUALC__ > 1000) ) || defined(__MWERKS__) )
f6bcfd97 153static wxString GetIidName(REFIID riid)
bbf1f0e5
KB
154{
155 // an association between symbolic name and numeric value of an IID
156 struct KNOWN_IID {
157 const IID *pIid;
f6bcfd97 158 const wxChar *szName;
bbf1f0e5
KB
159 };
160
161 // construct the table containing all known interfaces
f6bcfd97 162 #define ADD_KNOWN_IID(name) { &IID_I##name, _T(#name) }
bbf1f0e5
KB
163
164 static const KNOWN_IID aKnownIids[] = {
165 ADD_KNOWN_IID(AdviseSink),
166 ADD_KNOWN_IID(AdviseSink2),
167 ADD_KNOWN_IID(BindCtx),
168 ADD_KNOWN_IID(ClassFactory),
ba14d986 169#if ( !defined( __VISUALC__) || (__VISUALC__!=1010) ) && !defined(__MWERKS__)
bbf1f0e5
KB
170 ADD_KNOWN_IID(ContinueCallback),
171 ADD_KNOWN_IID(EnumOleDocumentViews),
172 ADD_KNOWN_IID(OleCommandTarget),
173 ADD_KNOWN_IID(OleDocument),
174 ADD_KNOWN_IID(OleDocumentSite),
175 ADD_KNOWN_IID(OleDocumentView),
176 ADD_KNOWN_IID(Print),
27a9bd48 177#endif
bbf1f0e5
KB
178 ADD_KNOWN_IID(DataAdviseHolder),
179 ADD_KNOWN_IID(DataObject),
180 ADD_KNOWN_IID(Debug),
181 ADD_KNOWN_IID(DebugStream),
182 ADD_KNOWN_IID(DfReserved1),
183 ADD_KNOWN_IID(DfReserved2),
184 ADD_KNOWN_IID(DfReserved3),
185 ADD_KNOWN_IID(Dispatch),
186 ADD_KNOWN_IID(DropSource),
187 ADD_KNOWN_IID(DropTarget),
188 ADD_KNOWN_IID(EnumCallback),
189 ADD_KNOWN_IID(EnumFORMATETC),
190 ADD_KNOWN_IID(EnumGeneric),
191 ADD_KNOWN_IID(EnumHolder),
192 ADD_KNOWN_IID(EnumMoniker),
193 ADD_KNOWN_IID(EnumOLEVERB),
194 ADD_KNOWN_IID(EnumSTATDATA),
195 ADD_KNOWN_IID(EnumSTATSTG),
196 ADD_KNOWN_IID(EnumString),
197 ADD_KNOWN_IID(EnumUnknown),
198 ADD_KNOWN_IID(EnumVARIANT),
199 ADD_KNOWN_IID(ExternalConnection),
200 ADD_KNOWN_IID(InternalMoniker),
201 ADD_KNOWN_IID(LockBytes),
202 ADD_KNOWN_IID(Malloc),
203 ADD_KNOWN_IID(Marshal),
204 ADD_KNOWN_IID(MessageFilter),
205 ADD_KNOWN_IID(Moniker),
206 ADD_KNOWN_IID(OleAdviseHolder),
207 ADD_KNOWN_IID(OleCache),
208 ADD_KNOWN_IID(OleCache2),
209 ADD_KNOWN_IID(OleCacheControl),
210 ADD_KNOWN_IID(OleClientSite),
211 ADD_KNOWN_IID(OleContainer),
212 ADD_KNOWN_IID(OleInPlaceActiveObject),
213 ADD_KNOWN_IID(OleInPlaceFrame),
214 ADD_KNOWN_IID(OleInPlaceObject),
215 ADD_KNOWN_IID(OleInPlaceSite),
216 ADD_KNOWN_IID(OleInPlaceUIWindow),
217 ADD_KNOWN_IID(OleItemContainer),
218 ADD_KNOWN_IID(OleLink),
219 ADD_KNOWN_IID(OleManager),
220 ADD_KNOWN_IID(OleObject),
221 ADD_KNOWN_IID(OlePresObj),
222 ADD_KNOWN_IID(OleWindow),
223 ADD_KNOWN_IID(PSFactory),
224 ADD_KNOWN_IID(ParseDisplayName),
225 ADD_KNOWN_IID(Persist),
226 ADD_KNOWN_IID(PersistFile),
227 ADD_KNOWN_IID(PersistStorage),
228 ADD_KNOWN_IID(PersistStream),
229 ADD_KNOWN_IID(ProxyManager),
230 ADD_KNOWN_IID(RootStorage),
231 ADD_KNOWN_IID(RpcChannel),
232 ADD_KNOWN_IID(RpcProxy),
233 ADD_KNOWN_IID(RpcStub),
234 ADD_KNOWN_IID(RunnableObject),
235 ADD_KNOWN_IID(RunningObjectTable),
236 ADD_KNOWN_IID(StdMarshalInfo),
237 ADD_KNOWN_IID(Storage),
238 ADD_KNOWN_IID(Stream),
239 ADD_KNOWN_IID(StubManager),
240 ADD_KNOWN_IID(Unknown),
241 ADD_KNOWN_IID(ViewObject),
242 ADD_KNOWN_IID(ViewObject2),
243 };
244
245 // don't clobber preprocessor name space
246 #undef ADD_KNOWN_IID
247
248 // try to find the interface in the table
c86f1403 249 for ( size_t ui = 0; ui < WXSIZEOF(aKnownIids); ui++ ) {
bbf1f0e5
KB
250 if ( riid == *aKnownIids[ui].pIid ) {
251 return aKnownIids[ui].szName;
252 }
253 }
254
4676948b 255#ifndef __WXWINCE__
bbf1f0e5 256 // unknown IID, just transform to string
f6bcfd97
BP
257 Uuid uuid(riid);
258 return wxString((const wxChar *)uuid);
4676948b
JS
259#else
260 return wxEmptyString;
261#endif
bbf1f0e5
KB
262}
263
f6bcfd97 264void wxLogQueryInterface(const wxChar *szInterface, REFIID riid)
bbf1f0e5 265{
ba14d986 266 wxLogTrace(wxTRACE_OleCalls, wxT("%s::QueryInterface (iid = %s)"),
f6bcfd97 267 szInterface, GetIidName(riid).c_str());
bbf1f0e5
KB
268}
269
f6bcfd97 270void wxLogAddRef(const wxChar *szInterface, ULONG cRef)
bbf1f0e5 271{
ba14d986 272 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::AddRef: m_cRef = %d"), szInterface, cRef + 1);
bbf1f0e5
KB
273}
274
f6bcfd97 275void wxLogRelease(const wxChar *szInterface, ULONG cRef)
bbf1f0e5 276{
ba14d986 277 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::Release: m_cRef = %d"), szInterface, cRef - 1);
bbf1f0e5
KB
278}
279
aeab10d0
JS
280#elif defined(__WXDEBUG__) && defined(__VISUALC__) && (__VISUALC__ <= 1000)
281
282// For VC++ 4
283void wxLogQueryInterface(const char *szInterface, REFIID riid)
284{
285 wxLogTrace("%s::QueryInterface", szInterface);
286}
287
288void wxLogAddRef(const char *szInterface, ULONG cRef)
289{
290 wxLogTrace("After %s::AddRef: m_cRef = %d", szInterface, cRef + 1);
291}
292
293void wxLogRelease(const char *szInterface, ULONG cRef)
294{
295 wxLogTrace("After %s::Release: m_cRef = %d", szInterface, cRef - 1);
296}
297
81099467 298#endif // __WXDEBUG__
bbf1f0e5
KB
299
300#endif
47d67540 301 // wxUSE_DRAG_AND_DROP
ae090fdb
JS
302
303#endif
304 // __CYGWIN10__
305
21709999
JS
306#endif
307 // wxUSE_OLE