source: Include/com/vbobject.ab@ 193

Last change on this file since 193 was 192, checked in by イグトランス (egtra), 18 years ago

Currencyを追加、その他修正

File size: 8.1 KB
Line 
1' com/vbobject.ab
2
3#ifndef _COM_VBIBJECT_AB
4#define _COM_VBIBJECT_AB
5
6#include <com/variant.ab>
7
8Class VBObject
9Public
10 Sub VBObject()
11 pdisp = 0
12 End Sub
13
14 Sub VBObject(className As String, pOuter As *IUnknown, clsContext As DWord)
15 VBObject(ToWCStr(className), pOuter, clsContext)
16 End Sub
17
18 Sub VBObject(className As PCSTR, pOuter As *IUnknown, clsContext As DWord)
19 VBObject(ToWCStr(className), pOuter, clsContext)
20 End Sub
21
22 Sub VBObject(className As PCWSTR, pOuter As *IUnknown, clsContext As DWord)
23 pdisp = 0
24 Dim clsid As CLSID
25 Dim hr = _System_CLSIDFromString(className, clsid)
26 VBObject(clsid, pOuter, clsContext)
27 End Sub
28
29 Sub VBObject(ByRef clsid As CLSID, pOuter As *IUnknown, clsContext As DWord)
30 Dim hr = CoCreateInstance(clsid, pOuter, clsContext, IID_IDispatch, pdisp)
31 End Sub
32
33 Sub VBObject(ByRef obj As VBObject)
34 pdisp = obj.pdisp
35 pdisp->AddRef()
36 End Sub
37
38 Sub Operator =(ByRef obj As VBObject)
39 ~VBObject()
40 VBObject(obj)
41 End Sub
42
43 Sub ~VBObject()
44 Clear()
45 End Sub
46
47 Sub Clear()
48 If pdisp <> 0 Then
49 pdisp->Release()
50 pdisp = 0
51 End If
52 End Sub
53
54 Function Operator [](name As PCWSTR) As DispatchCaller
55 Return GetCaller(name)
56 End Function
57
58 Function Operator [](name As String) As DispatchCaller
59 Return GetCaller(ToWCStr(name))
60 End Function
61
62 Function Equals(y As VBObject) As Boolean
63 Return _System_COMReferenceEquals(pdisp, y.pdisp)
64 End Function
65/*
66 Override Function GetHashCode() As Long
67 Dim punk = _System_GetUnknown(pdisp)
68 GetHashCode = _System_HashFromPtr(punk)
69 punk->Release()
70 End Function
71*/
72 Function Operator ==(y As VBObject) As Boolean
73 Return Equals(y)
74 End Function
75
76 Function Operator <>(y As VBObject) As Boolean
77 Return Not Equals(y)
78 End Function
79
80 Sub Assign(p As *IDispatch)
81 Clear()
82 VBObject.Copy(pdisp, p)
83 End Sub
84
85 Function Copy() As *IDispatch
86 VBObject.Copy(GetDispatch, pdisp)
87 End Function
88
89 Sub Attach(ByRef p As *IDispatch)
90 Clear()
91 VBObject.Move(pdisp, p)
92 End Sub
93
94 Function Detach() As *IDispatch
95 VBObject.Move(Detach, pdisp)
96 End Function
97
98 Function Dispatch() As *IDispatch
99 Dispatch = pdisp
100 End Function
101Private
102 Function GetCaller(name As PCWSTR) As DispatchCaller
103 Dim dispid As DISPID
104 Dim hr = pdisp->GetIDsOfNames(GUID_NULL, VarPtr(name), 1, LOCALE_USER_DEFAULT, VarPtr(dispid))
105 Return New DispatchCaller(pdisp, dispid)
106 End Function
107
108 pdisp As *IDispatch
109
110 Static Sub Copy(ByRef dst As *IDispatch, ByVal src As *IDispatch)
111 dst = src
112 dst->AddRef()
113 End Sub
114
115 Static Sub Move(ByRef dst As *IDispatch, ByRef src As *IDispatch)
116 dst = src
117 src = 0
118 End Sub
119End Class
120
121Class DispatchCaller
122Public
123 Sub DispatchCaller()
124 End Sub
125 Sub DispatchCaller(pDispatch As *IDispatch, dispatchId As DISPID)
126 pdisp = pDispatch
127 pdisp->AddRef()
128 dispid = dispatchId
129 End Sub
130
131 Sub DispatchCaller(ByRef dc As DispatchCaller)
132 pdisp = dc.pdisp
133 pdisp->AddRef()
134 dispid = dc.dispid
135 End Sub
136
137 Sub Operator =(ByRef dc As DispatchCaller)
138 Dispose()
139 DispatchCaller(dc)
140 End Sub
141
142 Sub Dispose()
143 If pdisp <> 0 Then
144 pdisp->Release()
145 pdisp = 0
146 End If
147 End Sub
148
149 Sub ~DispatchCaller()
150 Dispose()
151 End Sub
152
153 Function Prop() As Variant
154 Dim dispParams As DISPPARAMS
155 With dispParams
156 .rgvarg = 0
157 .rgdispidNamedArgs = 0
158 .cArgs = 0
159 .cNamedArgs = 0
160 End With
161 Dim ret As VARIANT
162 Dim hr = pdisp->Invoke(dispid, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, dispParams, ret, 0, 0)
163 Dispose()
164 Dim v = New Variant
165 v.Attach(ret)
166 Return v
167 End Function
168
169 Sub Prop(ByRef arg As Variant)
170 Prop(ByVal arg.PtrToVariant)
171 End Sub
172
173 Sub Prop(ByRef arg As VARIANT)
174 setProp(arg, DISPATCH_PROPERTYPUT)
175 End Sub
176
177 Sub PropRef(ByRef arg As Variant)
178 PropRef(arg.PtrToVariant)
179 End Sub
180
181 Sub PropRef(ByRef arg As VARIANT)
182 setProp(arg, DISPATCH_PROPERTYPUTREF)
183 End Sub
184
185 Function Call(cArgs As Long, args As *VARIANT) As Variant
186 Dim dispParams As DISPPARAMS
187 With dispParams
188 .rgvarg = args
189 .rgdispidNamedArgs = 0
190 .cArgs = cArgs
191 .cNamedArgs = 0
192 End With
193 Dim ret As VARIANT
194 Dim hr = pdisp->Invoke(dispid, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, dispParams, ret, ByVal 0, 0)
195 Dispose()
196 Dim v = New Variant
197 v.Attach(ret)
198 Return v
199 End Function
200
201' Function Call(cArgs As Long, args As *Variant) As Variant
202' End Function
203
204 Function Call() As Variant
205 Return Call(0, 0)
206 End Function
207
208 Function Call(arg1 As Variant) As Variant
209 Return Call(1, arg1.PtrToVariant)
210 End Function
211
212 Function Call(ByRef arg1 As Variant, ByRef arg2 As Variant) As Variant
213 Dim arg[1] As VARIANT
214
215 Return Call(2, VarPtr(arg1) As *VARIANT)
216 End Function
217
218Private
219 Sub setProp(ByRef arg As VARIANT, callType As Word)
220 Dim dispidNamed = DISPID_PROPERTYPUT As DISPID
221 Dim dispParams As DISPPARAMS
222 With dispParams
223 .rgvarg = VarPtr(arg)
224 .rgdispidNamedArgs = VarPtr(dispidNamed)
225 .cArgs = 1
226 .cNamedArgs = 1
227 End With
228 Dim hr = pdisp->Invoke(dispid, GUID_NULL, LOCALE_USER_DEFAULT, callType, dispParams, ByVal 0, ByVal 0, 0)
229 Dispose()
230 End Sub
231
232
233 pdisp As *IDispatch
234 dispid As DISPID
235End Class
236
237Function CallByName(obj As VBObject, procName As String, callType As Word, cArgs As Long, args As *VARIANT) As Variant
238 Return CallByName(obj.Dispatch, ToWCStr(procName), callType, cArgs, args)
239End Function
240
241Function CallByName(obj As VBObject, procName As PCWSTR, callType As Word, cArgs As Long, args As *VARIANT) As Variant
242 Return CallByName(obj.Dispatch, procName, callType, cArgs, args)
243End Function
244
245Function CallByName(obj As *IDispatch, name As PCWSTR, callType As Word, cArgs As Long, args As *VARIANT) As Variant
246 Dim dispid As DISPID
247 Dim hr = obj->GetIDsOfNames(GUID_NULL, VarPtr(name), 1, LOCALE_USER_DEFAULT, VarPtr(dispid))
248 Dim dispParams As DISPPARAMS
249 With dispParams
250 .rgvarg = args
251 .rgdispidNamedArgs = 0
252 .cArgs = cArgs
253 .cNamedArgs = 0
254 End With
255 Dim ret As VARIANT
256 hr = obj->Invoke(dispid, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, dispParams, ret, ByVal 0, 0)
257 CallByName.Attach(ret)
258 Return CallByName
259End Function
260
261Function CreateObject(className As PCWSTR) As VBObject
262 Return New VBObject(className)
263End Function
264
265Function CreateObject(className As String) As VBObject
266 Return New VBObject(ToWCStr(className))
267End Function
268/*
269#ifdef _WIN32_DCOM
270Function CreateObject(className As PCWSTR, serverName As PCWSTR) As VBObject
271 Dim clsid As CLSID
272 Dim si As COSERVERINFO
273 Dim context As DWord
274 If /*Server = 0 OrElse* / Server[0] = 0 Then
275 context = CLSCTX_SERVER
276 Else
277 context = CLSCTX_REMOTE_SERVER
278 si.pwszName = serverName
279 End If
280
281 Dim hr = _System_CLSIDFromString(className, clsid)
282
283 Dim mqi As MULTI_QI
284 mqi.pIID = VarPtr(IID_IUnknown)
285 hr = CoCreateInstanceEx(clsid, 0, context, si, 1, VarPtr(mqi))
286 If SUCCEEDED(hr) Then
287 Dim obj As VBObject
288 obj.Attach(mqi.pItf As IDispatch)
289 Return obj
290 Else
291 'Throw
292 Return Nothing
293 End If
294End Function
295
296Function CreateObject(className As String, serverName As String) As VBObject
297 Return CreateObject(ToWCStr(className), ToWCStr(serverName))
298End Function
299#endif
300*/
301Function _System_CLSIDFromString(pwString As PCWSTR, ByRef guid As GUID) As HRESULT
302 If pwString[0] = &h007b As WCHAR Then
303 ' 1文字目が { ならクラスIDの文字列表現と見なしCLSIDFromStringで変換。
304 _System_CLSIDFromString = CLSIDFromString(pwString, guid)
305 Else
306 _System_CLSIDFromString = CLSIDFromProgID(pwString, guid)
307 End If
308End Function
309
310Function _System_COMReferenceEquals(p As *IUnknown, q As *IUnknown) As Boolean
311 If p = q Then
312 Return True
313 Else If p = 0 Or q = 0 Then
314 Return False
315 End If
316
317 Dim punkX = _System_GetUnknown(p)
318 Dim punkY = _System_GetUnknown(q)
319 If punkX = punkY Then
320 _System_COMReferenceEquals = True
321 Else
322 _System_COMReferenceEquals = False
323 End If
324 punkX->Release()
325 punkY->Release()
326End Function
327
328Function _System_GetUnknown(p As *IUnknown) As *IUnknown 'pは任意のCOMインタフェース
329 If FAILDED(pdisp->QueryInterface(IID_Unknown, _System_GetUnknown)) Then
330 GetUnknown = 0
331 End If
332End Function
333
334#endif '_COM_VBIBJECT_AB
Note: See TracBrowser for help on using the repository browser.