forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostObjectSampleImpl.cpp
More file actions
100 lines (85 loc) · 3.01 KB
/
HostObjectSampleImpl.cpp
File metadata and controls
100 lines (85 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "HostObjectSampleImpl.h"
HostObjectSample::HostObjectSample(HostObjectSample::RunCallbackAsync runCallbackAsync)
: m_propertyValue(L"Example Property String Value"), m_runCallbackAsync(runCallbackAsync)
{
}
STDMETHODIMP HostObjectSample::MethodWithParametersAndReturnValue(
BSTR stringParameter, INT integerParameter, BSTR* stringResult)
{
std::wstring result = L"MethodWithParametersAndReturnValue(";
result += stringParameter;
result += L", ";
result += std::to_wstring(integerParameter);
result += L") called.";
*stringResult = SysAllocString(result.c_str());
return S_OK;
}
STDMETHODIMP HostObjectSample::get_Property(BSTR* stringResult)
{
*stringResult = SysAllocString(m_propertyValue.c_str());
return S_OK;
}
STDMETHODIMP HostObjectSample::put_Property(BSTR stringValue)
{
m_propertyValue = stringValue;
return S_OK;
}
STDMETHODIMP HostObjectSample::get_IndexedProperty(INT index, BSTR* stringResult)
{
std::wstring result(L"[");
result = result + std::to_wstring(index) + L"] is " + m_propertyValues[index] + L".";
*stringResult = SysAllocString(result.c_str());
return S_OK;
}
STDMETHODIMP HostObjectSample::put_IndexedProperty(INT index, BSTR stringValue)
{
m_propertyValues[index] = stringValue;
return S_OK;
}
STDMETHODIMP HostObjectSample::CallCallbackAsynchronously(IDispatch* callbackParameter)
{
wil::com_ptr<IDispatch> callbackParameterForCapture = callbackParameter;
m_runCallbackAsync([callbackParameterForCapture]() -> void {
callbackParameterForCapture->Invoke(
DISPID_UNKNOWN, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, nullptr, nullptr,
nullptr, nullptr);
});
return S_OK;
}
STDMETHODIMP HostObjectSample::GetTypeInfoCount(UINT* pctinfo)
{
*pctinfo = 1;
return S_OK;
}
STDMETHODIMP HostObjectSample::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
{
if (0 != iTInfo)
{
return TYPE_E_ELEMENTNOTFOUND;
}
if (!m_typeLib)
{
RETURN_IF_FAILED(LoadTypeLib(L"WebView2APISample.tlb", &m_typeLib));
}
return m_typeLib->GetTypeInfoOfGuid(__uuidof(IHostObjectSample), ppTInfo);
}
STDMETHODIMP HostObjectSample::GetIDsOfNames(
REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
wil::com_ptr<ITypeInfo> typeInfo;
RETURN_IF_FAILED(GetTypeInfo(0, lcid, &typeInfo));
return typeInfo->GetIDsOfNames(rgszNames, cNames, rgDispId);
}
STDMETHODIMP HostObjectSample::Invoke(
DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
wil::com_ptr<ITypeInfo> typeInfo;
RETURN_IF_FAILED(GetTypeInfo(0, lcid, &typeInfo));
return typeInfo->Invoke(
this, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}