-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute_wsh_site.h
164 lines (137 loc) · 6.15 KB
/
execute_wsh_site.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <windows.h>
#include <activscp.h>
#include "execute_wsh_dispatch.h"
#define WSH_SCRIPT_NAME OLESTR("proxy_execute_wsh")
typedef struct active_script_site_s {
IActiveScriptSite site;
ULONG ref_count;
script_dispatch_s script_dispatch;
} active_script_site_s;
#define cast_from_site_interface(this) (active_script_site_s *)((uint8_t *)this - offsetof(active_script_site_s, site))
static ULONG STDMETHODCALLTYPE active_script_site_add_ref(IActiveScriptSite *site) {
active_script_site_s *this = cast_from_site_interface(site);
return ++this->ref_count;
}
static ULONG STDMETHODCALLTYPE active_script_site_release(IActiveScriptSite *site) {
active_script_site_s *this = cast_from_site_interface(site);
if (--this->ref_count == 0)
return 0;
return this->ref_count;
}
static HRESULT STDMETHODCALLTYPE active_script_site_query_interface(IActiveScriptSite *site, REFIID riid, void **ppv) {
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IActiveScriptSite)) {
*ppv = site;
active_script_site_add_ref(site);
return NOERROR;
}
*ppv = 0;
return E_NOINTERFACE;
}
static HRESULT STDMETHODCALLTYPE active_script_site_get_lcid(IActiveScriptSite *site, LCID *plcid) {
UNUSED(site);
return plcid ? E_POINTER : E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE active_script_site_get_item_info(IActiveScriptSite *site, LPCOLESTR name,
DWORD return_mask, IUnknown **unknown_item,
ITypeInfo **type_info) {
if (return_mask & SCRIPTINFO_IUNKNOWN) {
if (!unknown_item)
return E_INVALIDARG;
if (wcscmp(name, WSH_SCRIPT_NAME) == 0) {
active_script_site_s *this = cast_from_site_interface(site);
// Return our IDispatch interface to handle native PAC functions
this->script_dispatch.ref_count = 0;
this->script_dispatch.dispatch.lpVtbl = script_dispatch_get_vtbl();
script_dispatch_add_ref(&this->script_dispatch.dispatch);
*unknown_item = (IUnknown *)&this->script_dispatch.dispatch;
return S_OK;
}
*unknown_item = NULL;
}
if (return_mask & SCRIPTINFO_ITYPEINFO) {
if (!type_info)
return E_INVALIDARG;
*type_info = NULL;
}
return TYPE_E_ELEMENTNOTFOUND;
}
static HRESULT STDMETHODCALLTYPE active_script_site_get_doc_version_string(IActiveScriptSite *site,
BSTR *version_string) {
UNUSED(site);
return !version_string ? E_POINTER : E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE active_script_site_on_script_terminate(IActiveScriptSite *site,
const VARIANT *result_ptr,
const EXCEPINFO *excep_info) {
UNUSED(site);
UNUSED(result_ptr);
UNUSED(excep_info);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE active_script_site_on_state_change(IActiveScriptSite *site, SCRIPTSTATE script_state) {
UNUSED(site);
UNUSED(script_state);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE active_script_site_on_script_error(IActiveScriptSite *site,
IActiveScriptError *error) {
EXCEPINFO excep_info = {0};
UNUSED(site);
// Print exception information to log
HRESULT result = IActiveScriptError_GetExceptionInfo(error, &excep_info);
if (FAILED(result)) {
LOG_ERROR("Failed to get active script error (0x%08lx)\n", result);
return S_OK;
}
printf("EXCEPTION: ");
char *excep_source = wchar_dup_to_utf8(excep_info.bstrSource);
if (excep_source) {
printf("%s\n", excep_source);
free(excep_source);
}
char *excep_description = wchar_dup_to_utf8(excep_info.bstrDescription);
if (excep_description) {
printf(" %s", excep_description);
free(excep_description);
}
DWORD source_ctx = 0;
ULONG line_num = 0;
LONG char_pos = 0;
result = IActiveScriptError_GetSourcePosition(error, &source_ctx, &line_num, &char_pos);
if (SUCCEEDED(result))
printf(" @ line %lu char %ld", line_num, char_pos);
printf("\n");
BSTR source_line_text;
result = IActiveScriptError_GetSourceLineText(error, &source_line_text);
if (SUCCEEDED(result)) {
char *excep_source_line = wchar_dup_to_utf8(source_line_text);
if (excep_source_line) {
printf(" %s\n", excep_source_line);
free(excep_source_line);
}
SysFreeString(source_line_text);
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE active_script_site_on_enter_script(IActiveScriptSite *site) {
UNUSED(site);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE active_script_site_on_leave_script(IActiveScriptSite *site) {
UNUSED(site);
return S_OK;
}
static const IActiveScriptSiteVtbl active_script_site_vtbl = {active_script_site_query_interface,
active_script_site_add_ref,
active_script_site_release,
active_script_site_get_lcid,
active_script_site_get_item_info,
active_script_site_get_doc_version_string,
active_script_site_on_script_terminate,
active_script_site_on_state_change,
active_script_site_on_script_error,
active_script_site_on_enter_script,
active_script_site_on_leave_script};
IActiveScriptSiteVtbl *active_script_site_get_vtbl(void) {
return (IActiveScriptSiteVtbl *)&active_script_site_vtbl;
}