Pergunta

I have a COM Object created using ATL (without MFC Support)

The Object has 1 method that opens a Dialog (that does all the rest)

Currently I call it from another EXE:

hr = CoCreateInstance(
    CLSID_MyControl,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IMyControl,
    (void**) &pMyControl
    );

and then:

hr = pMyControl->MyMethod (ATL::CComBSTR(InputString1), ATL::CComBSTR(InputString2), &IntReturned, &IntReturned);

Is it possible to call it as is from a browser ?

How can I Instantiate the object and invoke my method (with params) from the browser ?

Foi útil?

Solução

Some points to answer your question:

  • You won't be able to use a COM object in any browser other than IE or a WebBrowser-based app.

  • You'd need to implement IObjectSafety interface to allow IE to create your object. Naturally, the object should be safe for scripting by any untrusted source. Ideally, you should lock the object to your own list of sites. You could use SiteLock template for this.

  • The object should implement IDispatch interface, to be available for scripting. The best way is to use ATL's IDispatchImpl (most likely, it's already done in your code).

  • The MyMethod in your sample uses two [out] arguments for IntReturned. JavaScript only allows one output [out, retval] argument. If you need to return more than one value, you'd have to use VBScript.

Example (substitute your CLSID):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
    <title></title>
    <script type="text/vbscript">
        Option Explicit
        window.onload = GetRef("OnLoadHandler")

        Sub OnLoadHandler
            Dim InputString1
            Dim InputString2
            Dim IntReturned1
            Dim IntReturned2

            InputString1 = "a"  
            InputString1 = "b"  
            testObject.MyMethod InputString1, InputString2, IntReturned1, IntReturned

            alert "Result: " & IntReturned1 & ", " & IntReturned
        End Sub
    </script>
</head>

<body>
    <object id="testObject" classid="clsid:12345678-1234-1234-1234-1234567890AB">
        <span>Unable to create the object.</span>
    </object>
</body>
</html>

If you don't implement IObjectSafety, you can still run this code as HTML Application. Save it as an .HTA file, and run as C:\Windows\SysWOW64\mshta.exe C:\users\user\Documents\test.hta if your C++ COM DLL is 32-bit, or as C:\Windows\System32\mshta.exe C:\users\user\Documents\test.hta if it is 64-bit.

The COM DLL needs to be registered first with regsvr32.exe (you've probably already done this if you can use it from a C++ client project).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top