Microsoft KB Archive/184977

= FIX: ScriptControl Reports Invalid Language for VBScript in MFC =

Article ID: 184977

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Visual Basic, Scripting Edition 3.0
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition

-



This article was previously published under Q184977



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.
 * 1) 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 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.



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.
 * 1) 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.

<div class="references_section">