Com notes-cocreateinstance

Source: Internet
Author: User
Cocreateinstance

The simplest way to create a component is to use the cocreateinstance function.

The com library contains a function named cocreateinstance used to create a component. This function requires a CLSID parameter. On this basis, an instance of the corresponding component is created and an interface of this component is returned.

 

Cocreateinstance Declaration

Hresult _ stdcall cocreateinstance (refclsid rclsid,

Lpunknown punkouter,

DWORD dwclscontext,

Refiid riid,

Lpvoid * GMM );

 

The first parameter is the CLSID of the component to be created.

The second parameter is used to aggregate components.

The third parameter: dwclscontext defines the execution context of the created component.

Fourth parameter: IID is the IID of the interface to be used on the component.

Cocreateinstance returns the pointer to this interface in the last parameter. By passing an IID to cocreateinstance, you do not need to call the QueryInterface function after creating a component.

 

Cocreateinstance implementation

HRESULT CoCreateInstance(const CLSID& clsid,                IUnknown* punkonwnDuter,                DWORD dwClsContext,                const IID& iid,                void** ppv){    // Set the out paameter to NULL    *ppv = NULL;    // Create the class factory     // and get an IClassFactroy interface pointer.    IClassFactory* pIFactory = NULL;    HRESULT hr = CoGetClassObject(clsid,                        dwClsContext,                        NULL,                        IID_IClassFactory,                        (void**)&pIFactory);    if (SUCCEEDED(hr))    {        // create the component.        hr = pIFactory->CreateInstance(punkonwnDuter, iid, ppv);        pIFactory->Release()();    }    return hr;}

 

Use of cocreateinstance

 

// Create component.

IX * pix = NULL;

Hresult hR =: cocreateinstance (clsid_companent1,

Null,

Clsctx_inproc_server,

Iid_ix,

(Void **) & pix );

If (succeeded (HR ))

{

PIX-> FX ();

PIX-> release ();

}

The clsctx_inproc_server value tells cocreateinstance to load only components that contain servers or DLL in the process.

 

 

Class Context

The third parameter dwclscontext of cocreateinstance can control whether the created component runs in the same process as the customer, in different processes, or on another machine.

 

Clsctx_inproc_server

The customer wants to create components that run in the same process. To be able to run in the same process as the customer, the component must be implemented in the DLL.

Clsctx_inproc_handler

The customer wants to create a processor in the process. A processor in a process is actually a component in a process that implements only part of a certain component. The matrix appendix of this component will be implemented by an out-of-process component on the local or remote server.

Slsctx_local_server

The customer wants to create a component that runs in another process on the same machine. The local server is implemented by exe.

Slsctx_remote_server

The customer wants to create a component that runs on a remote machine. This flag requires the Distributed COM to work properly.

 

 

Some predefined combinations of execution context tags

Constant name

Value

Clsctx_inproc

Clsctx_inproc_server

Clsctx_inproc_handler

Clsctx_all

Clsctx_inproc_server

Clsctx_inproc_handler

Slsctx_local_server

Slsctx_remote_server

Clsctx_server

Clsctx_inproc_server

Slsctx_local_server

Slsctx_remote_server

 

In addition, the clsctx_remote_server value is only contained in the objebase. before H, _ win32_winnt is added to clsctx_all and cisctx_server (including objebase. the effect of _ win32_dcom defined before H will be the same .) If the clsctx_remove_server value is given to cocreateinstance in a system that does not support DCOM, this function will fail and return an e_invalidarg value.

 

 

Cocreateinstance example

The difference is that the customer uses: cocreateinstance when creating a component, and uses coinitialize and couninitialize to initialize the com library.

Http://www.cnblogs.com/fangyukuan/archive/2010/04/09/1708651.html

 

# Include "stdafx. H "# include <iostream> using namespace STD; # include" http://www.cnblogs.com/ATLComDemo/ATLComDemo/ATLComDemo_ I .c "# include" http://www.cnblogs.com/ATLComDemo/ATLComDemo/ATLComDemo_ I .h "int _ tmain (INT argc, _ tchar * argv []) {// declare the hresult and ikuan interface pointers ikuan * ikuanatl = NULL; hresult hR = coinitialize (null ); // initialize com // use the succeeded macro and check whether we can obtain an interface pointer if (succeeded (HR) {hR = cocreateinstance (clsid_kuan, null, clsctx_inproc_server, iid_ikuan, (void **) & ikuanatl); // If successful, the addnumbers method is called; otherwise, the corresponding error message if (succeeded (HR) {long returnvalue is displayed; ikuanatl-> Add (8, 9, & returnvalue); cout <"the answer for 8 + 9 is:" <returnvalue <Endl; ikuanatl-> release ();} else {cout <"cocreateinstance failed. "<Endl ;}}couninitialize (); // release com return 0 ;}

 

 

Cocreateinstance is not flexible

Cocreateinstance creates a component by passing it a CLSID, creating the corresponding component, and returning the interface pointer pointing to the request. It does not provide customers with a way to control the component creation process.

After cocreateinstance is complete, the component is actually created. After a component is created, it is basically impossible to control where the component is loaded into the memory or check whether the customer has created the component.

Address: http://www.cnblogs.com/fangyukuan/archive/2010/06/11/1756724.html

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.