First create a COM component, insert a dual interface ITest, and implement the following three methods on this interface:
STDMETHODIMP ctest::test (void) //No input/output parameter { //TODO: Add implementation code in this MessageBox (null,l "test", L "test", MB_OK); return S_OK;} STDMETHODIMP Ctest::test1 (BSTR A1) //have a string input parameter { //TODO: Add implementation code in this MessageBox (null,a1,l "test", MB_OK); return S_OK;} STDMETHODIMP Ctest::test3 (bstr* A1) //have a bstr* output parameter { //TODO: Add implementation code in this MessageBox (null,l "test3", L "test ", MB_OK); *a1=::sysallocstring (L "Beacon to million gold" in March); return S_OK;}
The above three methods in COM demonstrate that there are no input and output parameters, one input parameter, and one output parameter of three cases. After the program is compiled, use regsvr32 to register the components in the system, and then you can use JavaScript in IE to call the COM component of the method, in two cases are dropped, the example is as follows:
1. Calling COM methods directly
Create an HTML text and enter the following:
2. Calling COM methods in JavaScript functions
Create an HTML text and enter the following:
Note that a COM component is written for other system calls, so the input-output parameter string should be a BSTR type.
3. Methods for setting properties of COM components
First set the properties in the COM component. Adding attribute variables to the interface class
BSTR M_bstr;
Right-click on the interface, select "Add"-〉 "adding Properties" in the Add Property Wizard, enter the parameter type "BSTR" in the Input property type, "property name" Input "BSTR", the wizard automatically generates the property functions Put_bstr and Get_bstr, as follows:
STDMETHODIMP Ctest::get_bstr (bstr* pVal) {//TODO: Add implementation code *PVAL=M_BSTR; return S_OK;} STDMETHODIMP CTest::p ut_bstr (BSTR newval) {//TODO: Add implementation code m_bstr=newval here; MessageBox (null,m_bstr,l "attribute Test", MB_OK); return S_OK;}
The following code demonstrates setting and getting properties in the JavaScript script of IE