用C shape编写COM组件

1、新建一个类库项目


2、将Class1.cs改为我们想要的名字

问是否同时给类改名,确定


3、修改Properties目录下面的AssemblyInfo.cs
ComVisible属性设置为True


4、项目菜单->MyLib属性
找到“生成”选项卡

往下看,找到“为 COM Interop 注册”勾上


5、继续往下,找到“签名”选项卡
勾上“为程序集签名”
在下面的下拉框里面选择“ <新建...>”


6、在弹出的对话框里面,输入MyLib。。或者随便取个名字
去掉使用密码保护文件的选项


7、开始编码,任何一个公开的类,必须有一个 I开通的接口定义

C# code

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MyLib
{
    [ComVisible(true)]
    [Guid("2CBD3D76-35F1-4f9d-9C1B-9DBFEE412F76")]
    public interface IMyClass
    {
        void Initialize();
        void Dispose();
        int Add(int x, int y);
    }
    [ComVisible(true)]
    [Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0D")]
    [ProgId("MyLib.MyClass")]
    public class MyClass : IMyClass
    {
        public void Initialize()
        {
            //nothing todo
        }
        public void Dispose()
        {
            //nothing todo
        }
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}
 

8、GUID属性里面的那个字符串,在“工具”菜单下面,“创建 GUID”
选择 Registry Format,然后复制


注意在[Guid("....... 这个里面要去掉GUID前后的花括号

 

 

 

如果在win7下编译时提示无权访问注册表,以管理员身份打开vs

9、编译它
在命令提示符下面,进入Dll所在的目录
用 gacutil /i MyLib.dll 将这个DLL加入的全局缓存里
然后用 regasm MyLib.dll 注册这个dll


10、在VBScript里面试试。。。

HTML code
<script language="VBScript">
Dim o : Set o=CreateObject("MyLib.MyClass")  
  o.Initialize
    MsgBox "1 + 2 = " & o.Add(1,2)
    o.Dispose
    Set o=Nothing
</script>
 
  
  
原贴:http://www.cnblogs.com/Real_Dream/articles/1577938.html

11 , 在 vc里调用 需要 MFC的DISPATCH驱动类调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> using namespace std; class CShape{ public:float Area; float Perimeter; CShape() { Area=0;Perimeter=0; } virtual void GetArea(){} virtual void GetPerimeter(){} }; class CRectangle:public CShape{ private: int l;int h; public: CRectangle(int h,int l ):CShape() {this->h=h;this->l=l; } void GetArea() {Area=l*h;cout<<"矩形的面积="<<Area<<endl;} void GetPerimeter() {Perimeter=(l+h)*2;cout<<"矩形的周长="<<Perimeter<<endl; } }; class CCirle:public CShape{ private:float r; public:CCirle(float r):CShape() {this->r=r; } void GetArea() { Area=3.14159*r*r; cout<<"圆的面积="<<Area<<endl; } void GetPerimeter() { Perimeter=r*3.14159*2; cout<<"圆的周长="<<Perimeter<<endl; } }; class CSquare:public CShape{ private: int a; public: CSquare(int a):CShape() { this->a=a; } void GetArea() { Area=a*a; cout<<"正方形面积="<<Area<<endl; } void GetPerimeter() { Perimeter=4*a; } }; class CTrapeziod:public CShape{ private: int lu; int ld; int h; public: CTrapeziod(int lu,int ld,int h):CShape() { this->lu=lu; this->ld=ld; this->h=h; } void GetArea() { Area=(lu+ld)*h/2; cout<<"梯形面积="<<Area<<endl; } }; class CTrangle:public CShape{ private: int d; int h; public: CTrangle(int d,int h):CShape() { this->d=d; this->h=h; } void GetArea() { Area=d*h/2; cout<<"三角形的面积="<<Area<<endl; } }; void main() { CShape *p[5]; CRectangle CR(5,10); CCirle CC(3); CSquare CS(4); CTrapeziod CT1(2,5,4); CTrangle CT2(6,2); p[0]=&CR; p[0]->GetArea(); p[1]=&CC; p[1]->GetArea(); p[2]=&CS; p[2]->GetArea(); p[3]=&CT1; p[3]->GetArea(); p[4]=&CT2; p[4]->GetArea(); int i, double SumArea=0.f; for(i=0;i<5;i++) { SumArea=p[i]->Area+SumArea; } cout<<"面积总和="<<SumArea<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值