How to create an ActiveX object in C ++ that can be created with Javascript

I can use VS08 MFC / ActiveX template to create a C ++ ActiveX object that can be loaded into an HTML page and script using Javascript. But I can't figure out how to create an interface that allows me to call custom methods on my component using Javascript.

Could you please tell me how to do this? I spent over two hours on Google with no luck.

Thank.

0


source to share


3 answers


I'm not very familiar with the MFC ActiveX wrapper, but I can answer the question in a general sense:

A COM object with an interface derived from IDispatch can be called through automation languages ​​(such as Javascript). These methods must also be "automatic compatible", which means that the parameters are converted to VARIANT or explicitly VARIANT. Note that for I / O parameters, the type must be VARIANT * in order to automate the "connection" to work.



I don't know how to make the ActiveX object available in the client script (ex: insert it into the page), but if it has a single interface derived from IDispatch, that makes it callable from Javascript (and other automation languages). I hope this helps ...

+2


source


This works for embedding your ActiveX container in the html page and calling the method:



<html> 
<body> 
<object height="0" width="0" id="myControl" classid="CLSID:AC12D6F8-AEB7-4935-B3C9-0E4FB6CF7FB1" type="application/x-oleobject">
</object>
<script>
    var activexObj = document.getElementById('myControl');
    if(activexObj != null)
    {
        var result = myControl.myMethod();
        document.write("Result: " + result + "<br/>");
    }
    else
    {
        document.write("ActiveX component not found!<br/>");
    }
</script>
</body>
</html>

      

+1


source


If you are using the VS08 MFC ActiveX template, you can see this snippet in the .h control file (in the class declaration, it is protected):

afx_msg void AboutBox();

DECLARE_DISPATCH_MAP()

      

And one of them in the .cpp file:

// Dispatch map

BEGIN_DISPATCH_MAP(CActiveXOutlookCtrl, COleControl)
    DISP_FUNCTION_ID(yourCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()

      

I've never had to use this, but it's your dispatch interface, known as methods on your object that others can call. What does it mean:

  • "AboutBox" is the name they use to call it.
  • DISPID_ABOUTBOX is the integer identifier for the function (I think this is arbitrary. I would use a positive number because quite a few negatives were accepted by default).
  • AboutBox is the name of the method.
  • VT_EMPTY is the return type of the method.
  • VTS_NONE is the type of parameters it takes.

There's also DECLARE_MESSAGE_MAP () and DECLARE_EVENT_MAP (), although that might be what you want.

+1


source







All Articles