COM+简单示例(05)

本篇文章介绍了如何写一个简单的COM+客户端(CSharp)。

1、首先要导入COM组件编译时生成的TBL文件(ATL),并引用COM组件的Assembly(CS)

2、代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATLCOMPLib;
using CSDll;

namespace CSTestComp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                testATL();
                //testCS();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

        static void testATL()
        {
            Type justATestCOMPType = System.Type.GetTypeFromProgID("ATLCOMP.JustATestCOMP", "172.16.172.3");
            IJustATestCOMP svc = (IJustATestCOMP)System.Activator.CreateInstance(justATestCOMPType);
            int c = svc.Add(1, 2);
            System.Console.WriteLine(c);
            String retValue = svc.SayHiTo("com+");
            System.Console.WriteLine(retValue);
            System.Console.Read();
        }

        static void testCS()
        {
            Type justATestCSSvcType = System.Type.GetTypeFromProgID("CSDll.JustATestCSSvc", "172.16.172.3");
            IJustATestCSSvc svc = (IJustATestCSSvc)System.Activator.CreateInstance(justATestCSSvcType);
            int c = svc.Add(1, 2);
            System.Console.WriteLine(c);
            String retValue = svc.SayHiTo("com+");
            System.Console.WriteLine(retValue);
            System.Console.Read();
        }
    }
}

如果是本机测试(带IP),一般不会遇到权限问题

PS:
用了一晚上时间,只能调通Win7与Win7之间远程调用,无法调通Win7与XP之间远程调用(总是各种提示Access is Denied)。
如果有谁调通过,麻烦留言告诉我一下。谢谢!

Leave a Reply

Your email address will not be published. Required fields are marked *

*