C#调用COM组件的几个步骤

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>

这里我们将介绍C#调用COM组件的几个步骤,先将Com类型信息转换为.NET元数据,然后查看元数据,最后测试程序。

首先将Com类型信息转换为.NET元数据

  1. tlbimp sample.dll /out: sample_clw.dll

工具:Tlbimp.exe(类型库导入程序 )

参考:.Net framework SDK 文档

  1. ms-help://MS.NETFrameworkSDK.CHS/cptools/html/cpgrftypelibraryimportertlbimpexe.htm

查看元数据

工具:ILDasm

参考:ms-help://MS.NETFrameworkSDK.CHS/cptutorials/html/il_dasm_tutorial.htm

测试程序:

生成一个console programme

在project->add reference里,选择com,browser你的com,select

ok,现在在你的bin目录下应该有一个sample_clw.dll了

下面我们来用sample_clw的方法

  1. using System;
  2. using sample_clw;
  3. namespace CompConsole
  4. {
  5. /// <summary>
  6. /// Class1 的摘要说明。
  7. /// </summary>
  8. class Class1
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// Date:2003/6/20
  13. /// </summary>
  14. [STAThread]
  15. static void Main(string[] args)
  16. {
  17. //
  18. // TODO: 在此处添加代码以启动应用程序
  19. Console.Write ("=======Demo for Call com functions in c#=============\n");
  20. sampleClass sam = new sampleClass();
  21. //now we call functions
  22. //
  23. sam.SayHello();
  24. }
  25. }
posted @ 2010-11-05 13:33  netcorner  阅读(6588)  评论(0编辑  收藏  举报