10

I am working with an ActiveX control in Internet Explorer 8 that is to display a save file dialog which let's the user choose a file name and file type (jpg, gif, etc). These values get passed to code and then are used in a different method to save the file. Unfortunately the method that invokes the dialog has no return value, and the file name and file type are passed in as out parameters.

The signature of the method (expressed in Visual Basic) looks like this:

Public Sub SaveFileDialog( _
   ByVal bstrDialogType As Variant, _
   ByRef pbstrFileName As String, _
   ByRef out_pvType As Long _
)

The two ByRef parameters are the out parameters.

I have written the following JavaScript code:

try
{
    var saveFileName, saveFileType; // out variables
    gxVideoPlayBack.SaveFileDialog("image", saveFileName, saveFileType);
    alert(saveFileName); // displays "undefined"
    alert(saveFileType); // displays "undefined"
}
catch(error)
{
    if(!error.number === -2147221484) // User clicked cancel.
    {  
        alert(error.message);
    }
}

The code works in that the ActiveX control produces its dialog, and I can handle error conditions, but I can't seem to figure out how to capture the values of the out parameters.

In the code gxVideoPlayBack is a reference to the ActiveX control embedded in the DOM via an HTML element.

If JavaScript will not work for this, can it be done in VBScript?

As an alternative I can just implement my own dialog, but would rather use the one provided.

RunnerRick
  • 949
  • 2
  • 12
  • 21

3 Answers3

7

Edit: It seems that it's not possible to have "out" parameters in JavaScript/JScript.

Original: Perhaps the approach described in this article will work:

var saveFileName={}, saveFileType={}; // Empty "output" objects.
gxVideoPlayBack.SaveFileDialog("image", saveFileName, saveFileType);
alert(saveFileName.value); // The "value" attribute is assigned ...
alert(saveFileType.value); // ... by the "SaveFileDialog" method?

I suppose the idea is that the WSH wrapper for this native call will attempt to assign the "value" property of the given output parameters, so you can either override the value setter or just give it an object with a built-in value setter.

maerics
  • 151,642
  • 46
  • 269
  • 291
  • That's how XPConnect connect bridges the ref gap between JavaScript and XPCOM, but I have no idea if ActiveX does something similar. – Wayne Feb 25 '11 at 22:05
  • @lwburk: yeah, same here, just throwing out an idea. I think the reality is that "out" parameters are not available in JScript/JavaScript. – maerics Feb 25 '11 at 22:08
  • 1
    Interesting concept. Unfortunately when I initialize the two variables to an empty object the *SaveFileDialog* throws a type exception. It's expecting a string and an integer. – RunnerRick Feb 25 '11 at 22:55
  • My conclusion is that it isn't possible to leverage "out" parameters in an ActiveX method from JavaScript, but this is the best answer. – RunnerRick Feb 26 '11 at 01:10
1

All function arguments in JavaScript are passed by value (even if the value being passed is a reference to an object (which it is)). There is no pass-by-reference.

If SaveFileDialog modifies the objects referenced by saveFileName and saveFileType then you have access to those changes through your existing variables.

Community
  • 1
  • 1
Wayne
  • 59,728
  • 15
  • 131
  • 126
0

Unfortunately, out/ByRef parameters will only work in JScript for objects; not for any other type (numbers, strings).

In this case, you’ll have to use VBScript, which does support ByRef arguments, or like maerics says, write a VB/VBScript wrapper for the SaveFileDialog method, which could return an object containing both file name and type.

Community
  • 1
  • 1
Martijn
  • 13,225
  • 3
  • 48
  • 58