Cominterop-platform interoperability

Source: Internet
Author: User

I registered in the blog Park for half a year and have also been diving for half a year. I hope you will forgive me for writing this article for the first time today.

I believe many of my friends have experience in calling the platform. However, from the online search information, it seems that they are not very optimistic.
Based on my own experiences in using C ++ and C # hybrid calls, I will summarize the cost and hope to give some inspiration to friends who are interested or need it.
Because I am only a little familiar with the C # language, the. NET platform mentioned in this article takes C # as an example.

1. Use the function in the C ++ DLL file in the C # language environment: Use dllimport.
This is detailed in msdn, including data sending and processing, and comparison of basic types of C ++/C.

2. Use the hosted C # component in the C ++ language environment.

2.1 packaging class.

The RegEx in C # provides powerful functions.CodeSo we need to wrap the RegEx and define the following Regexp class.

Regexp. CS

Using System;
Using System. Text. regularexpressions;
Using System. runtime. interopservices;

Namespace Dotnetlib
{
[GUID ( " 0f0d2a66-0262-47b5-a7f9-8bffb8a13f90 " ), Comvisible ( True )]
Public   Interface Iregexp
{
String Replace ( String Input, String Pattern, String Replacement );
String [] Split ( String Input, String Pattern );
}

///   <Summary>
/// Wrapper of RegEx class to provide
/// It's powerful function of dealing with text.
/// This will generate a com class which can be
/// Used by various platforms or ages.
///   </Summary>
[GUID ( " 2634bea5-17ff-40ba-a837-12d30bad38d5 " ), Classinterface (classinterfacetype. None), comsourceinterfaces ( Typeof (Iregexp), comvisible ( True )]
Public   Class Regexp: iregexp
{
Public Regexp ()
{
}

# RegionIregexp members

// [Return: financialas (unmanagedtype. lpwstr)]
Public   String Replace ( String Input, String Pattern, String Replacement)
{
Return RegEx. Replace (input, pattern, replacement );
}

Public String[] Split (StringInput,StringPattern)
{
ReturnRegEx. Split (input, pattern );
}

# Endregion
}
}

Compile the file into a DLL file and use regasm for registration. The corresponding type library file will be generated. We assume it is dotnetlib. TLB. So far our packaging is complete.

2.2 ing Class.

Because we call it through COM, we need to perform a lot of data conversion work. To avoid these details, we define a ing class so that users can use this class just like using a real RegEx class.

Cominterface. h

# Pragma Once
# Include < Comutil. h >

ClassCominterface
{
Public:
Cominterface (refclsid rclsid );
Cominterface (iunknown*Piunknown );
~Cominterface (Void);

Protected:
Iunknown*M_piunknown;

Private:
Idispatch*M_pidispatch;
Static BoolM_bcoinitialize;
};

Cominterface. cpp

# Include " Stdafx. h "
# Include " Cominterface. h "
# Include < Comutil. h >

BoolCominterface: m_bcoinitialize= False;

Cominterface: cominterface (refclsid rclsid)
{
M_piunknown=NULL;
M_pidispatch=NULL;

If(!M_bcoinitialize)
Coinitialize (null );

Hresult HR=Cocreateinstance (rclsid, null, clsctx_inproc_server,
Iid_iunknown ,(Void**)&M_piunknown );
Atlassert (succeeded (HR ));

HR=M_piunknown->QueryInterface (iid_idispatch ,(Void**)&M_pidispatch );
Atlassert (succeeded (HR ));
}

Cominterface: cominterface (iunknown*Piunknown)
{
M_piunknown=Piunknown;
M_pidispatch=NULL;

Hresult HR=M_piunknown->QueryInterface (iid_idispatch ,(Void**)&M_pidispatch );
Atlassert (succeeded (HR ));
}

Cominterface ::~Cominterface (Void)
{
If(M_pidispatch)
M_pidispatch->Release ();

If(M_piunknown)
M_piunknown->Release ();

If(M_bcoinitialize)
{
M_bcoinitialize= False;
Couninitialize ();
}
}

Regexp. h

# Pragma Once
# Include " Atlsafe. h "
# Include " Cominterface. h "
# Import " E: \ mydocument \ myprojects \ cominteroptest \ dotnetlib \ Lib \ dotnetlib. TLB " Named_guids
Using   Namespace Dotnetlib;

Class Cregexp: Public Cominterface
{
Public :
Cregexp ();
Virtual ~ Cregexp ();
Cstring Replace ( Const Cstring & Strinput, Const Cstring & Strparttern,
Const Cstring & Strreplacement );
Bool Split ( Const Cstring & Strinput, Const Cstring & Strparttern,
Safearray ** Ppsafearray );

Private:
Iregexp*M_piregexp;
};

Regexp. cpp

# Include " Stdafx. h "
# Include " . \ Regexp. h "

Cregexp: cregexp ()
: Cominterface (clsid_regexp)
{
Hresult HR=M_piunknown->QueryInterface (iid_iregexp, (lpvoid*)&M_piregexp );
Atlassert (succeeded (HR ));
}

cregexp: ~ cregexp ()
{< br> If (m_piregexp)
m_piregexp -> release ();
}

cstring cregexp: Replace ( const cstring & strinput, const cstring & strparttern,
const cstring & strreplacement)
{< br> cstring strresult;

If (m_piregexp)
{< br> strresult = m_piregexp -> Replace (_ bstr_t (strinput), _ bstr_t (strparttern),
_ bstr_t (strreplacement )). getbstr ();
}< br> else
strresult = _ T ( " " );

ReturnStrresult;
}

Bool Cregexp: Split ( Const Cstring & Strinput, Const Cstring & Strparttern,
Safearray ** Ppsafearray)
{
If (M_piregexp)
{
* Ppsafearray = M_piregexp -> Split (_ bstr_t (strinput), _ bstr_t (strparttern ));

return true ;
}< br> else
return false ;
}

Now, the ing Class is defined. We can do a simple test:
Cregexp exp;
Cstring strtest = _ T ("He is a good boy, he is very like his father ");
Strtest = exp. Replace (strtest, _ T ("he"), _ T ("she "));
Strtest = exp. Replace (strtest, _ T ("boy", _ T ("girl "));
Strtest = exp. Replace (strtest, _ T ("his"), _ T ("her "));
Strtest = exp. Replace (strtest, _ T ("father"), _ T ("mother "));
// OK, now strtest shoshould be "She is a good girl, she is very like her mother ".

The packaging scheme is clumsy and the code execution efficiency is not high, but I have not found a better solution yet. If there are talents in this area, I hope to add it.

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.