The CLR fully introduces

Source: Internet
Author: User
Tags definition uuid

COM is a very good technology. It is precisely because the common language runtime (CLR) enables seamless interaction between microsoft®.net applications and unmanaged COM components that makes the CLR an extremely powerful platform. But when I search on the web, I can hardly find a useful example that describes the most basic concepts of COM Interop. The purpose of this column is to explain these basic concepts and provide practical and useful examples to help users get started quickly in this technology area.

I'll start with a simple Active Template Library (ATL) COM server, try different access methods on that server using an unmanaged COM client, and then do the same with the managed client. I'll introduce each DLL in detail, explain the transition from unmanaged to managed, and also explain how to use P/invoke to access the exported method in an unmanaged DLL. The hardest part of this article is to understand the marshaling of complex structures, which will not be covered in detail in this column. Just introducing this knowledge requires opening up a complete column or writing a book. I'll show you how unmanaged code uses an interface to tune managed code back and forth. (You can also use a delegate to do this, but this column does not describe this approach.) )

Finally, I'll introduce debugging your COM Interop project with a common symbol. This section provides the most basic introduction to WinDbg.exe, unmanaged debugging, and managed debugging using SOS. I will demonstrate the stack when calling unmanaged code in managed code or when calling managed code in unmanaged code.

A simple ATL COM server

Let's start by writing a simple COM server. The client hosts the server that is running and executes the method on the server.

To make development easier, I'll use ATL COM. Create a directory named Cominteropsample, and then create a new Visual C++®atl project and name it (assuming Msdncomserver). Deselect the Create new directory option, and the other items take the default values.

Now add a new class/interface to the solution. If you haven't seen it yet, open Solution Explorer. Right-click Msdncomserver and choose Add | Class ". Select ATL and ATL Simple objects, and then click Add. Enter the abbreviation for the class (assumed to be mycomserver), and then the other items take the default values. Click Finish to add the class.

Add a new method to the interface you created for you. Go to Class View, right-click the Imycomserver interface, and choose Add | Add method ". Name the method (assumed to be Mycomservermethod), and then click Finish.

Now, we're going to implement this method in the class. Select MyCOMServer.cpp and view the source code. You will see the method added by the wizard in the interface of the CPP file. In the TODO section, add the following code:

wprintf_s (L "Inside Mycomservermethod");

Compile the code to build the server DLL.

Now, we have completed the ATL COM server. It implements the Imycomserver interface in the class Cmycomserver and writes a message to the default output stream. The GUID of the interface is defined in the IDL file as an attribute of the interface definition, as shown in Figure 1.

Figure 1 Imycomserver definition

[
object,
uuid (45fa03a3-fd24-41eb-921c-15204caf68ae),
nonextensible,
helpstring ("Imycomserver Interface "),
pointer_default (unique)
]
Interface imycomserver:iunknown {
[helpstring (" Method Mycomservermethod ")]
HRESULT (Mycomservermethod) (void);
[
uuid (3BA5DF7B-F8EF-4EDE-A7B5-5E7D13764ACC),
version (1.0),
helpstring ("Msdncomserver 1.0 Type  Library ")
]
library msdncomserverlib
{
importlib (" Stdole2.tlb ");
[
uuid (2D6D2821-A7FB-4F99-A61A-2286A47CD4D1),
helpstring ("Mycomserver Class")
]
coclass My  COMServer
{
[default] interface Imycomserver;
};
};

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.