bug with named object

Mar 23, 2009 at 5:19 AM
Edited Mar 23, 2009 at 5:21 AM
My.vbs:

Function Add()
    Add = leftA & rightA
End Function


test:
            VBScript script = new VBScript();
            script.AddScript(GetScript("My.vbs"));
            ScriptLanguageBase s = script;
            s.AddObject("leftA", "a");
            s.AddObject("rightA", "b");
            script.Open();
            string result = script.ExecuteMethod("Add", new object[] { }) as string;
            script.Close();


exception: invalid procedure call or parameter.

  at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
  at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
  at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)




how ever, it works with integer:

My.vbs:

Function Add()
    Add = CInt(leftA) + CInt(rightA)
End Function


test:
            VBScript script = new VBScript();
            script.AddScript(GetScript("My.vbs"));
            ScriptLanguageBase s = script;
            s.AddObject("leftA", 4);
            s.AddObject("rightA", 5);
            script.Open();
            int result = Convert.ToInt32(script.ExecuteMethod("Add", new object[]{}));
            script.Close();



very strange, right?

Coordinator
Mar 24, 2009 at 12:38 AM
That is strange. Passing 'A' and 'B' works as well. The COM runtime is taking issue with a .NET string for some reason. Here is what I did to work around it. I created a class like this:

[System.Runtime.InteropServices.ComVisible(true)]
    public class StringHolder
    {
        private string _val = "";

        public StringHolder(string val)
        {
            _val = val;
        }

        public override string ToString()
        {
            return _val;
        }
    }

I then do this for the script:

s.AddObject("leftA", new StringHolder("A"));
s.AddObject("rightA", new StringHolder("B"));

That's the best I can come up with right now. I'm not sure why a string is an issue. I know the GetItemInfo is expecting an IUnknown to be returned.

Hope this helps.

-Eric
Mar 24, 2009 at 1:42 AM
hi,

      great thanks for you work around. it works now. but I think it is not so "generic", still hope that you could find out why and fix it without extra StringHolder :)