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.
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.
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());
}
}
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Internet Explorer 5.
For additional information, please see the following
article(s) in the Microsoft Knowledge Base:
184904
(http://support.microsoft.com/kb/184904/EN-US/
)
FILE: MSSCPCTL.EXE Script Control Header File MSSCPCTL.H
For additional information, please see the following World Wide Web URL:
Article ID: 184977 - Last Review: August 8, 2007 - Revision: 2.2
APPLIES TO
- Microsoft Visual Basic, Scripting Edition 3.0
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
| kbbug kbfix kbie500fix KB184977 |
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.