Generally, you can use cocreateinstance to call the COM component. However, for the COM component that implements the idispatch interface, it is easier to call the COM component by using the coledispatchdriver package class of the VC ++ automatically generated interface. Example: execute script statements in VC ++, such as VBScript statements. The system provides a control: c: \ windows \ system32 \ msscript. ocx, which provides an interface called iscriptcontrol, through which we can execute script statements.
1. Use VC ++ to automatically create a packaging class
Use VC ++ to create a project that supports MFC, add a class, select "MFC class in the Type Library", and then select msscript. ocx file, and add iscriptcontrol to the right bar, such:
Click Finish to generate the cscriptcontrol packaging class.2. Use the generated class
// Initialize the com library coinitialize (null); // create a msscriptcontrol. scriptcontrol instance // This name (progid) can be obtained through the tool oleview in the VC ++ directory. Cscriptcontrol JS;If(Js. createDispatch ("MSScriptControl. scriptControl ") {// sets the currently used scripting language js. put_Language ("JScript"); // execution statement. After execution, var contains the expression result.VARIANTVar = js. Eval ("1.234 + 5.31"); // release the js. ReleaseDispatch ();} // close the COM library CoUninitialize ();3. Other functions of this example
This code can be easily encapsulated into an expression calculator.