-
Notifications
You must be signed in to change notification settings - Fork 4
/
RunScriptDemo.cpp
69 lines (61 loc) · 2.33 KB
/
RunScriptDemo.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
// RunScriptDemo.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include "SimpleScriptSite.h"
#include <iostream>
#include <stdio.h>
void testExpression(const wchar_t* prefix, IActiveScriptParse* pScriptParse, LPCOLESTR script)
{
wprintf(L"%s: %s: ", prefix, script);
HRESULT hr = S_OK;
CComVariant result;
EXCEPINFO ei = { };
hr = pScriptParse->ParseScriptText(script, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &result, &ei);
CComVariant resultBSTR;
VariantChangeType(&resultBSTR, &result, 0, VT_BSTR);
wprintf(L"%s\n", V_BSTR(&resultBSTR));
}
void testScript(const wchar_t* prefix, IActiveScriptParse* pScriptParse, LPCOLESTR script)
{
wprintf(L"%s: %s\n", prefix, script);
HRESULT hr = S_OK;
CComVariant result;
EXCEPINFO ei = { };
hr = pScriptParse->ParseScriptText(script, NULL, NULL, NULL, 0, 0, 0, &result, &ei);
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Initialize
CSimpleScriptSite* pScriptSite = new CSimpleScriptSite();
CComPtr<IActiveScript> spJScript;
CComPtr<IActiveScriptParse> spJScriptParse;
hr = spJScript.CoCreateInstance(OLESTR("JScript"));
hr = spJScript->SetScriptSite(pScriptSite);
hr = spJScript->QueryInterface(&spJScriptParse);
hr = spJScriptParse->InitNew();
CComPtr<IActiveScript> spVBScript;
CComPtr<IActiveScriptParse> spVBScriptParse;
hr = spVBScript.CoCreateInstance(OLESTR("VBScript"));
hr = spVBScript->SetScriptSite(pScriptSite);
hr = spVBScript->QueryInterface(&spVBScriptParse);
hr = spVBScriptParse->InitNew();
// Run some scripts
testExpression(L"JScript", spJScriptParse, OLESTR("(new Date).getTime()"));
testExpression(L"VBScript", spVBScriptParse, OLESTR("Now"));
testScript(L"VBScript", spVBScriptParse, OLESTR("MsgBox \"Hello World! The current time is: \" & Now"));
// Outputs:
// JScript: (new Date).getTime() : 1554685325378
// VScript: Now : 8 / 04 / 2019 11 : 02 : 05 AM
// VBScript: MsgBox "Hello World! The current time is: " & Now
// Cleanup
spVBScriptParse = NULL;
spVBScript = NULL;
spJScriptParse = NULL;
spJScript = NULL;
pScriptSite->Release();
pScriptSite = NULL;
::CoUninitialize();
return 0;
}