-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddInNative.cpp
More file actions
319 lines (282 loc) · 8.43 KB
/
AddInNative.cpp
File metadata and controls
319 lines (282 loc) · 8.43 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#if !defined( __linux__ ) && !defined(__APPLE__) && !defined(__ANDROID__)
#include "stdafx.h"
#endif
#include <stdio.h>
#include <wchar.h>
#include "AddInNative.h"
static const std::u16string sClassName(u"SecureStorage");
static const std::u16string sVersion(u"1.0");
static const std::array<std::u16string, CAddInNative::eMethLast> osMethods = { u"getvalue",u"addvalue"};
static const std::array<std::u16string, CAddInNative::eMethLast> osMethods_ru = { u"получитьзначение",u"добавитьзначение"};
static const std::array<std::u16string, CAddInNative::ePropLast> osProps = {};
static const std::array<std::u16string, CAddInNative::ePropLast> osProps_ru = {};
AppCapabilities g_capabilities = eAppCapabilitiesInvalid;
//---------------------------------------------------------------------------//
long GetClassObject(const WCHAR_T* wsName, IComponentBase** pInterface)
{
if (!*pInterface)
{
*pInterface = new CAddInNative;
return (long)*pInterface;
}
return 0;
}
//---------------------------------------------------------------------------//
AppCapabilities SetPlatformCapabilities(const AppCapabilities capabilities)
{
g_capabilities = capabilities;
return eAppCapabilitiesLast;
}
//---------------------------------------------------------------------------//
long DestroyObject(IComponentBase** pIntf)
{
if (!*pIntf)
return -1;
delete *pIntf;
*pIntf = 0;
return 0;
}
//---------------------------------------------------------------------------//
const WCHAR_T* GetClassNames()
{
return (WCHAR_T*)sClassName.c_str();
}
//---------------------------------------------------------------------------//
// CAddInNative
//---------------------------------------------------------------------------//
CAddInNative::CAddInNative()
{
m_iMemory = 0;
m_iConnect = 0;
}
//---------------------------------------------------------------------------//
CAddInNative::~CAddInNative()
{
}
//---------------------------------------------------------------------------//
bool CAddInNative::Init(void* pConnection)
{
m_iConnect = (IAddInDefBase*)pConnection;
return m_iConnect != NULL;
}
//---------------------------------------------------------------------------//
long CAddInNative::GetInfo()
{
// Component should put supported component technology version
// This component supports 2.1 version
return 2100;
}
//---------------------------------------------------------------------------//
void CAddInNative::Done()
{
}
/////////////////////////////////////////////////////////////////////////////
// ILanguageExtenderBase
//---------------------------------------------------------------------------//
bool CAddInNative::RegisterExtensionAs(WCHAR_T** wsExtensionName)
{
if (wsExtensionName == nullptr) {
return false;
}
const size_t size = (sClassName.size() + 1) * sizeof(char16_t);
if (!m_iMemory || !m_iMemory->AllocMemory(reinterpret_cast<void**>(wsExtensionName), size) || *wsExtensionName == nullptr) {
return false;
};
memcpy(*wsExtensionName, sClassName.c_str(), size);
return true;
}
//---------------------------------------------------------------------------//
long CAddInNative::GetNProps()
{
return ePropLast;
}
//---------------------------------------------------------------------------//
long CAddInNative::FindProp(const WCHAR_T* wsPropName)
{
return -1;
}
//---------------------------------------------------------------------------//
const WCHAR_T* CAddInNative::GetPropName(long lPropNum, long lPropAlias)
{
return 0;
}
//---------------------------------------------------------------------------//
bool CAddInNative::GetPropVal(const long lPropNum, tVariant* pvarPropVal)
{
return true;
}
//---------------------------------------------------------------------------//
bool CAddInNative::SetPropVal(const long lPropNum, tVariant *varPropVal)
{
return false;
}
//---------------------------------------------------------------------------//
bool CAddInNative::IsPropReadable(const long lPropNum)
{
return false;
}
//---------------------------------------------------------------------------//
bool CAddInNative::IsPropWritable(const long lPropNum)
{
return false;
}
//---------------------------------------------------------------------------//
long CAddInNative::GetNMethods()
{
return eMethLast;
}
//---------------------------------------------------------------------------//
long CAddInNative::FindMethod(const WCHAR_T* wsMethodName)
{
std::u16string usMethodName = (char16_t*)(wsMethodName);
tolowerStr(usMethodName);
auto it = std::find(osMethods.begin(), osMethods.end(), usMethodName);
if (it != osMethods.end()) {
return it - osMethods.begin();
}
it = std::find(osMethods_ru.begin(), osMethods_ru.end(), usMethodName);
if (it != osMethods_ru.end()) {
return it - osMethods_ru.begin();
}
return -1;
}
//---------------------------------------------------------------------------//
const WCHAR_T* CAddInNative::GetMethodName(const long lMethodNum, const long lMethodAlias)
{
if (lMethodNum >= eMethLast)
return nullptr;
const std::basic_string<char16_t>* usCurrentName;
switch (lMethodAlias)
{
case 0: // First language
{
usCurrentName = &osMethods[lMethodNum];
break;
}
case 1: // Second language
{
usCurrentName = &osMethods_ru[lMethodNum];
break;
}
default:
return nullptr;
}
if (usCurrentName == nullptr)
return nullptr;
WCHAR_T* result = nullptr;
const size_t bytes = (usCurrentName->length() + 1) * sizeof(char16_t);
if (!m_iMemory || !m_iMemory->AllocMemory(reinterpret_cast<void**>(&result), bytes)) {
return nullptr;
};
memcpy(result, usCurrentName->c_str(), bytes);
return result;
}
//---------------------------------------------------------------------------//
long CAddInNative::GetNParams(const long lMethodNum)
{
switch (lMethodNum)
{
case eMethGetValue:
return 1;
case eMethAddValue:
return 2;
default:
return 0;
}
return 0;
}
//---------------------------------------------------------------------------//
bool CAddInNative::GetParamDefValue(const long lMethodNum, const long lParamNum,
tVariant *pvarParamDefValue)
{
TV_VT(pvarParamDefValue) = VTYPE_EMPTY;
return false;
}
//---------------------------------------------------------------------------//
bool CAddInNative::HasRetVal(const long lMethodNum)
{
switch (lMethodNum)
{
case eMethGetValue:
return true;
default:
return false;
}
return false;
}
//---------------------------------------------------------------------------//
bool CAddInNative::CallAsProc(const long lMethodNum,
tVariant* paParams, const long lSizeArray)
{
switch (lMethodNum)
{
case eMethAddValue:
{
if (paParams[0].vt != VTYPE_PWSTR || paParams[1].vt != VTYPE_PWSTR)
{
return false;
}
const std::wstring valueName{paParams[0].pwstrVal, paParams[0].wstrLen };
CREDENTIALW cred = { 0 };
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = (LPWSTR)valueName.c_str();
cred.CredentialBlobSize = paParams[1].wstrLen * sizeof(wchar_t);
cred.CredentialBlob = (LPBYTE)paParams[1].pwstrVal;
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
cred.UserName = nullptr;
CredWriteW(&cred, 0);
return true;
}
default:
return false;
}
return true;
}
//---------------------------------------------------------------------------//
bool CAddInNative::CallAsFunc(const long lMethodNum,
tVariant* pvarRetValue, tVariant* paParams, const long lSizeArray)
{
switch (lMethodNum)
{
case eMethGetValue:
{
if (paParams[0].vt != VTYPE_PWSTR)
{
return false;
}
TV_VT(pvarRetValue) = VTYPE_PWSTR;
std::wstring valueName{ paParams[0].pwstrVal, paParams[0].wstrLen};
std::wstring res;
PCREDENTIALW pcred;
if (CredReadW(valueName.c_str(), CRED_TYPE_GENERIC, 0, &pcred)) {
res.assign((wchar_t*)pcred->CredentialBlob, pcred->CredentialBlobSize / sizeof(wchar_t));
CredFree(pcred);
}
if (m_iMemory->AllocMemory((void**)&pvarRetValue->pwstrVal, (res.length() + 1) * sizeof(wchar_t)))
{
memcpy(pvarRetValue->pwstrVal, res.c_str(), (res.length() + 1) * sizeof(wchar_t));
pvarRetValue->wstrLen = res.length();
}
return true;
}
default:
return false;
}
return true;
}
void CAddInNative::SetLocale(const WCHAR_T* loc)
{
#if !defined( __linux__ ) && !defined(__APPLE__) && !defined(__ANDROID__)
_wsetlocale(LC_ALL, L"");
#else
setlocale(LC_ALL, "");
#endif
}
/////////////////////////////////////////////////////////////////////////////
// LocaleBase
//---------------------------------------------------------------------------//
bool CAddInNative::setMemManager(void* mem)
{
m_iMemory = (IMemoryManager*)mem;
return m_iMemory != 0;
}