'IActiveScriptSite treats \\02 as control character when passesd as string parameter of a function while using JScript
The problem I have is that IActiveScriptParse seems to behave differently in regards to string parameters of a method which shall be called via automation depending if VBScript or JScript is beeing used.
The script call is in both cases only
app.Search("tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02")
In VBScript the parameter will be transmitted properly to my Search function. Whereas JScript will treat the \02 as control character, in this case STX, and thus I will not be able to find the proper control which was meant to be found.
I've created a simple MFC project with automation support following https://www.codeproject.com/Articles/8202/Adding-automation-to-MFC-applications therefore I added an Application class which shall implement a very simple interface described in the idl:
interface IApplication : IDispatch
{
[id(1), helpstring("method Search")] LPDISPATCH Search(LPCTSTR Id);
};
Mapped it to "SearchInternal"
BEGIN_DISPATCH_MAP(CApplication, CCmdTarget)
DISP_FUNCTION_ID(CApplication, "Search", dispidSearch, SearchInternal, VT_DISPATCH, VTS_BSTR VTS_VARIANT)
END_DISPATCH_MAP()
LPDISPATCH CApplication::SearchInternal(LPCTSTR Id)
{
CString title;
CString output = _T("Passed Id was: \n");
output.Append(g_strIdBeforeCall);
output.AppendFormat(_T("\nreceived parameter Id is \n%s"), Id);
if (g_calledByVBS)
title = (_T("VBScriptParse"));
else
title = (_T("JScriptParse"));
MessageBox(NULL, output, title, MB_OK);
return NULL;
}
Now I try to call my method via JScript and VBScript by implementing an IActiveScriptSite. Basically like https://github.com/stephenquan/RunScriptDemo from a different stackoverflow question.
For testing purposes the script calls are initiated on a Button Click, but the behaviour is the same when it actually comes from a dropped .vbs / .jscript file.
void ChildViewButton::runScript()
{
//get application object
CApplication* pApp = new CApplication();
CSimpleScriptSite* pScriptSite = new CSimpleScriptSite();
pScriptSite->m_pUnkSessionScriptObject = pApp->GetIDispatch(FALSE);
// Initialize JScript
CComPtr<IActiveScript> spJScript;
CComPtr<IActiveScriptParse> spJScriptParse;
HRESULT hr = spJScript.CoCreateInstance(OLESTR("JScript"));
hr = spJScript->SetScriptSite(pScriptSite);
spJScript->AddNamedItem(L"app", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE | SCRIPTITEM_GLOBALMEMBERS);
hr = spJScript->QueryInterface(&spJScriptParse);
hr = spJScriptParse->InitNew();
//Initialize VBscript
CComPtr<IActiveScript> spVBScript;
CComPtr<IActiveScriptParse> spVBScriptParse;
hr = spVBScript.CoCreateInstance(OLESTR("VBScript"));
hr = spVBScript->SetScriptSite(pScriptSite);
spVBScript->AddNamedItem(L"app", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE | SCRIPTITEM_GLOBALMEMBERS);
hr = spVBScript->QueryInterface(&spVBScriptParse);
hr = spVBScriptParse->InitNew();
// Run some scripts
g_strIdBeforeCall = _T("app.Search(\"tabsTAXI_TABSTRIP_OVERVIEW/tabpT\\02\")");
_bstr_t strCodeBstr = bstr_t(g_strIdBeforeCall);
//check CApplication::SearchInternal
g_calledByVBS = true;
testScript(L"VBScript", spVBScriptParse, strCodeBstr);
g_calledByVBS = false;
testScript(L"JScript", spJScriptParse, strCodeBstr);
// Cleanup
spVBScriptParse = NULL;
spVBScript = NULL;
spJScriptParse = NULL;
spJScript = NULL;
pScriptSite->Release();
pScriptSite = NULL;
}
With TestScript() beeing:
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);
}
Results in:
JScript treats \02 as control character \x2 == STX Do you have any suggestions if I might miss something here or is this an API Bug? Unfortunately we cannot change our indexing system \02, \03, \04... and I also do not know how to solve this problem once I reached SearchInternal() as there could by any number of calls to Search in one script with different indexes in the same format.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


