COM operations (ii) interface queries in C #

Source: Internet
Author: User
Tags count wrapper

A question left at the end of the last article come back and give me a answer. You can see the following figure is clear:

The conclusion is that T1,T2,T3 is three different references, that is to say, in. NET represents three different types, but the three types of GUIDs are the same, because in COM the GUID represents a COM class, and as long as the GUID is the same, then it means a COM class, so the three types are the same COM type only from the point of view of the COM class.

The type of the. NET wrapper for the COM object created in the 1th way is generally the type of the corresponding declaration inside the. NET wrapper assembly that is imported by COM.

The type of the. NET wrapper for the COM object created in the 2nd way is always __comobject.

The. NET wrapper for the COM object created in the 3rd way, or the wrapper of the. NET that the pointer passes through the Marshal class, is the type of __comobject that corresponds to the two methods.

The 4th variant, which is essentially the 1th way, is more flexible and more widely used, so the corresponding type should also be declared. NET in the type

The result of the second question left in the previous article is what type, after the Marshal class method and the IntPtr after the conversion of the result or what type, should be the CLR within the record of the pointer and. NET type of the corresponding relationship, Do not use a different wrapper each time the IntPtr is transferred to object (it feels a bit like WinForm from handle).

In the previous article we talked about several ways to create COM objects in C #. I do not know that the last way the Jetengineclass class does not provide a way for us to invoke, to use it must first turn the reference into an interface reference in order to directly use the method inside, implement the early function binding. Although we did not specify this class to implement the Ijetengine interface when we declared the Jetengineclass class, we used the following to turn the engine into the Ijetengine interface with the as operation, and it turned out to be successful. And you can also use the IS operator to test that the result of the engine is ijetengine back is true. This is the query---C # COM object interface in this article.

As with Com-created methods, there are several ways to query COM interfaces in C #:

The 1th method of Marshal.queryinterface

This method itself is the Orthodox method provided by the framework for querying COM objects, which has been explained in detail on MSDN, and I don't say much more. The only thing to notice here is that only the pointer IntPtr of the COM object can be passed, and after this method is successfully returned, The count of the referenced COM objects will increase by 1. Later, when the pointer to the returned query is no longer in use, it is necessary to manually invoke Marshal.release to balance the COM reference count. Even if it's simple, give it a piece of code.

1 IntPtr pJetClass = GetJetEngine(); // return JetEngineClass Ptr
2
3 IntPtr pJet;
4 Guid g = typeof(IJetEngine).Guid;
5 int hr = Marshal.QueryInterface(pJetClass, ref g, out pJet);
6 if(hr <0)
7   Marshal.ThrowExceptionFromHR(hr);
8

In fact, when using IntPtr to refer to COM objects, just as in C + + directly using the COM pointer, in theory, each copy of the pointer, we need to manually call the AddRef method, increase the reference count of COM objects, Whenever we set the pointer to be invalid or no longer use this pointer, we also need to manually reduce the reference count with the release method, releasing the COM object when the reference count becomes 0. It's still not getting rid of C + + It is easy to forget the problem of balancing reference count when using the original COM pointer. Here I deliberately use the "original COM pointers" concept, mainly distinguishes in C + + we often use COM pointers in another way comptr<t> generic class, with this generic class C + + The reference count of the COM objects inside can be increased and reduced in a normal and timely manner, allowing the developer to spend no effort on maintaining the COM reference count. But even so, to query an interface can not get rid of that QueryInterface method.

C # As a language that inherits most of the benefits of C + +, of course, provides a similar way to keep us away from the trap of reference counting, and provides a more elegant way for us to use it.

That's the 2nd way we're going to talk about COM interface queries.

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.