source: trunk/Utilities/JsObjectDispatch/Foo.h @ 937

Last change on this file since 937 was 919, checked in by roman, 4 years ago

Update and VBS test

  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2012, 2019
3// Created by Roman Ryltsov roman@alax.info
4
5#pragma once
6
7#include <atlstr.h>
8#include <atlsafe.h>
9#include "resource.h"       // main symbols
10#include "JsObjectDispatch_i.h"
11
12#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
13#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
14#endif
15
16using namespace ATL;
17
18// CFoo
19
20class ATL_NO_VTABLE CFoo :
21        public CComObjectRootEx<CComSingleThreadModel>,
22        public CComCoClass<CFoo, &CLSID_Foo>,
23        public IDispatchImpl<IFoo>
24{
25public:
26
27DECLARE_REGISTRY_RESOURCEID(IDR_FOO)
28
29BEGIN_COM_MAP(CFoo)
30        COM_INTERFACE_ENTRY(IFoo)
31        COM_INTERFACE_ENTRY(IDispatch)
32END_COM_MAP()
33
34public:
35// CFoo
36        static VOID TraceProperty(CComPtr<IDispatch> pDispatch, DISPID nIdentifier, BOOL bTraceGetResultFailure = TRUE)
37        {
38                CComVariant vPropertyValue;
39                const HRESULT nGetResult = pDispatch.GetProperty(nIdentifier, &vPropertyValue);
40                if(SUCCEEDED(nGetResult))
41                {
42                        CComVariant vStringPropertyValue;
43                        const HRESULT nChangeTypeResult = vStringPropertyValue.ChangeType(VT_BSTR, &vPropertyValue);
44                        if(SUCCEEDED(nChangeTypeResult))
45                        {
46                                ATLTRACE(_T("nIdentifier 0x%x, vPropertyValue.vt 0x%x, vStringPropertyValue.bstrVal \"%s\"\n"), nIdentifier, vPropertyValue.vt, CString(vStringPropertyValue.bstrVal));
47                        } else
48                                ATLTRACE(_T("nIdentifier 0x%x, vPropertyValue.vt 0x%x, nChangeTypeResult 0x%08x\n"), nIdentifier, vPropertyValue.vt, nChangeTypeResult);
49                } else
50                        if(bTraceGetResultFailure)
51                                ATLTRACE(_T("nIdentifier 0x%x, nGetResult 0x%08x\n"), nIdentifier, nGetResult);
52        }
53        static VOID TraceProperty(CComPtr<IDispatch> pDispatch, LPCWSTR pszName, BOOL bTraceGetResultFailure = TRUE)
54        {
55                ATLASSERT(pszName);
56                CComVariant vPropertyValue;
57                const HRESULT nGetResult = pDispatch.GetPropertyByName(pszName, &vPropertyValue);
58                if(SUCCEEDED(nGetResult))
59                {
60                        CComVariant vStringPropertyValue;
61                        const HRESULT nChangeTypeResult = vStringPropertyValue.ChangeType(VT_BSTR, &vPropertyValue);
62                        if(SUCCEEDED(nChangeTypeResult))
63                        {
64                                ATLTRACE(_T("pszName %ls, vPropertyValue.vt 0x%x, vStringPropertyValue.bstrVal \"%s\"\n"), pszName, vPropertyValue.vt, CString(vStringPropertyValue.bstrVal));
65                        } else
66                                ATLTRACE(_T("pszName %ls, vPropertyValue.vt 0x%x, nChangeTypeResult 0x%08x\n"), pszName, vPropertyValue.vt, nChangeTypeResult);
67                } else
68                        if(bTraceGetResultFailure)
69                                ATLTRACE(_T("pszName %ls, nGetResult 0x%08x\n"), pszName, nGetResult);
70        }
71
72// IFoo
73        STDMETHOD(BarA)(VARIANT Value) throw()
74        {
75                ATLTRACE(_T("-------- Value.vt 0x%x -------- -------- --------\n"), Value.vt);
76                if(Value.vt != VT_DISPATCH)
77                {
78                        ATLTRACE(_T("Value.lVal 0x%x\n"), Value.lVal);
79                        return S_FALSE;
80                }
81                ATLASSERT(Value.vt == VT_DISPATCH);
82                CComQIPtr<IDispatchEx> pDispatchEx = Value.pdispVal;
83                #if defined(_DEBUG)
84                if(pDispatchEx)
85                {
86                        DISPID nIdentifier = DISPID_STARTENUM;
87                        for(; ; )
88                        {
89                                if(pDispatchEx->GetNextDispID(fdexEnumAll, nIdentifier, &nIdentifier) != S_OK)
90                                        break;
91                                CComBSTR sName;
92                                ATLVERIFY(SUCCEEDED(pDispatchEx->GetMemberName(nIdentifier, &sName)));
93                                ATLTRACE(_T("nIdentifier %d, sName %s\n"), nIdentifier, CString(sName));
94                                TraceProperty((IDispatchEx*) pDispatchEx, sName);
95                        }
96                }
97                #endif // defined(_DEBUG)
98                //TraceProperty(Value.pdispVal, (DISPID) DISPID_VALUE);
99                for(DISPID nIdentifier = -1000; nIdentifier <= 1000; nIdentifier++)
100                        TraceProperty(Value.pdispVal, nIdentifier, FALSE);
101                return S_OK;
102        }
103        STDMETHOD(get_BarB)(VARIANT* Value) throw()
104        {
105                ATLASSERT(Value);
106                CComSafeArray<VARIANT> Array;
107                ATLVERIFY(SUCCEEDED(Array.Create(2, 0)));
108                CComVariant ValueA(777, VT_I4);
109                ATLVERIFY(SUCCEEDED(Array.SetAt(0, ValueA)));
110                CComVariant ValueB(L"777-777");
111                ATLVERIFY(SUCCEEDED(Array.SetAt(1, ValueB)));
112                CComVariant VariantValue;
113                VariantValue.vt = VT_ARRAY | VT_VARIANT;
114                VariantValue.parray = Array.Detach();
115                ATLVERIFY(SUCCEEDED(VariantValue.Detach(Value)));
116                return S_OK;
117        }
118};
119
120OBJECT_ENTRY_AUTO(__uuidof(Foo), CFoo)
Note: See TracBrowser for help on using the repository browser.