Reprinted: original address: http://blogs.msdn.com/silverlightshanghai/archive/2008/07/23/com-net.aspx
In the previous blog, we used Visual Studio to convert the. NET Component (managed component) into a COM server. This topic continues to show you how to use the. NET toolkit to complete relevant functions.
First, let's take a look at the relationship between today's topic and CLR InterOP. In our group, the term InterOP contains four categories: P/invoke, reverse P/invoke, com InterOP and reverse com InterOP. The first two concepts implement interoperability in the native world and in the. NET world through the dynamic connection library (DLL); the last two concepts, as the name suggests, are dealing with COM. Among them, com InterOP uses COM components in. NET applications; reverse com InterOP refers to the use of. NET components in COM applications. The concept is a bit of a tongue twister. Don't rush to look at it.
Some may ask why. Net programmers need to deal with COM technology for a long time? The answer is the word "component. For example, a cool man wrote a program several years ago with excellent scalability. He used com to clearly define the interface of the plug-in, and we wanted to use. NET as the plug-in...
Write a. NET component for com. You can refer to the following Trilogy
1. Define the. NET interface and write. Net class
2. Deploy. NET components
3. Write a COM Client
The first step is very familiar to developers who often work in. net. Here is an example.
CSC/Target: Library A. CS
Second, to deploy the. NET component, there are two aspects:
Regasm A. dll/TLB
1. Export the Type Library. For a com application, it only knows the Type Library and is a binary "standard" followed by COM components ". The/TLB option tells regasm to export the Type Library. We can use oleview to view the content of TLB, as shown in. Net interfaces and classes previously defined are in it.
2. Place the DLL generated in step 2 to the Registry. COM does not understand concepts such as GAC. Instead, it queries hkey_classes_root/cls_id/commandid -0000-0000-ffff-000000000004 through the Registry. In inprocserver32, the dynamic link library of COM points to mscoree. DLL, which is the legendary Shim. It will load the common language runtime and find the real. NET Component ----. DLL.
With the above two steps, we can use the. NET Component in the com application. Writing COM components is beyond the scope of this article. If you are interested, refer to the comments in the code.
CL client. CS
- // Client. CS
- # DEFINE _ win32_com
- # Include <stdio. h>
- # Include <wtypes. h>
-
- # Import "A. TLB" no_namespace named_guids raw_interfaces_only
- // A. TLB is exported in step 2
- Int main ()
- {
- Iunknown * punk = NULL;
- Ia * Pia = NULL;
- Hresult;
-
- // 0. initialize the COM Component
- Hresult = coinitializeex (null, coinit_multithreaded );
- If (failed (hresult ))
- {
- Printf ("error: cannot initialze COM: 0x % x/N", hresult );
- Return-1;
- }
-
- // 1. Obtain the iunknown Interface
- Hresult = cocreateinstance (clsid_a, null, clsctx_inproc_server, iid_iunknown, (void **) & punk );
- If (failed (hresult ))
- {
- Printf ("error: cannot get the iunkown interface: 0x % x/N", hresult );
- Return-1;
- }
-
- // 2. Obtain the IA Interface
- Hresult = punk-> QueryInterface (iid_ia, (void **) & PIA );
- If (failed (hresult ))
- {
- Printf ("error: cannot convert to idispatch interface: 0x % x/N", hresult );
- Punk-> release ();
- Return-1;
- }
-
- Punk-> release ();
-
- // 3. Call the. NET Component
- Hresult = Pia-> Hello ();
- If (failed (hresult ))
- {
- Printf ("error: invoke failed: 0x % x/N", hresult );
- }
-
- // 4. Clear
- Pia-> release ();
- Couninitialize ();
- Return 0;
- }