-
Notifications
You must be signed in to change notification settings - Fork 20
/
jsobject.cpp
186 lines (151 loc) · 4.49 KB
/
jsobject.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <utility>
#include <fstream>
#include "jsobject.h"
#include "atlbase.h"
#include <cstring>
JSObject::JSObject() : ref(0)
{
idMap.insert(std::make_pair(L"execute", DISPID_USER_EXECUTE));
idMap.insert(std::make_pair(L"writefile", DISPID_USER_WRITEFILE));
idMap.insert(std::make_pair(L"readfile", DISPID_USER_READFILE));
idMap.insert(std::make_pair(L"getevar", DISPID_USER_GETVAL));
idMap.insert(std::make_pair(L"setevar", DISPID_USER_SETVAL));
}
HRESULT STDMETHODCALLTYPE JSObject::QueryInterface(REFIID riid, void **ppv)
{
*ppv = NULL;
if (riid == IID_IUnknown || riid == IID_IDispatch) {
*ppv = static_cast<IDispatch*>(this);
}
if (*ppv != NULL) {
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE JSObject::AddRef()
{
return InterlockedIncrement(&ref);
}
ULONG STDMETHODCALLTYPE JSObject::Release()
{
int tmp = InterlockedDecrement(&ref);
if (tmp == 0) {
OutputDebugString("JSObject::Release(): delete this");
delete this;
}
return tmp;
}
HRESULT STDMETHODCALLTYPE JSObject::GetTypeInfoCount(UINT *pctinfo)
{
*pctinfo = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE JSObject::GetTypeInfo(UINT iTInfo, LCID lcid,
ITypeInfo **ppTInfo)
{
return E_FAIL;
}
HRESULT STDMETHODCALLTYPE JSObject::GetIDsOfNames(REFIID riid,
LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
HRESULT hr = S_OK;
for (UINT i = 0; i < cNames; i++) {
std::map<std::wstring, DISPID>::iterator iter = idMap.find(rgszNames[i]);
if (iter != idMap.end()) {
rgDispId[i] = iter->second;
} else {
rgDispId[i] = DISPID_UNKNOWN;
hr = DISP_E_UNKNOWNNAME;
}
}
return hr;
}
HRESULT STDMETHODCALLTYPE JSObject::Invoke(DISPID dispIdMember, REFIID riid,
LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
if (wFlags & DISPATCH_METHOD) {
HRESULT hr = S_OK;
std::string *args = new std::string[pDispParams->cArgs];
for (size_t i = 0; i < pDispParams->cArgs; ++i) {
BSTR bstrArg = pDispParams->rgvarg[i].bstrVal;
LPSTR arg = NULL;
arg = BSTRToLPSTR(bstrArg, arg);
args[pDispParams->cArgs - 1 - i] = arg; // also re-reverse order of arguments
delete [] arg;
}
switch (dispIdMember) {
case DISPID_USER_EXECUTE: {
//LSExecute(NULL, args[0].c_str(), SW_NORMAL);
break;
}
case DISPID_USER_WRITEFILE: {
std::ofstream outfile;
std::ios_base::openmode mode = std::ios_base::out;
if (args[1] == "overwrite") {
mode |= std::ios_base::trunc;
} else if (args[1] == "append") {
mode |= std::ios_base::app;
}
outfile.open(args[0].c_str());
outfile << args[2];
outfile.close();
break;
}
case DISPID_USER_READFILE: {
std::string buffer;
std::string line;
std::ifstream infile;
infile.open(args[0].c_str());
while(std::getline(infile, line)) {
buffer += line;
buffer += "\n";
}
int lenW = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buffer.c_str(), -1, NULL, 0);
BSTR bstrRet = SysAllocStringLen(0, lenW - 1);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buffer.c_str(), -1, bstrRet, lenW);
pVarResult->vt = VT_BSTR;
pVarResult->bstrVal = bstrRet;
break;
}
case DISPID_USER_GETVAL: {
char *buf = new char[256];
strncpy(buf, values[args[0]].c_str(), 256);
int lenW = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, NULL, 0);
BSTR bstrRet = SysAllocStringLen(0, lenW - 1);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, bstrRet, lenW);
pVarResult->vt = VT_BSTR;
pVarResult->bstrVal = bstrRet;
break;
}
case DISPID_USER_SETVAL: {
std::map<std::string, std::string>::iterator itr = values.find(args[0]);
if (itr == values.end()) {
values.insert(std::make_pair(args[0], args[1]));
} else {
values[args[0]] = args[1];
}
break;
}
default:
hr = DISP_E_MEMBERNOTFOUND;
}
delete [] args;
return hr;
}
return E_FAIL;
}
char *JSObject::BSTRToLPSTR(BSTR bStr, LPSTR lpstr)
{
int lenW = SysStringLen(bStr);
int lenA = WideCharToMultiByte(CP_ACP, 0, bStr, lenW, 0, 0, NULL, NULL);
if (lenA > 0) {
lpstr = new char[lenA + 1]; // allocate a final null terminator as well
WideCharToMultiByte(CP_ACP, 0, bStr, lenW, lpstr, lenA, NULL, NULL);
lpstr[lenA] = '\0'; // Set the null terminator yourself
} else {
lpstr = NULL;
}
return lpstr;
}