OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
autocli.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectComponents
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4/// \file
5/// OLE Automation Client Implementation
6//----------------------------------------------------------------------------
7#include <ocf/pch.h>
8#include <ocf/autodefs.h>
9
10
11namespace ocf {
12
13using namespace owl;
14
15
17
18//
19//
21{
22 PRECONDITION(progid);
23
24 GUID guid;
25 TXOle::Check(CLSIDFromProgID(OleStr(progid), &guid), progid);
26 Bind(guid);
27}
28
29//
30//
32{
33 IUnknown* unk;
34 HRESULT stat = ::CoCreateInstance(guid,0,CLSCTX_SERVER,IID_IUnknown,(void **)&unk);
35 if (stat != HR_NOERROR) {
36 _TCHAR guidBuf[60];
39 TXOle::Check(stat, guidBuf);
40 }
41 Bind(unk);
42}
43
44//
45//
46void TAutoProxy::Bind(IUnknown* unk)
47{
48 if (!unk)
49 return;
51 HRESULT stat = unk->QueryInterface(IID_IDispatch, (void * *)&dsp);
52 unk->Release();
53 TXOle::Check(stat, _T("IUnknown"));
54 Bind(dsp);
55}
56
57//
58//
60{
61 Unbind();
62 That = dsp;
63}
64
65//
66//
67void TAutoProxy::Bind(IUnknown& unk)
68{
70 TXOle::Check(unk.QueryInterface(IID_IDispatch, (void * *)&dsp), _T("IUnknown"));
71 Bind(dsp);
72}
73
74//
75//
77{
78 Unbind();
79 obj.AddRef();
80 That = &obj;
81}
82
83//
84//
86{
87 if (val.GetDataType() == atVoid)
88 That = 0;
89 else
90 // NOTE: The following call throws exception in case of failure
91 //
93}
94
95//
96//
102
103//
104//
106{
107 CLSID clsid;
108 TXOle::Check(CLSIDFromProgID(OleStr(progid), &clsid));
109
110 LPUNKNOWN punk = 0;
111 LPDISPATCH pdisp = 0;
112 HRESULT hr = GetActiveObject(clsid, 0, &punk);
113 if (SUCCEEDED(hr)) {
114 punk->QueryInterface(IID_IDispatch,
115 (void**)&pdisp);
116 punk->Release();
117 }
118 return pdisp;
119}
120
121//
122//
124{
125 LPCTSTR failure = _T("(null)");
126 if (!name)
127 name = failure; // force controlled failure
128 long id;
129
130 MustBeBound();
131#if !defined(_UNICODE)
133 wchar_t* wname = tsname;
134 TXOle::Check(That->GetIDsOfNames(IID_NULL, &wname, 1, Lang, &id), name);
135#else
136 TXOle::Check(That->GetIDsOfNames(IID_NULL, (LPOLESTR*)&name, 1, Lang, &id), name);
137#endif
138 return id;
139}
140
141//
142//
143//
144void TAutoProxy::Lookup(LPCTSTR names, long* ids, unsigned count)
145{
146 MustBeBound();
147
148#if !defined(_UNICODE)
149 const char * pc = names;
150 int i;
151 for (i = 0; i < (int)count; i++)
152 pc += strlen(pc) + 1;
153
155 wchar_t** args = new wchar_t *[count];
156 wchar_t* pw = wnames;
157 for (i = 0; i < (int)count; i++) {
158 args[i] = pw;
159 pw += lstrlenW(pw) + 1;
160 }
161 HRESULT stat = That->GetIDsOfNames(IID_NULL, args, count, Lang, ids);
162 delete wnames;
163#else
164 _TCHAR ** args = new _TCHAR *[count];
165 const _TCHAR * pc = names;
166 for (int i = 0; i < (int)count; i++) {
167 args[i] = (_TCHAR *)pc; // for non-unicode, names separated by null bytes
168 pc += _tcslen(pc) + 1;
169 }
170 HRESULT stat = That->GetIDsOfNames(IID_NULL, args, count, Lang, ids);
171#endif
172
173 delete[] args;
174 if (stat) {
175 int bad = 0;
176 if ((stat = HR_DISP_UNKNOWNNAME) != HR_NOERROR)
177 while (bad < (int)count && ids[bad] != DISPID_UNKNOWN) {
178 bad++;
179 names += (_tcslen(names) + 1);
180 }
181 TXOle::Check(stat, names);
182 }
183}
184
185//
186//
187//
190 long* ids, unsigned named)
191{
192 // Check that we're bound
193 //
194 MustBeBound();
195
196 VARIANT* retval = 0;
197 if (!(attr & (acPropSet | acVoidRet))) {
198
199 // NOTE: The first TAutoVal/VARIANT of TAutoProxyArgs (really TAutoArgs)
200 // is for the return value...
201 retval = args;
202
203 // !BB ::VariantInit(retval);
204 // !BB Don't need this anymore now that TAutoVal had ctrs/dtrs
205
206 }
207
208 DISPID funcId = ids[0];
210 params.cArgs = args;
211 params.cNamedArgs = named;
212 params.rgvarg = args;
213 params.rgdispidNamedArgs = ids;
214
215 if (attr & acPropSet) {
216 ids[0] = DISPID_PROPERTYPUT;
217 params.cNamedArgs++;
218 params.cArgs++;
219 }
220 else {
221 params.rgdispidNamedArgs++;
222 params.rgvarg++;
223 }
224
225 unsigned int errarg = 0;
226 EXCEPINFO errinfo = {0};
227 HRESULT stat = That->Invoke(funcId, IID_NULL, Lang,
230
231 // Restore function id incase PropSet
232 //
233 ids[0] = funcId;
234
235 // Check for failure
236 //
237 if (stat != HR_NOERROR) {
238 TAPointer<_TCHAR> errMsg(new _TCHAR[errinfo.bstrDescription ?\
239 (wcslen(errinfo.bstrDescription) + 100)\
240 : 100]);
241 // Is there additional informatiol in the EXCEPINFO structure
242 //
243 if (stat == ResultFromScode(DISP_E_EXCEPTION)) {
244#if defined(_UNICODE)
245 wsprintf(errMsg, _T("Invoke ID= %ld/%lX (%s) %s=%X"), funcId, funcId,\
246 errinfo.bstrDescription,\
247 (errinfo.wCode ? _T("wCode") : _T("scode")),\
248 (errinfo.wCode ? (int)errinfo.wCode : (int)errinfo.scode));
249#else
250 TString str(errinfo.bstrDescription);
251 const _TCHAR *errDesc = errinfo.bstrDescription ? (const _TCHAR*)str : _T("");
252 wsprintf(errMsg, _T("Invoke ID= %ld/%lX (%s) %s=%X"), funcId, funcId,\
253 errDesc,\
254 (errinfo.wCode ? _T("wCode") : _T("scode")),\
255 (errinfo.wCode ? (int)errinfo.wCode : (int)errinfo.scode));
256#endif
257 }
258 else if (stat == ResultFromScode(DISP_E_TYPEMISMATCH) ||
260 wsprintf(errMsg, _T("Invoke ID= %ld/%lX, arg Index:%d"), funcId, funcId,
261 errarg);
262 }
263 else
264 wsprintf(errMsg, _T("Invoke Id= %ld/%lX"), funcId, funcId);
265
266 TXOle::Check(stat, errMsg);
267 }
268 return args;
269}
270
271//
272//
273//
281
282//
283//
284//
286{
287 Current.Copy(copy.Current);
288 Unbind();
289 Iterator = copy.Iterator;
290 if (Iterator)
292}
293
294//
295//
296//
298{
299 Unbind();
300 IUnknown& unk = val;
301 TXOle::Check(unk.QueryInterface(IID_IEnumVARIANT, (void * *) &Iterator),
302 _T("_NewEnum"));
303}
304
305//
306//
307//
309{
310 Clear();
311 if (!Iterator)
312 TXOle::Check(HR_NOINTERFACE, _T("_NewEnum"));
313 else if (HRIsOK(Iterator->Next(1,(VARIANT *)&Current,0)))
314 return true;
315 return false;
316}
317
318} // OCF namespace
319
320//==============================================================================
321
OLE Automation Class Definitions.
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
void operator=(const TAutoEnumeratorBase &copy)
Definition autocli.cpp:285
IEnumVARIANT * Iterator
Definition autodefs.h:1359
void Bind(TAutoVal &val)
Definition autocli.cpp:297
void Bind(IUnknown *obj)
Definition autocli.cpp:46
static IDispatch * GetObject(LPCTSTR progid)
Definition autocli.cpp:105
void Unbind(bool release=true)
Definition autodefs.h:1415
long Lookup(const long id)
Definition autodefs.h:1315
owl::TLangId Lang
Definition autodefs.h:1327
IDispatch * That
Definition autodefs.h:1326
TAutoVal & Invoke(owl::uint16 attr, TAutoProxyArgs &args, long *ids, unsigned named=0)
Definition autocli.cpp:189
void MustBeBound()
Definition autocli.cpp:97
Automation data element (same data as OLE/BASIC VARIANT)
Definition autodefs.h:526
void Copy(const TAutoVal &copy)
Definition autodefs.h:1722
GUID/IID/CLSID management.
Definition oleutil.h:171
static void Raise(TError err)
Definition autoval.cpp:831
static void Check(HRESULT stat, LPCTSTR msg)
Definition except.cpp:47
Reference to reference counted string object TUString Lightweight reference object consisting of a po...
Definition string.h:67
static wchar_t * ConvertAtoW(const char *src, size_t len=(size_t) -1)
Definition ustring.cpp:57
#define _tcscpy
Definition cygwin.h:79
size_t __stdcall wcslen(const wchar_t *str)
#define _tcslen
Definition cygwin.h:74
#define _T(x)
Definition cygwin.h:51
Include for OC, gets common headers when precompiled headers are enabled.
Object Component Framework (COM encapsulation)
Definition appdesc.h:22
bool HRIsOK(HRESULT hr)
Definition defs.h:132
@ acVoidRet
pass NULL for return variant, not OLE2 flag
Definition autodefs.h:1232
@ acPropGet
returns property value, same as OLE2
Definition autodefs.h:1230
@ acPropSet
set property value, same as OLE2
Definition autodefs.h:1231
@ acMethod
method call, same as OLE2
Definition autodefs.h:1229
@ atVoid
Definition autodefs.h:323
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned short uint16
Definition number.h:33
#define HR_NOINTERFACE
Definition defs.h:79
#define HR_DISP_UNKNOWNNAME
Definition defs.h:98
#define HR_NOERROR
Definition defs.h:73
#define OleStr(s)
Definition string.h:128