转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
在Delphi.net中调用COM/COM+         ★★★★

在Delphi.net中调用COM/COM+

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1614 更新时间:2009/4/23 18:34:53

客户的系统是C/S和B/S的结合,B/S部分原来计划用IntraWeb,
刚好Delphi.net出现,所以B/S部分改用Delphi.net;
在开发过程中出现了一个问题:加密算法是Delphi 7开发的,
时间太紧不可能用Delphi.net重写加密算法,于是想到COM/COM+,
只可惜Delphi.net资料很少,与COM/COM+互操作的资料就更少了。
于是自己摸索,便有下文(源码附后):

1,在Delphi 7中建立COM/COM+
(1)在IDE中File -> New -> Other -> ActiveX -> ActiveX Library,
将工程文件保存为ComProject;
(2)在IDE中File -> New -> Other -> ActiveX -> COM Object,
在Class Name栏填上Test,该单元保存为Test;
(3)在IDE中View -> Type Library给ITest加一个名为GetMsg的过程;
(4)Ctrl + F9;
(5)在IDE中Run -> Register ActiveX Server; // 注册COM/COM+也可用命令行
2, 在Delphi.net中调用COM/COM+
(1)在IDE中File -> New -> ASP.NET Web Application,
可能会遇到很多麻烦要你自己搞定哦!
(2)添加COM/COM+的引用:
在IDE中Project ->Add Reference -> COM Imports,
选中刚才注册的ComProject,点击Add Reference 添加引用,
单击OK(要注意你的项目文件(ASP.NET Web Application)
的路径(Path)不能使用中文哦);
(3)在WebForm1得uses中添加ComProject;
(4)调用COM/COM+
在WebForm1上方上两个Web Control:一个Button,一个Label;
双击Button,在过程中写上:
var
  Msg: string;
  Obj: TObject;
begin
  Obj := Server.CreateObject(''''ComProject.Test'''');  // 建立COM/COM+
  Test(Obj).GetMsg(Msg);   // 将Obj转换为Test,调用GetMsg方法;
  Label1.Text := Msg;
end;
(5)在WebForm1.aspx的Page行中加上Aspcompat="true",
如:<%@ Page language="c#" Debug="true" Codebehind="WebForm1.pas" AutoEventWireup="false" Inherits="WebForm1.TWebForm1" Aspcompat="true"%>
(6)Ctrl + F9,Run ->Run(或者Run Without Debugging);
(7)在网页中点击Button,是不是搞定了!

// 以下为源码部分:

COM/COM+工程文件(ComProject.dpr):

library ComProject;

uses
  ComServ,
  ComProject_TLB in ''''ComProject_TLB.pas'''',
  Test in ''''Test.pas'''' {Test: CoClass};

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
end.

//COM/COM+的Test单元(Test.pas):

unit Test;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj, ComProject_TLB, StdVcl;

type
  TTest = class(TTypedComObject, ITest)
  protected
    function GetMsg(out Msg: WideString): HResult; stdcall;
    {Declare ITest methods here}
  end;

implementation

uses ComServ;

function TTest.GetMsg(out Msg: WideString): HResult;
begin
  Msg := ''''Com/Com+测试成功!'''';
end;

initialization
  TTypedComObjectFactory.Create(ComServer, TTest, Class_Test,
    ciMultiInstance, tmApartment);
end.

//COM/COM+的类型库t单元(ComProject_TLB.pas):

unit ComProject_TLB;

// ************************************************************************ //
// WARNING                                                                   
// -------                                                                   
// The types declared in this file were generated from data read from a      
// Type Library. If this type library is explicitly or indirectly (via       
// another type library referring to this type library) re-imported, or the  
// ''''Refresh'''' command of the Type Library Editor activated while editing the  
// Type Library, the contents of this file will be regenerated and all       
// manual modifications will be lost.                                        
// ************************************************************************ //

// PASTLWTR : 1.2
// File generated on 2004-2-6 13:28:46 from Type Library described below.

// ************************************************************************  //
// Type Lib: D:\Program Files\Borland\Delphi7\Projects\comtest2\ComProject.tlb (1)
// LIBID: {92B46A1D-8A31-46C5-98FE-C03FEA98DC21}
// LCID: 0
// Helpfile:
// HelpString: ComProject Library
// DepndLst:
//   (1) v2.0 stdole, (D:\WINDOWS\system32\stdole2.tlb)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface

uses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;
 

// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:       
//   Type Libraries     : LIBID_xxxx                                     
//   CoClasses          : CLASS_xxxx                                     
//   DISPInterfaces     : DIID_xxxx                                      
//   Non-DISP interfaces: IID_xxxx                                       
// *********************************************************************//
const
  // TypeLibrary Major and minor versions
  ComProjectMajorVersion = 1;
  ComProjectMinorVersion = 0;

  LIBID_ComProject: TGUID = ''''{92B46A1D-8A31-46C5-98FE-C03FEA98DC21}'''';

  IID_ITest: TGUID = ''''{96CECA70-1438-4C54-982E-67C378F085E8}'''';
  CLASS_Test: TGUID = ''''{CA54722F-C5EF-4814-A8DB-C1C357832F08}'''';
type

// *********************************************************************//
// Forward declaration of types defined in TypeLibrary                   
// *********************************************************************//
  ITest = interface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library                      
// (NOTE: Here we map each CoClass to its Default Interface)             
// *********************************************************************//
  Test = ITest;


// *********************************************************************//
// Interface: ITest
// Flags:     (256) OleAutomation
// GUID:      {96CECA70-1438-4C54-982E-67C378F085E8}
// *********************************************************************//
  ITest = interface(IUnknown)
    [''''{96CECA70-1438-4C54-982E-67C378F085E8}'''']
    function GetMsg(out Msg: WideString): HResult; stdcall;
  end;

// *********************************************************************//
// The Class CoTest provides a Create and CreateRemote method to         
// create instances of the default interface ITest exposed by             
// the CoClass Test. The functions are intended to be used by            
// clients wishing to automate the CoClass objects exposed by the        
// server of this typelibrary.                                           
// *********************************************************************//
  CoTest = class
    class function Create: ITest;
    class function CreateRemote(const MachineName: string): ITest;
  end;

implementation

uses ComObj;

class function CoTest.Create: ITest;
begin
  Result := CreateComObject(CLASS_Test) as ITest;
end;

class function CoTest.C

[1] [2]  下一页


[C语言系列]NET 中C#的switch语句的语法  [聊天工具]Google Suggest十大妙用
[聊天工具]保驾护航Web迅雷 全新版本给你更多安全  [聊天工具]玩转火狐的Cookie 让火狐狸吃好小甜饼!
[聊天工具]P2P下载的好工具 POCO完全攻略  [聊天工具]横扫一切高价话费 Vbuzzer八分钱国际长途任你打
[聊天工具]众人拾柴火焰高 改进迅雷于不经意间 迅雷  [聊天工具]中英文互翻 Google Toolbar4中文版试用手记
[聊天工具]巧用µTorrent 体验国外下载站的乐趣  [聊天工具]可远程搜索桌面—Google Desktop 3全新体验
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台