In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to learn C++ com programming, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Introduction to COM
COM, which is full component object model, is a binary standard that can be used to call dll modules across languages or to achieve componentization and reuse. Com can be used not only in a single operating system but also across services, and you can see it in many large software such as wps,office.
For example, the call specification in java is as follows:
JAVA COM programming
You may have a lack of dll on your computer. One way to fix it is to download dll and then call regsvr32.exe xxx.dll.
Above is the registration of the COM component, which is essentially registering this dll information in the registry so that other system software can load it.
Flutter also provides related interfaces to encapsulate links to flutter-related documents.
This paper mainly introduces the use of com specification programming under C++.
Why do I need COM? Just to invoke dll, why refer to such a complex concept?
1. Suppose an exe upgrade and one of the dll wants to publish only the dll instead of the body file, in most cases there is no problem. However, the main exe and dll compiled by different compilers (or different versions of the same compiler) may be caused by differences in memory layout. A classical problem on startoverflow
two。 Cross-language calls, such as c, end with\ 0, but this is not the case for all language string definitions.
3. Scheduling dll functions across processes or services
4.dll code reuse and sharing
COM specification
Com uses idl files to define dll functions or interfaces, and then uses the midl compiler to produce the corresponding header files, which developers use to implement the interfaces.
The interface has its own identifier IID to prevent name conflicts with other people's interfaces.
For one of the compiled header files, you will see that the IID_XXXXX looks like this
If IID is to identify an interface, then there should be an ID to identify the implementation class. The id of this implementation class, which we call CLSID,CLSID, will map a dll message in the registry, that is, we can use this CLSID to find the dll file information in the registry.
Tip: an implementation class may contain multiple interfaces
More idl syntax can be found in the official guide:
Https://docs.microsoft.com/en-us/windows/win32/com/defining-com-interfaces
Https://bbs.csdn.net/topics/30094944?list=34484
Write a com shared dll library using ATL to run vs with administrator privileges (compiling dll automatically invokes regsvr32 to register in the registry, but requires permissions)
First create an ATL project, after which you will see an idl file
Create a new API as follows:
The above ProgId is optional and its function is to provide another way to find registered dll.
After completion, our IDL will automatically generate the relevant syntax into the file.
At the same time, the corresponding header file and c file are created as follows
At this point we add an interface method to the class view
After being added, idl will also produce the corresponding syntax as shown in the following figure.
The corresponding c file implements the interface by itself (the last parameter is used as the return parameter)
The project name _ i.c and project name .h files are generated after compilation, and the dll is automatically registered in the registry.
Copy the appeal two files to other usage projects (note that we did not copy dll) as shown below:
Then adjust the code to call as follows:
# include # include "FMYALTFOUR_i.h" int main () {/ / initialize CoInitialize (NULL); IClassFactory * pFactory = NULL / / find the dll location from the registry through CLSID, load and return a class factory HRESULT hr = CoGetClassObject (CLSID_IfmyMathHelper,CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (void**) & pFactory); / / use the class factory to get an interface instantiation object IIfmyMathHelper * pSuperMath = NULL PFactory- > CreateInstance (NULL, IID_IIfmyMathHelper, (void**) & pSuperMath); long ret; pSuperMath- > add (1,2, & ret); / / Deinitialize CoUninitialize ();}
Of course, this is one of the invocation methods, and the other is reserved for language calls such as vb. In this way, you don't need to copy the two appeal files, but you must check the interface double to create the interface.
Int main () {/ / initialize CoInitialize (NULL); HRESULT hr; GUID clsid; IUnknown FAR* punk; IDispatch FAR* pdisp = (IDispatch FAR*) NULL; / / find clsid reverse through progId to load dll hr = CLSIDFromProgID ("progIdfmyMathHelper.1"), & clsid; IDispatch* pDispatch = NULL Hr= CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch, (void**) & pDispatch); LPOLESTR szMember [1] = {(LPOLESTR) OLESTR ("add")}; DISPID dipid [1] = {0}; hr=pDispatch- > GetIDsOfNames (IID_NULL, szMember, 1, LOCALE_USER_DEFAULT, dipid); CComVariant vars [2]; DISPPARAMS args = {NULL,NULL,0,0}; vars [0] = 2 Vars [1] = 1; args.cArgs = 2; args.rgvarg = vars; CComVariant Ret; hr=pDispatch- > Invoke (dipid [0], IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, & args, & Ret,NULL,NULL); std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.