Results 1 to 11 of 11

Thread: JavaScript/C++: Passing a JavaScript String to C++ ActiveX Control

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    7

    JavaScript/C++: Passing a JavaScript String to C++ ActiveX Control

    Hey everyone, I'm writing an ActiveX control to embed inside an Internet Explorer page, and I'm having trouble passing a string to an ActiveX method from inside JavaScript.

    On the ActiveX side, I'm using C++ to compile and register an OCX, and the syntax for my method looks like this:

    VARIANT_BOOL ProcessText(char * TextString)

    On the JavaScript side, I call the method like this:

    <OBJECT id="TextControl" classid="clsid:F73A5DFD-1117-4485-B486-8ECDF0638736" VIEWASTEXT>
    <input name="btnProcess" type="button" value="Process" onclick="document.TextControl.ProcessText("Textblahblah");">

    Clicking the Process button gives me a "Type Mismatch" error.

    So what do I have to do to make this call work? The ActiveX function works fine if I call it from the ActiveX Test Container. It seems to be some kind of conversion problem between JavaScript and C++.

    Thank you for any help.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The parameter should be a BSTR.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    7
    Okay, I've tried both

    VARIANT_BOOL ProcessText(BSTR TextString)

    and

    VARIANT_BOOL ProcessText(BSTR * TextString)

    I still get a "Type Mismatch" from Internet Explorer with both.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    7

    Update

    A forum member suggested moving the JavaScript call out from the button onclick to a stand-alone JavaScript function like this:

    function passmystring(myString)
    {
    TextControl.ProcessText(myString);
    }

    But, I still get a Type Mismatch inside the JavaScript function when it calls the ActiveX function.

    I double-checked everything inside the debugger, and I've confirmed that I'm passing a proper string for myString.

    Any other ideas on what's going wrong here?

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I have written a lot of C but no C++. And I have written Java applets that receive Strings from JavaScript routines. So, I tried Googling for sites with Java<->C++ data types looking for the C++ equivalent of a Java String.

    I found this: http://www.codemesh.com/en/faqProgrammingStrings.html, but I don't know if it will be helpful to you or not.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This is about JavaScript (specifically, JScript), not Java. The two technologies are not related.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by CornedBee
    This is about JavaScript (specifically, JScript), not Java. The two technologies are not related.
    I am quite aware of that.

    Apparently it wasn't clear that, knowing that JavaScript strings are compatible with Java Strings, I was trying to determine the C++ equivalent of a Java String.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm not so sure about that. Are JavaScript strings actually required by the ECMAScript standard to be of a particular form? JavaScript has a weird object model, I'm not sure if it's compatible with anything.

    Anyway, Java strings are 16-bit UNICODE strings, I don't know if they're 0-terminated or where they store their length. The JNI functions do some sort of conversion.

    BSTR, the ActiveX string type, is 16-bit UNICODE characters too, 0-terminated and with a 2-byte length just before the first character, but with the pointer (that BSTR is a typedef for) pointing not to the beginnning of the object (the length) but to the first character, thus the length has a negative offset!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Since JavaScript variables are not typed, I'm guessing that they are all varients. And since I am able to pass string variables from my JavaScript routines to methods in my Java applets, it would appear that Java has no promlem converting them while the same cannot be said for C++. Does C++ have a varient data type?

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Variants, well...

    C++ has of course no native variant datatype. There's a lot of management to a variant, nothing that would belong into C++.

    But of course there's VARIANT, from the WinAPI. It's the underlying implementation of the ActiveX and VB variant type.

    A question about the error, when does it occur? No, wait, that's described.

    Try the variant type. It might work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    New Member
    Join Date
    Mar 2005
    Posts
    1

    Smile Re: JavaScript/C++: Passing a JavaScript String to C++ ActiveX Control

    Making the variable a LPCTSTR type will work. I have done that.
    I have a problem of passing in an array of strings. I have made the parameter a BSTR, VARIANT FAR*. Can anybody help?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width