Com application Summary (1/3)

Source: Internet
Author: User

I haven't touched on the Content of COM components for a long time. In the past two days, an existing product needs to be updated and involves the processing of COM. The team members are not very familiar with this and have a chat with the team members, record the main content and provide some guidance for quick understanding and getting started. Of course, for specific working principles and more content, refer to specific books for learning.

Summary:

Ø COM components in C ++

Ø COM components in C #

C ++ instance and deployment

Basic and references

All-in-One code frameworkHttp://cfx.codeplex.com/this project has detailed steps for using C # C ++ to create a COM component such as [dll exe.

Com essence and the principles and applications of COM compiled by Pan aimin are excellent references.

COM components in C ++

Use an example to describe

COM Component

Create a simple description. The component interface is named imykit, and the method is a dowork, a parameter is input, and an output is as follows:

Interface imykit: idispatch {
[ID (1)] hresult dowork ([in] BSTR text, [out] BSTR * MSG );
};
[
UUID (87bf6309-28b6-40fa-ad26-5175d884d28e ),
Version (1.0 ),
]
Library compluglib
{
Importlib ("stdole2.tlb ");
[
UUID (D7D0B2E8-1795-4E23-96BF-F07EC28FB44C)
]
Coclass mykit
{
[Default] interface imykit;
};
};

Stdmethodimp cmykit: dowork (BSTR text, BSTR * MSG)
{
Afx_manage_state (afxgetstaticmodulestate ());
Cstring T = text;
Afxmessagebox (t );
Cstring r = _ T ("out text ");
* MSG = R. allocsysstring ();
Return s_ OK;
}
How to use the above components in C ++

First introduce Definition

# Ifdef _ debug

# Import "../debug/complug. dll" no_namespace raw_interfaces_only

# Else

# Import "../release/complug. dll" no_namespace raw_interfaces_only

# Endif

Initialization

// You must call this initialization.
Coinitialize (null );

/* The COM component parameters include in out/ret. The space allocation principle is as follows:
In: The caller is responsible for application and release.
Out: the COM component is responsible for application and the caller is responsible for release.
Memory leakage may occur in violation of the above principles.
*/

Usage 1

Used in pure Win32 projects, and occasionally used

Hresult hr;
/// Method for pure Win32 Application
Imykit * pkit;
HR =: cocreateinstance (_ uuidof (mykit), null, clsctx_inproc_server, _ uuidof (imykit), (void **) & pkit );
If (succeeded (HR ))
{
Bstr t =: sysallocstring (L "hello ");
Bstr r;
Pkit-> dowork (T, & R );
Messageboxw (null, R, null, mb_ OK );
/// String Conversion is supported. There are a series of functions in the same type, and the space is allocated to the stack. Do not release them.
Uses_conversion;
Lpstr P = w2a (R );
Messageboxa (null, P, null, mb_ OK );
: Sysfreestring (t );
: Sysfreestring (R );
Pkit-> release ();
Pkit = NULL;
}

Usage 2

Common

// This is the most common case when it is used in combination with the MFC and ATL libraries.
// Encapsulation of _ bstr_t in comutil. H is also used.
Imykitptr kit;
HR = kit. createinstance (_ uuidof (mykit ));
If (succeeded (HR ))
{
/* The basic types in COM, such as variant, are complex. Generally, do not use them directly.
Several common variables simplify the call of com-type variant colesafearray colevariant ccombstr
*/
Colevariant vtrue = colevariant (short) True ),
Vfalse = colevariant (short) False ),
Vopt = colevariant (long) disp_e_paramnotfound, vt_error ),
Vnull = (colevariant) (_ T ("")),
Vone = colevariant (short) 1 );
Ccombstr text, MSG;
TEXT = _ T ("in text ");
Kit-> dowork (text, & MSG );
Cstring STR = MSG;
Afxmessagebox (STR );
Kit = NULL;
}

Usage 3

Complicated and not commonly used

/// Dynamic call
Ccomptr <idispatch> PCF;
CLSID;
Const lpolestr guid = l "{D7D0B2E8-1795-4E23-96BF-F07EC28FB44C }";
Clsidfromstring (guid, & CLSID );
Hresult result = cocreateinstance (CLSID, null, clsctx_server, iid_idispatch, (void **) & PCF );
If (result = s_ OK)
{
Dispparams;
Memset (& dispparams, 0, sizeof dispparams );
Dispparams. cargs = 2;
Dispparams. cnamedargs = 0;
Variantarg * pvarg = new variantarg [dispparams. cargs];
Dispparams. rgvarg = pvarg;
Memset (pvarg, 0, sizeof (variantarg) * dispparams. cargs );
Ccombstr T = "dynamic", R;
// The opposite order of function declaration
Pvarg [0]. Vt = vt_bstr | vt_byref;
Pvarg [0]. pbstrval = & R;
Pvarg [1]. Vt = vt_bstr;
Pvarg [1]. bstrval = T;
Dispid id = 1; // function ID
Result = PCF-> invoke (ID, iid_null, locale_user_default, dispatch_method, & dispparams, null, null );
For (uint I = 0; I <dispparams. cargs; I ++)
{
Variantclear (& dispparams. rgvarg [I]);
}
Delete [] pvarg;

End

Couninitialize ();

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.