Preliminary exploration of vs2019 COM component – simple COM writing and cross language call

Time:2021-9-17

prerequisite

1. Master the basic syntax of C + +

2. Platform installation vs2019

3. The local platform is windows 10 1909 x64

4. Understand the basic syntax of vbs

 

This goal

1. Master the concept and principle of COM components

2. Write a simple COM component in the form of DLL

3. Cross language calling com through vbs

 

1. Concept and principle of COM component

  What is com:

    Baidu Encyclopedia: COM component is a new software development technology developed by Microsoft for the software production of computer industry to be more in line with human behavior. Under the com framework, people can develop a variety of functional components, and then combine them as needed to form a complex application system. The benefits are various: the components in the system can be replaced with new ones, so that the system can be upgraded and customized at any time; The same component can be reused in multiple application systems; It can easily expand the application system to the network environment; COM is independent of language and platform, so that all programmers can give full play to their intelligence and expertise to write component modules.

(personal understanding: COM component is a standard, and the components developed according to this standard are COM components. Similarly, any language can call this component as long as it complies with this standard. COM component is essentially a binary executable file, which is published by COM in the form of DLL or exe)

 

  What is ATL:

 

    Baidu Encyclopedia: ATL is the abbreviation of ActiveX template library. It is a set of C + + template library. Using ATL can quickly develop efficient and slim code, and provide maximum automatic code generation and visual support for the development of COM components. For ease of use, Microsoft has integrated ATL into the visual c + + development environment since Microsoft Visual C + + version 5.0. Visual Studio 6.0, launched in September 1998, integrates ATL version 3.0. ATL has become an important member of Microsoft standard development tools and has been paid more and more attention by C + + developers.

    (personal understanding: ATL is a C + + template released by Microsoft to simplify COM programming)

 

  What is ActiveX:

    Baidu Encyclopedia: ActiveX is Microsoft’s name for a series of strategic object-oriented program technologies and tools, of which the main technology is component object model (COM). In the network with directory and other support, com becomes distributed com (DCOM). ActiveX controls are small programs for the Internet, sometimes called plug-insThey can enhance your browsing experience by allowing you to play animations or help with tasks, such as installing security updates in Microsoft Update.  When creating ActiveX programs, the main work is components, a self-sufficient program that can run arbitrarily in the ActiveX network (windows, MAC, Linux). This component is an ActiveX control. ActiveX is proposed by Microsoft to compete with the Java technology of Sun Microsystems. The function of this control is similar to that of Java applet.

    (personal understanding: ActiveX is the name of COM component collection, and it is mainly used in the web, which is essentially com) it is referenced here   360 Encyclopedia   New entry   [vernacular is easy to understand]

 

2. Write a simple COM component

 

  Vs2019 create a new project based on ATL template next step

   

 

  Set the project name (Note: the project name must be remembered) and create it

   

 

 

   Set ATL project properties (based on. DLL mode)

  

  “Support component registration” here means that component registration will be performed automatically after compilation

Click OK after selection and wait for the creation to complete

  

  After completion, ComTest and comtestps will appear, regardless of PS

  

 

  

 

Click ComTest and you can see the header file and source file automatically generated by the system

   

 

  Now add a new ATL object

  Right click ComTest to create a new item – > Add – > create a new item

  

 

 

   Create a new “ATL simple object” for ComTest and click Add

  

 

   

  Set the short name to temp, and others will be filled automatically

Progid is set to project name + “. + short name (ComTest. Temp)

File type and other defaults are OK. Click finish

  Two values need to be noted here

Progid is the name to be provided by VBS and other language calling components

Interface itemp is the interface name that needs to be found after configuring IDL

  

 

   After completion, temp. H and temp.cpp will be automatically added under ComTest and a ctemp class will be generated

  

  

 

   Now we can create our own methods

  Add a function declaration to ctemp in the temp. H header file

  

 

 Implement this function in the temp.cpp file

  

It is worth mentioning that

__ Result must be a pointer for return

Return defaults to s_ OK indicates successful execution

 

  After defining the method, open comtest.idl

Remember the “short name” we set when creating ATL simple objects (itemp automatic naming rule is I + short name)

Find the interface itemp: IDispatch

  

 

   Write in interface itemp: IDispatch

  

[in]   Indicates input   [out] indicates output [retval] indicates to the outside world as a return value

It is worth mentioning that retval must have only one and must follow out

 

  

The above has written all the contents of the ComTest component

 

  Select the number of bits to generate x64 shortcut key Ctrl + B generate DLL

  

  

You will see the generated error message here

Never mind. Open our project directory

  

 

  You can see the first generated comtest.dll, and then register the COM component

  Notice two files here

Comtest.dll is the COM component ontology

Comtest.tlb is the file required for C + + to call com later

    

 

  Open CMD or PowerShell with administrator privileges

  And enter the directory where the DLL exists

  Enter the command to register our DLL

  

 

After entering, the message “succeeded” will appear

If unsuccessful, try to register with administrator privileges

  

 

3. Cross language calling com through vbs

 

   Write VBS code   Comtest.temp is the “progid” set for “ATL simple object” and number is the function name in the class

   

 

Save and run successfully

  

 

 

 

matters needing attention:

The com called by VBS must be IDispatch interface

Vs2019 ATL inherits IDispatch interface by default

COM components must be registered before VBS can call

 

  

GitHub source address

  3065190005/ComTest: ComTest Code (github.com)

 

The next article implements the CreateObject function logic of VBS and calls com through the IDispatch interface

  https://www.cnblogs.com/MF-Blog/p/14070878.html

 

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]