FIX: ScriptControl Reports Invalid Language for VBScript in MFC
ID: Q184977
|
The information in this article applies to:
-
Microsoft Visual Basic, Scripting Edition, version 3.0
-
Microsoft Visual C++, 32-bit Editions, version 5.0
SYMPTOMS
When hosting the ScriptControl object in a MFC application, you may receive
the following error:
The operation could not be completed because the script engine has not
been initialized to a valid language.
This occurs even though you previously set the language to VBScript using
the SetLanguage method. This error does not occur if you specify JScript or
JavaScript as the language for the ScriptControl Object.
CAUSE
When hosted as a control (and not just as a simple automation object), the
ScriptControl object fails to initialize itself properly if the specified
language is VBScript.
RESOLUTION
Here are two workarounds:
- Clear the language before setting it:
Add a call to ScriptControl::SetLanguage(NULL) right before calling
ScriptControl::SetLanguage("VBScript").
- Automating the ScriptControl object:
Alternatively, applications that use the ScriptControl can switch to
automating the Script Control just as an automation object rather than
as a full-blown control. This has the advantage of resolving the bug
indicated by this article as well as improving performance. The client
application does not need to go through the overhead of the ActiveX
control hosting negotiation, which is essentially unnecessary for the
non-UI Script Control.
Below is a piece of sample code that uses Visual C++ 5.0 COM support to
easily CoCreateInstance the ScriptControl and activate it.
Sample Code
// Import Type Info from Script OCX - point path to the
// location of the Script Control on your development computer.
#import "C:\Program Files\Microsoft Script Control\msscript.ocx"
void CScrptctrlView::OnInitialUpdate()
{
CView::OnInitialUpdate();
OleInitialize(NULL); // Only if you haven't done this already
// or called AfxOleInit.
using namespace MSScriptControl;
try // Make sure exception handling is turned on.
{
_bstr_t bstrLanguage(L"VBScript");
_bstr_t bstrCode(L"MsgBox \"Hello World\"");
// Create the Script Control, initialize Language, and
// add code for processing.
IScriptControlPtr spScriptCtl(__uuidof(ScriptControl));
spScriptCtl->put_Language(bstrLanguage);
spScriptCtl->AddCode(bstrCode);
}
catch(_com_error e)
{
TRACE(_T("Error (%08x) in %s: %s\n"), e.Error(),
e.Source(), e.Description());
}
}
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article. This bug was corrected in Microsoft Internet Explorer 5.
MORE INFORMATION
Steps to Reproduce Behavior
- Create a new MFC SDI application with the default AppWizard settings,
including support for ActiveX controls.
- Insert the ScriptControl by selecting "ScriptControl Object" from the
"Registered ActiveX Controls" folder under the Project\Add to
Project\Components and Controls menu item. Accept all defaults on the
dialogs that follow.
- In the OnInitialUpdate method of the view, insert the following code:
// Import Type Info from Script OCX - point path to the
// location of the Script Control on your development computer.
#import "C:\Program Files\Microsoft Script Control\msscript.ocx"
void CScrptctrlView::OnInitialUpdate()
{
CView::OnInitialUpdate();
OleInitialize(NULL); // Only if you haven't done this already
// or called AfxOleInit.
using namespace MSScriptControl;
try // Make sure exception handling is turned on.
{
_bstr_t bstrLanguage(L"VBScript");
_bstr_t bstrCode(L"MsgBox \"Hello World\"");
// Create the Script Control, initialize Language, and
// add code for processing.
IScriptControlPtr spScriptCtl(__uuidof(ScriptControl));
spScriptCtl->put_Language(bstrLanguage);
spScriptCtl->AddCode(bstrCode);
}
catch(_com_error e)
{
TRACE(_T("Error (%08x) in %s: %s\n"), e.Error(),
e.Source(), e.Description());
}
}
- Add a #include for the new script control class file, Scriptcontrol.h,
to the view and #define ID_SCRIPT_CONTROL in your Resource.h. Build and
run the application.
REFERENCES
For additional information, please see the following
article(s) in the Microsoft Knowledge Base:
Q184904 FILE: MSSCPCTL.EXE Script Control Header File MSSCPCTL.H
For additional information, please see the following World Wide Web URL:
http://msdn.microsoft.com/scripting/
Additional query words:
ScriptControl Script Control kbScript
Keywords : kbIE500fix
Version : WINDOWS:3.0; winnt:5.0
Platform : WINDOWS winnt
Issue type : kbbug