-
Notifications
You must be signed in to change notification settings - Fork 162
/
CWSH.cpp
454 lines (404 loc) · 11.8 KB
/
CWSH.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*! @file
@brief WSH Handler
@author 鬼
@date 2002年4月28日
*/
/*
Copyright (C) 2002, 鬼, genta
Copyright (C) 2003, FILE
Copyright (C) 2004, genta
Copyright (C) 2005, FILE, zenryaku
Copyright (C) 2009, syat
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include <process.h> // _beginthreadex
#ifdef __MINGW32__
#define INITGUID 1
#endif
#include <ObjBase.h>
#include <InitGuid.h>
#include <ShlDisp.h>
#include "macro/CWSH.h"
#include "macro/CIfObj.h"
#include "window/CEditWnd.h"
#include "util/os.h"
#include "util/module.h"
#include "util/window.h" // BlockingHook
#include "dlg/CDlgCancel.h"
#include "sakura_rc.h"
#ifdef USE_JSCRIPT9
const GUID CLSID_JSScript9 =
{
0x16d51579, 0xa30b, 0x4c8b, { 0xa2, 0x76, 0x0f, 0xf4, 0xdc, 0x41, 0xe7, 0x55 }
};
#endif
/* 2009.10.29 syat インタフェースオブジェクト部分をCWSHIfObj.hに分離
class CInterfaceObjectTypeInfo: public ImplementsIUnknown<ITypeInfo>
*/
//IActiveScriptSite, IActiveScriptSiteWindow
/*!
@date Sep. 15, 2005 FILE IActiveScriptSiteWindow実装.
マクロでMsgBoxを使用可能にする.
*/
class CWSHSite: public IActiveScriptSite, public IActiveScriptSiteWindow
{
private:
CWSHClient *m_Client;
ITypeInfo *m_TypeInfo;
ULONG m_RefCount;
public:
CWSHSite(CWSHClient *AClient): m_Client(AClient), m_RefCount(0)
{
}
virtual ULONG STDMETHODCALLTYPE AddRef() {
return ++m_RefCount;
}
virtual ULONG STDMETHODCALLTYPE Release() {
if(--m_RefCount == 0)
{
delete this;
return 0;
}
return m_RefCount;
}
virtual HRESULT STDMETHODCALLTYPE QueryInterface(
/* [in] */ REFIID iid,
/* [out] */ void ** ppvObject)
{
*ppvObject = NULL;
if(iid == IID_IActiveScriptSiteWindow){
*ppvObject = static_cast<IActiveScriptSiteWindow*>(this);
++m_RefCount;
return S_OK;
}
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetLCID(
/* [out] */ LCID *plcid)
{
#ifdef TEST
cout << "GetLCID" << endl;
#endif
return E_NOTIMPL; //システムデフォルトを使用
}
virtual HRESULT STDMETHODCALLTYPE GetItemInfo(
/* [in] */ LPCOLESTR pstrName,
/* [in] */ DWORD dwReturnMask,
/* [out] */ IUnknown **ppiunkItem,
/* [out] */ ITypeInfo **ppti)
{
#ifdef TEST
wcout << L"GetItemInfo:" << pstrName << endl;
#endif
//指定された名前のインタフェースオブジェクトを検索
const CWSHClient::List& objects = m_Client->GetInterfaceObjects();
for( CWSHClient::ListIter it = objects.begin(); it != objects.end(); it++ )
{
// Nov. 10, 2003 FILE Win9Xでは、[lstrcmpiW]が無効のため、[_wcsicmp]に修正
if( _wcsicmp( pstrName, (*it)->m_sName.c_str() ) == 0 )
{
if(dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
(*ppiunkItem) = *it;
(*ppiunkItem)->AddRef();
}
if(dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
(*it)->GetTypeInfo(0, 0, ppti);
}
return S_OK;
}
}
return TYPE_E_ELEMENTNOTFOUND;
}
virtual HRESULT STDMETHODCALLTYPE GetDocVersionString(
/* [out] */ BSTR *pbstrVersion)
{
#ifdef TEST
cout << "GetDocVersionString" << endl;
#endif
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnScriptTerminate(
/* [in] */ const VARIANT *pvarResult,
/* [in] */ const EXCEPINFO *pexcepinfo)
{
#ifdef TEST
cout << "OnScriptTerminate" << endl;
#endif
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStateChange(
/* [in] */ SCRIPTSTATE ssScriptState)
{
#ifdef TEST
cout << "OnStateChange" << endl;
#endif
return S_OK;
}
// Nov. 3, 2002 鬼
// エラー行番号表示対応
virtual HRESULT STDMETHODCALLTYPE OnScriptError(
/* [in] */ IActiveScriptError *pscripterror)
{
EXCEPINFO Info;
if(pscripterror->GetExceptionInfo(&Info) == S_OK)
{
DWORD Context;
ULONG Line;
LONG Pos;
if(Info.bstrDescription == NULL) {
Info.bstrDescription = SysAllocString(LS(STR_ERR_CWSH09));
}
if(pscripterror->GetSourcePosition(&Context, &Line, &Pos) == S_OK)
{
wchar_t *Message = new wchar_t[SysStringLen(Info.bstrDescription) + 128];
// Nov. 10, 2003 FILE Win9Xでは、[wsprintfW]が無効のため、[auto_sprintf]に修正
const wchar_t* szDesc=Info.bstrDescription;
auto_sprintf(Message, L"[Line %d] %ls", Line + 1, szDesc);
SysReAllocString(&Info.bstrDescription, Message);
delete[] Message;
}
m_Client->Error(Info.bstrDescription, Info.bstrSource);
SysFreeString(Info.bstrSource);
SysFreeString(Info.bstrDescription);
SysFreeString(Info.bstrHelpFile);
}
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnEnterScript() {
#ifdef TEST
cout << "OnEnterScript" << endl;
#endif
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnLeaveScript() {
#ifdef TEST
cout << "OnLeaveScript" << endl;
#endif
return S_OK;
}
// Sep. 15, 2005 FILE IActiveScriptSiteWindow実装
virtual HRESULT STDMETHODCALLTYPE GetWindow(
/* [out] */ HWND *phwnd)
{
*phwnd = CEditWnd::getInstance()->m_cSplitterWnd.GetHwnd();
return S_OK;
}
// Sep. 15, 2005 FILE IActiveScriptSiteWindow実装
virtual HRESULT STDMETHODCALLTYPE EnableModeless(
/* [in] */ BOOL fEnable)
{
return S_OK;
}
};
//implementation
CWSHClient::CWSHClient(const wchar_t *AEngine, ScriptErrorHandler AErrorHandler, void *AData):
m_OnError(AErrorHandler), m_Data(AData), m_Valid(false), m_Engine(NULL)
{
// 2010.08.28 DLL インジェクション対策としてEXEのフォルダに移動する
CCurrentDirectoryBackupPoint dirBack;
ChangeCurrentDirectoryToExeDir();
CLSID ClassID;
if(CLSIDFromProgID(AEngine, &ClassID) != S_OK)
Error(LS(STR_ERR_CWSH01));
else
{
#ifdef USE_JSCRIPT9
if( 0 == wcscmp( AEngine, LTEXT("JScript") ) ){
ClassID = CLSID_JSScript9;
}
#endif
if(CoCreateInstance(ClassID, 0, CLSCTX_INPROC_SERVER, IID_IActiveScript, reinterpret_cast<void **>(&m_Engine)) != S_OK)
Error(LS(STR_ERR_CWSH02));
else
{
IActiveScriptSite *Site = new CWSHSite(this);
if(m_Engine->SetScriptSite(Site) != S_OK)
{
delete Site;
Error(LS(STR_ERR_CWSH03));
}
else
{
m_Valid = true;
}
}
}
}
CWSHClient::~CWSHClient()
{
//インタフェースオブジェクトを解放
for( ListIter it = m_IfObjArr.begin(); it != m_IfObjArr.end(); it++ ){
(*it)->Release();
}
if(m_Engine != NULL)
m_Engine->Release();
}
// AbortMacroProcのパラメータ構造体
typedef struct {
HANDLE hEvent;
IActiveScript *pEngine; //ActiveScript
int nCancelTimer;
CEditView *view;
} SAbortMacroParam;
// WSHマクロ実行を中止するスレッド
static unsigned __stdcall AbortMacroProc( LPVOID lpParameter )
{
SAbortMacroParam* pParam = (SAbortMacroParam*) lpParameter;
//停止ダイアログ表示前に数秒待つ
if(::WaitForSingleObject(pParam->hEvent, pParam->nCancelTimer * 1000) == WAIT_TIMEOUT){
//停止ダイアログ表示
DEBUG_TRACE(L"AbortMacro: Show Dialog\n");
MSG msg;
CDlgCancel cDlgCancel;
HWND hwndDlg = cDlgCancel.DoModeless(G_AppInstance(), NULL, IDD_MACRORUNNING); // エディタビジーでも表示できるよう、親を指定しない
// ダイアログタイトルとファイル名を設定
::SendMessage(hwndDlg, WM_SETTEXT, 0, (LPARAM)GSTR_APPNAME);
::SendMessage(GetDlgItem(hwndDlg, IDC_STATIC_CMD),
WM_SETTEXT, 0, (LPARAM)pParam->view->GetDocument()->m_cDocFile.GetFilePath());
bool bCanceled = false;
for(;;){
DWORD dwResult = MsgWaitForMultipleObjects( 1, &pParam->hEvent, FALSE, INFINITE, QS_ALLINPUT );
if(dwResult == WAIT_OBJECT_0){
::SendMessage( cDlgCancel.GetHwnd(), WM_CLOSE, 0, 0 );
}else if(dwResult == WAIT_OBJECT_0+1){
while(::PeekMessage(&msg , NULL , 0 , 0, PM_REMOVE )){
if(cDlgCancel.GetHwnd() != NULL && ::IsDialogMessage(cDlgCancel.GetHwnd(), &msg)){
}else{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
}else{
//MsgWaitForMultipleObjectsに与えたハンドルのエラー
break;
}
if(!bCanceled && cDlgCancel.IsCanceled()){
DEBUG_TRACE(L"Canceld\n");
bCanceled = true;
cDlgCancel.CloseDialog( 0 );
}
if(cDlgCancel.GetHwnd() == NULL){
DEBUG_TRACE(L"Close\n");
break;
}
}
DEBUG_TRACE(L"AbortMacro: Try Interrupt\n");
pParam->pEngine->InterruptScriptThread(SCRIPTTHREADID_BASE, NULL, 0);
DEBUG_TRACE(L"AbortMacro: Done\n");
}
DEBUG_TRACE(L"AbortMacro: Exit\n");
return 0;
}
bool CWSHClient::Execute(const wchar_t *AScript)
{
bool bRet = false;
IActiveScriptParse *Parser;
if(m_Engine->QueryInterface(IID_IActiveScriptParse, reinterpret_cast<void **>(&Parser)) != S_OK)
Error(LS(STR_ERR_CWSH04));
else
{
if(Parser->InitNew() != S_OK)
Error(LS(STR_ERR_CWSH05));
else
{
bool bAddNamedItemError = false;
for( ListIter it = m_IfObjArr.begin(); it != m_IfObjArr.end(); it++ )
{
DWORD dwFlag = SCRIPTITEM_ISVISIBLE;
if( (*it)->IsGlobal() ){ dwFlag |= SCRIPTITEM_GLOBALMEMBERS; }
if(m_Engine->AddNamedItem( (*it)->Name(), dwFlag ) != S_OK)
{
bAddNamedItemError = true;
Error(LS(STR_ERR_CWSH06));
break;
}
}
if( !bAddNamedItemError )
{
//マクロ停止スレッドの起動
SAbortMacroParam sThreadParam;
sThreadParam.pEngine = m_Engine;
sThreadParam.nCancelTimer = GetDllShareData().m_Common.m_sMacro.m_nMacroCancelTimer;
sThreadParam.view = (CEditView*)m_Data;
HANDLE hThread = NULL;
unsigned int nThreadId = 0;
if( 0 < sThreadParam.nCancelTimer ){
sThreadParam.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
hThread = (HANDLE)_beginthreadex( NULL, 0, AbortMacroProc, (LPVOID)&sThreadParam, 0, &nThreadId );
DEBUG_TRACE(L"Start AbortMacroProc 0x%08x\n", nThreadId);
}
//マクロ実行
if(m_Engine->SetScriptState(SCRIPTSTATE_STARTED) != S_OK)
Error(LS(STR_ERR_CWSH07));
else
{
HRESULT hr = Parser->ParseScriptText(AScript, 0, 0, 0, 0, 0, SCRIPTTEXT_ISVISIBLE, 0, 0);
if (hr == SCRIPT_E_REPORTED) {
/*
IActiveScriptSite->OnScriptErrorに通知済み。
中断メッセージが既に表示されてるはず。
*/
} else if(hr != S_OK) {
Error(LS(STR_ERR_CWSH08));
} else {
bRet = true;
}
}
if( 0 < sThreadParam.nCancelTimer ){
::SetEvent(sThreadParam.hEvent);
//マクロ停止スレッドの終了待ち
DEBUG_TRACE(L"Waiting for AbortMacroProc to finish\n");
::WaitForSingleObject(hThread, INFINITE);
::CloseHandle(hThread);
::CloseHandle(sThreadParam.hEvent);
}
}
}
Parser->Release();
}
m_Engine->Close();
return bRet;
}
void CWSHClient::Error(BSTR Description, BSTR Source)
{
if(m_OnError != NULL)
m_OnError(Description, Source, m_Data);
}
void CWSHClient::Error(const wchar_t* Description)
{
BSTR S = SysAllocString(L"WSH");
BSTR D = SysAllocString(Description);
Error(D, S);
SysFreeString(S);
SysFreeString(D);
}
//インタフェースオブジェクトの追加
void CWSHClient::AddInterfaceObject( CIfObj* obj )
{
if( !obj ) return;
m_IfObjArr.push_back( obj );
obj->m_Owner = this;
obj->AddRef();
}
/////////////////////////////////////////////
/*!
MacroCommand→CWSHIfObj.cppへ移動
CWSHMacroManager → CWSHManager.cppへ移動
*/