First create a COM component, insert a dual-interface Itest, and implement the following three methods on this interface:
STDMETHODIMPCtest::test(void)//No input and output parameters {//TODO: Add the implementation code MessageBox(NULL,L"test",L"test",MB_OK); returnS_OK;}STDMETHODIMPCtest::test1(BSTRa1)//There is a string input parameter {//TODO: Add the implementation code MessageBox(NULL, a1,L"test",MB_OK);returnS_OK;}STDMETHODIMPCtest::test3(BSTR*a1)//There is a BSTR* output parameter {//TODO: Add the implementation code here MessageBox(NULL,L"test3",L"test",MB_OK);*a1=::SysAllocString(L"Fenghuo three months of family letters are worth ten thousand gold");returnS_OK;}The above three methods in COM demonstrate three situations: no input and output parameters, one input parameter, and one output parameter. After the program is compiled, use regsvr32 to register the component in the system, and then you can use com component method in IE with javascript. There are two situations to use, the examples are as follows:
1. Directly call the com method
Create an html text and enter the following:
<html> <head> <title> Example of method to call com component</title> <script language="javascript"> document.write("<hr>") var xml=new ActiveXObject("atldll.test.1") xml.test() xml.test1("Pass parameters to the com method and call com method") var str=xml.test3() //Return parameters from com method document.write("str"+str) document.write("<hr>") </script> </head> <body> Demonstrate the method to call com component in IE script program</body> </html>2. Call the com method in the javascript function
Create an html text and enter the following:
<html> <head> <title> donghailin active object </title> <script type="text/javascript"> function displaymessage() { document.write("<hr>") var xml=new ActiveXObject("atldll.test.1") xml.test() xml.test1("Passing a string to the com component") var str=xml.test3() //Return the string from the com component document.write("str"+str) document.write("<hr>") } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" > </form> </body> </html>Note that the com component written is for other system calls, so the input and output parameter string should be of type BSTR.
3. Methods to set properties of com component
First set properties in the com component. Add attribute variables in interface class
BSTR m_bstr;
Right-click the interface and select "Giveaway" -> "Add attributes" Enter the parameter type "BSTR" in the "Input attribute type" in the "Add attribute wizard, enter "bstr" in "Attribute name" and enter "bstr". The wizard automatically generates the attribute functions put_bstr and get_bstr, as follows:
STDMETHODIMP Ctest::get_bstr(BSTR* pVal) { // TODO: Add the implementation code here *pVal=m_bstr; return S_OK; } STDMETHODIMP Ctest::put_bstr(BSTR newVal) { // TODO: Add the implementation code here m_bstr=newVal; MessageBox(NULL,m_bstr,L"attribute test",MB_OK); return S_OK; }The following code demonstrates setting and obtaining properties in IE's javascript script
<html> <head> <title> donghailin active object </title> <script type="text/javascript"> function displaymessage() { document.write("<hr>") var xml=new ActiveXObject("atldll.test.1") xml.bstr="The spring scenery in the garden cannot lock a red apricot out of the wall" //The property is bstr, the property setting in C++ is put_bstr(...) document.write("The property return value is: "+xml.bstr) //The property value is obtained in c++ get_bstr(variable pointer) document.write("<hr>") } </script> </head> <body> <table align=center width=50> <form> <input type="button" value="Click me!" onclick="displaymessage()" > </form> </table> </body> </html>The above simple implementation method of using com components in JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.