Whether coinitialize is required when Delphi uses the COM control

Source: Internet
Author: User
Copyright Notice

Respect Original Works. Reprinted, please maintain the integrity of the article, and in the form of a hyperlink to indicate the original author "tingsking18" and the main site address, so that other friends can ask and correct.

 

Whether coinitialize is required when Delphi uses the COM control

Description of coinitialize on msdn:
Initializes the com library on the current apartment and identifies the concurrency model as single-thread apartment (STA ). applications must initialize the com library before they can call com library functions other than cogetmalloc and memory allocation functions.

Coinitialize does not load the com library. This function is only used to initialize the suite used by the current thread. When this function is used, the thread establishes a correspondence with a suite.
The suite mode of the thread determines how the thread calls the COM Object and whether the column set is required.
You can take a look at the information about the column set and use the object interface between different suites through the column set. The implementation of the column set is described in detail in many books.
Coinitialize () does not interfere with the communication between the customer and the service. The set of things is to let the thread register a suite, and the thread must be in this suite during running, just like every one of us who lives must belong to a country.
It should be emphasized that the suite is a very effective solution to the conflict of concurrent calls in COM.
 
Before calling any COM functions, a thread needs to call coinitialize to load the com infrastructure (and to enter an apartment). Once a thread CILS coinitialize, the thread is free to call com APIs.

Coinitialize (0) enables the current thread to enter the apartment of a sta

Coinitialize and coinitializeex are windows APIs, which mainly tell windows how to create COM objects for programs.
What are the methods? Single-thread and multi-thread.
Coinitialize indicates that it is created in a single thread mode.
Coinitializeex can specify coinit_multithreaded to be created in multiple threads.
When creating a single-threaded COM server, you do not need to consider serialization.
In use, you can use coinitialize to create an object that can be directly connected to a thread to achieve the highest performance. Creating a multi-threaded object can directly receive calls from all threads, without queuing of messages as a single thread, but with the need for com to create a collection proxy between threads, the access efficiency is not high.

Let's take a look at the Delphi Code. There is such a function in the comobj. PAS and comserv. Pas code:
Procedure initcomobj;
Begin
If saveinitproc <> nil then tprocedure (saveinitproc );
If (coinitflags <>-1) and assigned (comobj. coinitializeex) then
Begin
Needtouninitialize: = succeeded (comobj. coinitializeex (nil, coinitflags ));
Ismultithread: = ismultithread or
(Coinitflags and coinit_apartmentthreaded) <> 0) or
(Coinitflags = coinit_multithreaded); // this flag has value zero
End
Else
Needtouninitialize: = succeeded (coinitialize (NiL ));
End;
Obviously, it is used to call coinitialize to initialize the com environment.
In the initialization section of comobj. PAS:
Initialization
Begin
Loadcomexprocs;
Vardispproc: = @ vardispinvoke;
Dispcallbyidproc: = @ dispcallbyid;
{$ Ifdef mswindows}
Safecallerrorproc: = @ safecallerror;
{$ Endif}
If not islibrary then
Begin
Saveinitproc: = initproc;
// Point the initproc pointer to the initcomobj function.
Initproc: = @ initcomobj;
End;
End;

In the forms. Pas file, take a look at the tapplication. initialize section:
Procedure tapplication. initialize;
Begin
If initproc <> nil then tprocedure (initproc );
End;
Therefore, as long as we include comobj. Pas or comserv. PAS in the DPR file, Delphi will automatically initialize the com environment for us, so that we do not need to call it manually.

There are several methods to call the COM control in Delphi:

Createcomobject and createoleobject are both included in comobj. Pas.

Import the COM control through the Import Type Library. All generated PAS files contain the comobj unit.

The PAS file generated through import ActiveX also contains the comobj unit.

Therefore, you do not need to manually call coinitialize to use the COM control in Delphi.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.