Search code examples
c#vbscriptclearscript

Clearscript vbscript com ado interaction


I'm using clearscript as a vbscript execution engine.

I have exposed the following C# object (I appreciate it doesn't actually execute a query, I'm just testing for now):

public class SCB
{
        public ADODB.Recordset executeQuery(string q)
        {
            ADODB.Recordset _recordset = new ADODB.Recordset();

            _recordset.Fields.Append("Id", ADODB.DataTypeEnum.adInteger);
            _recordset.Fields.Append("Name", ADODB.DataTypeEnum.adVarChar, 20);
            _recordset.Open(System.Reflection.Missing.Value
                    , System.Reflection.Missing.Value
                    , ADODB.CursorTypeEnum.adOpenStatic
                    , ADODB.LockTypeEnum.adLockOptimistic, 0);
            _recordset.AddNew(Type.Missing, Type.Missing);
            _recordset.Fields["Name"].Value = "Test";

            return _recordset;
        }
    }
}

I have created a vbscript host using:

_engine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging);

And added the class as an object with this line:

_engine.AddHostObject("SCB", HostItemFlags.GlobalMembers, new SCB());

If I run some vbscript code via the following call I receive an error (described below):

_engine.ExecuteCommand(code);

The code I am executing is the following:

Function GetUniversalMessage
    dim lRS, sMessage, sColour,sTimeout
    Set Lrs = SCB. executeQuery ("SELECT MESSAGE, TIMEOUT, COLOUR, ENABLED FROM U_SCROLLER WHERE SCROLLER_ID=1 AND ENABLED='Y' AND (DISABLE_TIME IS NULL OR DISABLE_TIME>GETDATE())")
    if not lRS.EOF then
    End If
End Function

I receive an exception that lrs.eof is not a valid field ... but it is valid for an ado com object, and if I check the object created in executeQuery before it returns to the script engine, the EOF field is present. When I debug by attaching the debugger and calling the stop command, I can see that the lrs object in vbscript does not contain EOF, or most of the valid Recordset fields.

Can someone assist me in explaining what clearscript is doing to the object before it is inserted into the vbscript engine?

Thanks,

Rob


Solution

  • Check whether you're using embedded Interop types. That feature is problematic for scripting because the embedded types are stripped of any members you don't use in managed code.

    For example, if you don't use EOF in managed code, the metadata for the EOF property will be left out of the embedded version of Recordset.

    In Visual Studio, try setting the Embed Interop Types property of your ADODB reference to False.

    Another thing to try would be to change the executeQuery return type to object, or add the attribute [ScriptMember(ScriptMemberFlags.ExposeRuntimeType)].