Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime parameter #141

Closed
fvi1234 opened this issue Nov 7, 2019 · 3 comments
Closed

DateTime parameter #141

fvi1234 opened this issue Nov 7, 2019 · 3 comments
Assignees

Comments

@fvi1234
Copy link

fvi1234 commented Nov 7, 2019

Hello, thanks for this project, apart from MSScriptControl (where I can't get callbacks to work) it seems to be the only currently maintained way left to run VBScript from C# (correct?). I can't use Core thought because it does not include Microsoft.ClearScript.Windows which has the VBScriptEngine, so I have a Framework based project.

I've tested and everything seems to work execpt an issue passing a DateTime parameter. It does not seem to get converted into a (VBS) Date object, but has the type 'Object' in VBS. I know a workaround would be to convert to string and then back to date again (but would need to change a lot of existing scripts). But this is unexpected and wonder if this is a bug, especially since a conversion from (VBS) Date to (C#) does seem to be going on.

Is it a bug in ClearScript? Is it undefined behaviour? Am I doing something wrong? (impossible right?)

This is code from my test case including remarks:

private static void TestDateTimeParameter(VBScriptEngine engine)
{
    var scriptText = @"
        Public Sub TestDateTimeParameter(myDateParam)

            Rem outputs 'False'
            StandardOutput.Echo CStr(IsDate(myDateParam))

            Rem outputs 'Object'
            StandardOutput.Echo TypeName(myDateParam)

            Rem outputs 'True' and 'Date'
            myDateLocal = Now()
            StandardOutput.Echo CStr(IsDate(myDateLocal))
            StandardOutput.Echo TypeName(myDateLocal)

            Rem this would give a 'Type mismatch' error
            Rem foobar = CStr(myDateParam)
            Rem this also
            Rem foobar = CDate(myDateParam)

            Rem you can pass the object back to c#
            StandardOutput.EchoDate(myDateParam)
            StandardOutput.EchoDateObject(myDateParam)

            Rem there is conversion from a Date (VBS) -> DateTime (C#)
            StandardOutput.EchoDate(myDateLocal)

            Rem but there seems no conversion going from DateTime (C#) -> Date (VBS)
        End Sub";
    try
    {
        engine.AddHostObject("StandardOutput", new StandardOutput());

        engine.Execute(scriptText);

        engine.Invoke("TestDateTimeParameter", DateTime.Now);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

public class StandardOutput
{
    public void Echo(string value)
    {
        Console.WriteLine(value);
    }

    public void EchoDate(DateTime date)
    {
        Console.WriteLine(date.ToString("yyyy/MM/dd HH:mm:ss"));
    }

    public void EchoDateObject(object obj)
    {
        if (obj is DateTime)
            EchoDate((DateTime)obj);
    }
}

edit: completely screwed up the formatting, corrected

@ClearScriptLib
Copy link
Collaborator

Hi @fvi1234,

ClearScript generally prefers full access over automatic conversion. That is, it aims to expose to script code the full capabilities of .NET data types, and vice versa.

On the other hand, some data types (numbers, strings, etc.) are so fundamental that forcing manual conversion would have made ClearScript difficult to use.

Timestamps are a bit of a gray area. They're quite fundamental, but their representation varies to the extent that automatic conversion can mean data loss. Nevertheless, we've received a number of requests for automatic conversion from V8 users, so we added it as a V8-only option.

For VBScript's Date, the automatic conversion to DateTime is provided by the .NET runtime and is not under ClearScript's control. There's currently no automatic conversion going the other way, but we'll add it as an option in the next ClearScript release.

In the meantime, you can use .NET's ToOADate in conjunction with VBScript's CDate to perform the conversion manually.

Good luck!

@fvi1234
Copy link
Author

fvi1234 commented Nov 7, 2019

Hi,
thanks for the explanation, makes sense. ToOADate would indeed provide a workaround, but I would need to change all the existing VBScripts (which are stored in customer's databases)

There's currently no automatic conversion going the other way, but we'll add it as an option in the next ClearScript release.

That would be very helpful, thanks very much.

Thanks

ClearScriptLib added a commit that referenced this issue Dec 18, 2019
…ws (GitHub Issue #9); added support for system documents (GitHub Issue #143); added IList implementation to V8 arrays; added ExtendedHostFunctions.typeLibEnums (GitHub Issue #147); added WindowsScriptEngineFlags.MarshalDateTimeAsDate (GitHub Issue #141); fixed generic type inference for COM interfaces; fixed enumeration of COM collections that don't provide class information (GitHub Issue #146); fixed memory leak impacting Windows Script object property assignment (GitHub Issue #135); updated build and API documentation. Tested with V8 7.9.317.32.
@ClearScriptLib
Copy link
Collaborator

ClearScript 6.0 added WindowsScriptEngineFlags.MarshalDateTimeAsDate to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants