source: trunk/Utilities/JsFunctionDispatch/Foo.h @ 405

Last change on this file since 405 was 405, checked in by roman, 8 years ago
File size: 2.8 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2015
3// Created by Roman Ryltsov roman@alax.info
4
5#pragma once
6
7#include <atlstr.h>
8#include "resource.h"       // main symbols
9#include "JsFunctionDispatch_i.h"
10
11#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
12#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."
13#endif
14
15using namespace ATL;
16
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 Call(CComPtr<IDispatch>& pFunctionDispatch)
37        {
38                ATLASSERT(pFunctionDispatch);
39                static LONG g_nCounter = 0;
40                CString sText;
41                sText.Format(_T("%d"), ++g_nCounter);
42                CComVariant vText(sText), vResult;
43                ATLENSURE_SUCCEEDED(pFunctionDispatch.Invoke1((DISPID) 0, &vText, &vResult));
44                ATLTRACE(_T("vResult.vt 0x%X\n"), vResult.vt);
45        }
46        static DWORD STDMETHODCALLTYPE ThreadProc(IStream* pStream)
47        {
48                ATLASSERT(pStream);
49                ATLVERIFY(SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)));
50                _ATLTRY
51                {
52                        CComPtr<IDispatch> pFunctionDispatch;
53                        ATLENSURE_SUCCEEDED(CoGetInterfaceAndReleaseStream(pStream, __uuidof(IDispatch), (VOID**) &pFunctionDispatch));
54                        ATLASSERT(pFunctionDispatch);
55                        Call(pFunctionDispatch);
56                        Call(pFunctionDispatch);
57                }
58                _ATLCATCHALL()
59                {
60                }
61                CoUninitialize();
62                return 0;
63        }
64
65// IFoo
66        STDMETHOD(Bar)(IDispatch* pFunction)
67        {
68                ATLTRACE(_T("pFunction 0x%p\n"), pFunction);
69                _ATLTRY
70                {
71                        CComPtr<IDispatch>& pFunctionDispatch = reinterpret_cast<CComPtr<IDispatch>&>(pFunction);
72                        Call(pFunctionDispatch);
73                        Call(pFunctionDispatch);
74                        CComPtr<IStream> pStream;
75                        ATLENSURE_SUCCEEDED(CoMarshalInterThreadInterfaceInStream(__uuidof(IDispatch), pFunctionDispatch, &pStream));
76                        CHandle Thread;
77                        Thread.Attach(AtlCreateThread<IStream>(&CFoo::ThreadProc, pStream.Detach()));
78                        ATLENSURE_THROW(Thread, AtlHresultFromLastError());
79                }
80                _ATLCATCH(Exception)
81                {
82                        return Exception;
83                }
84                return S_OK;
85        }
86};
87
88OBJECT_ENTRY_AUTO(__uuidof(Foo), CFoo)
Note: See TracBrowser for help on using the repository browser.