OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
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
11
namespace
ocf
{
12
13
using namespace
owl;
14
15
16
DIAG_DECLARE_GROUP
(OcfDll);
17
18
//
19
//
20
void
TAutoProxy::Bind
(LPCTSTR progid)
21
{
22
PRECONDITION
(progid);
23
24
GUID guid;
25
TXOle::Check
(CLSIDFromProgID(
OleStr
(progid), &guid), progid);
26
Bind
(guid);
27
}
28
29
//
30
//
31
void
TAutoProxy::Bind
(
const
GUID& guid)
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];
37
TClassId
copy(guid);
38
_tcscpy
(guidBuf, copy);
39
TXOle::Check
(stat, guidBuf);
40
}
41
Bind
(unk);
42
}
43
44
//
45
//
46
void
TAutoProxy::Bind
(IUnknown* unk)
47
{
48
if
(!unk)
49
return
;
50
IDispatch* dsp;
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
//
59
void
TAutoProxy::Bind
(IDispatch* dsp)
60
{
61
Unbind
();
62
That
= dsp;
63
}
64
65
//
66
//
67
void
TAutoProxy::Bind
(IUnknown& unk)
68
{
69
IDispatch* dsp;
70
TXOle::Check
(unk.QueryInterface(IID_IDispatch, (
void
* *)&dsp),
_T
(
"IUnknown"
));
71
Bind
(dsp);
72
}
73
74
//
75
//
76
void
TAutoProxy::Bind
(IDispatch& obj)
77
{
78
Unbind
();
79
obj.AddRef();
80
That
= &obj;
81
}
82
83
//
84
//
85
void
TAutoProxy::Bind
(
TAutoVal
& val)
86
{
87
if
(val.
GetDataType
() ==
atVoid
)
88
That
= 0;
89
else
90
// NOTE: The following call throws exception in case of failure
91
//
92
Bind
((IDispatch&)val);
93
}
94
95
//
96
//
97
void
TAutoProxy::MustBeBound
()
98
{
99
if
(!
That
)
100
TXAuto::Raise
(
TXAuto::xNotIDispatch
);
101
}
102
103
//
104
//
105
IDispatch*
TAutoProxy::GetObject
(LPCTSTR progid)
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
//
123
long
TAutoProxy::Lookup
(LPCTSTR name)
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)
132
TString
tsname(name);
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
//
144
void
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
154
wchar_t
* wnames =
TUString::ConvertAtoW
(names, pc-names-1);
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
//
188
TAutoVal
&
189
TAutoProxy::Invoke
(
uint16
attr,
TAutoProxyArgs
& args,
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];
209
DISPPARAMS params;
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
,
228
uint16
(attr & (
acMethod
|
acPropSet
|
acPropGet
)),
229
¶ms, retval, &errinfo, &errarg);
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) ||
259
stat == ResultFromScode(DISP_E_PARAMNOTFOUND)){
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
//
274
TAutoEnumeratorBase::TAutoEnumeratorBase
(
const
TAutoEnumeratorBase
& copy)
275
{
276
Current
.
Copy
(copy.
Current
);
277
Iterator
= copy.
Iterator
;
278
if
(
Iterator
)
279
TXOle::Check
(
Iterator
->Clone(&
Iterator
));
280
}
281
282
//
283
//
284
//
285
void
TAutoEnumeratorBase::operator =
(
const
TAutoEnumeratorBase
& copy)
286
{
287
Current
.
Copy
(copy.
Current
);
288
Unbind
();
289
Iterator
= copy.
Iterator
;
290
if
(
Iterator
)
291
TXOle::Check
(
Iterator
->Clone(&
Iterator
));
292
}
293
294
//
295
//
296
//
297
void
TAutoEnumeratorBase::Bind
(
TAutoVal
& val)
298
{
299
Unbind
();
300
IUnknown& unk = val;
301
TXOle::Check
(unk.QueryInterface(IID_IEnumVARIANT, (
void
* *) &
Iterator
),
302
_T
(
"_NewEnum"
));
303
}
304
305
//
306
//
307
//
308
bool
TAutoEnumeratorBase::Step
()
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
autodefs.h
OLE Automation Class Definitions.
PRECONDITION
#define PRECONDITION(condition)
Definition
checks.h:227
ocf::TAutoEnumeratorBase::Clear
void Clear()
Definition
autodefs.h:1351
ocf::TAutoEnumeratorBase::Step
bool Step()
Definition
autocli.cpp:308
ocf::TAutoEnumeratorBase::Current
TAutoVal Current
Definition
autodefs.h:1360
ocf::TAutoEnumeratorBase::operator=
void operator=(const TAutoEnumeratorBase ©)
Definition
autocli.cpp:285
ocf::TAutoEnumeratorBase::Iterator
IEnumVARIANT * Iterator
Definition
autodefs.h:1359
ocf::TAutoEnumeratorBase::Bind
void Bind(TAutoVal &val)
Definition
autocli.cpp:297
ocf::TAutoEnumeratorBase::TAutoEnumeratorBase
TAutoEnumeratorBase()
Definition
autodefs.h:1357
ocf::TAutoEnumeratorBase::Unbind
void Unbind()
Definition
autodefs.h:1349
ocf::TAutoProxyArgs
Definition
autodefs.h:1237
ocf::TAutoProxy::Bind
void Bind(IUnknown *obj)
Definition
autocli.cpp:46
ocf::TAutoProxy::GetObject
static IDispatch * GetObject(LPCTSTR progid)
Definition
autocli.cpp:105
ocf::TAutoProxy::Unbind
void Unbind(bool release=true)
Definition
autodefs.h:1415
ocf::TAutoProxy::Lookup
long Lookup(const long id)
Definition
autodefs.h:1315
ocf::TAutoProxy::Lang
owl::TLangId Lang
Definition
autodefs.h:1327
ocf::TAutoProxy::That
IDispatch * That
Definition
autodefs.h:1326
ocf::TAutoProxy::Invoke
TAutoVal & Invoke(owl::uint16 attr, TAutoProxyArgs &args, long *ids, unsigned named=0)
Definition
autocli.cpp:189
ocf::TAutoProxy::MustBeBound
void MustBeBound()
Definition
autocli.cpp:97
ocf::TAutoVal
Automation data element (same data as OLE/BASIC VARIANT).
Definition
autodefs.h:526
ocf::TAutoVal::GetDataType
int GetDataType() const
Definition
autodefs.h:1684
ocf::TAutoVal::Copy
void Copy(const TAutoVal ©)
Definition
autodefs.h:1722
ocf::TClassId
GUID/IID/CLSID management.
Definition
oleutil.h:171
ocf::TXAuto::Raise
static void Raise(TError err)
Definition
autoval.cpp:831
ocf::TXAuto::xNotIDispatch
@ xNotIDispatch
Definition
autodefs.h:120
ocf::TXOle::Check
static void Check(HRESULT stat, LPCTSTR msg)
Definition
except.cpp:47
owl::TAPointer
Pointer to an array of type T.
Definition
pointer.h:103
owl::TString
Reference to reference counted string object TUString Lightweight reference object consisting of a po...
Definition
string.h:67
owl::TUString::ConvertAtoW
static wchar_t * ConvertAtoW(const char *src, size_t len=(size_t) -1)
Definition
ustring.cpp:57
_tcscpy
#define _tcscpy
Definition
cygwin.h:79
wcslen
size_t __stdcall wcslen(const wchar_t *str)
_tcslen
#define _tcslen
Definition
cygwin.h:74
pch.h
Include for OC, gets common headers when precompiled headers are enabled.
ocf
Object Component Framework (COM encapsulation).
Definition
appdesc.h:22
ocf::HRIsOK
bool HRIsOK(HRESULT hr)
Definition
defs.h:132
ocf::acVoidRet
@ acVoidRet
pass NULL for return variant, not OLE2 flag
Definition
autodefs.h:1232
ocf::acPropGet
@ acPropGet
returns property value, same as OLE2
Definition
autodefs.h:1230
ocf::acPropSet
@ acPropSet
set property value, same as OLE2
Definition
autodefs.h:1231
ocf::acMethod
@ acMethod
method call, same as OLE2
Definition
autodefs.h:1229
ocf::_T
_T("!ForeColor")
ocf::atVoid
@ atVoid
Definition
autodefs.h:323
owl::uint16
unsigned short uint16
Definition
number.h:33
owl::DIAG_DECLARE_GROUP
DIAG_DECLARE_GROUP(OwlThread)
HR_NOINTERFACE
#define HR_NOINTERFACE
Definition
defs.h:79
HR_DISP_UNKNOWNNAME
#define HR_DISP_UNKNOWNNAME
Definition
defs.h:98
HR_NOERROR
#define HR_NOERROR
Definition
defs.h:73
OleStr
#define OleStr(s)
Definition
string.h:128
source
ocf
autocli.cpp
Generated by
1.18.0