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