The customer's system is a combination of C/S and B/S. The B/S part was originally planned to use IntraWeb.
Delphi.net happened to appear, so the B/S part was switched to Delphi.net;
A problem arose during the development process: the encryption algorithm was developed by Delphi 7.
Time was too tight and it was impossible to rewrite the encryption algorithm using Delphi.net, so I thought of COM/COM+,
It's a pity that there is very little information on Delphi.net, and even less information on interoperability with COM/COM+.
So I explored it myself and came up with the following (source code attached):
1. Create COM/COM+ in Delphi 7
(1) In the IDE, File -> New -> Other -> ActiveX -> ActiveX Library,
Save the project file as CompRoject;
(2) In the IDE, File -> New -> Other -> ActiveX -> COM Object,
Fill in Test in the Class Name column and save the unit as Test;
(3) Add a process named GetMsg to ITest in View -> Type Library in the IDE;
(4)Ctrl + F9;
(5) Run -> Register ActiveX Server in the IDE; // Registering COM/COM+ can also be done using the command line
2. Call COM/COM+ in Delphi.net
(1) In the IDE, File -> New -> asp.net Web application,
You may encounter a lot of troubles that you have to deal with yourself!
(2) Add a reference to COM/COM+:
In the IDE Project ->Add Reference -> COM Imports,
Select the ComProject you just registered and click Add Reference to add a reference.
Click OK (Pay attention to your project file (ASP.NET Web Application)
The path (Path) cannot use Chinese);
(3) Add ComProject to uses in WebForm1;
(4) Call COM/COM+
Put two Web Controls above WebForm1: a Button and a Label;
Double-click the Button and write in the process:
var
Msg: string;
Obj: TObject;
begin
Obj := Server.CreateObject('ComProject.Test'); // Create COM/COM+
Test(Obj).GetMsg(Msg); // Convert Obj to Test and call the GetMsg method;
Label1.Text := Msg;
end;
(5) Add Aspcompat="true" to the Page line of WebForm1.aspx,
For example: <%@ Page language="c#" Debug="true" Codebehind="WebForm1.pas" AutoEventWireup="false" Inherits="WebForm1.TWebForm1" Aspcompat="true"%>
(6)Ctrl + F9,Run ->Run (or Run Without Debugging);
(7) Click the Button on the web page, is it done?
//The following is the source code part:
COM/COM+ project file (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 unit (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+ test successful!';
end;
initialization
TTypedComObjectFactory.Create(ComServer, TTest, Class_Test,
ciMultiInstance, tmApartment);
end.
//COM/COM+ type library t unit (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
usesComObj;
class function CoTest.Create: ITest;
begin
Result := CreateComObject(CLASS_Test) as ITest;
end;
class function CoTest.CreateRemote(const MachineName: string): ITest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_Test) as ITest;
end;
end.
///////////////////////////////////////The following is the source code for calling COM/COM+ from Delphi.net/// ////////////////////////////
//WebForm1.pas unit:
unit WebForm1;
interface
uses
System.Collections, System.ComponentModel,
System.Data, System.Drawing, System.Web, System.Web.sessionState,
System.Web.UI, System.Web.UI.WebControls, System.Web.UI.HtmlControls,
ComProject;
type
TWebForm1 = class(System.Web.UI.Page)
{$REGION 'Designer Managed Code'}
strict private
procedureInitializeComponent;
procedure Button1_Click(sender: System.Object; e: System.EventArgs);
{$ENDREGION}
strict private
procedure Page_Load(sender: System.Object; e: System.EventArgs);
strictly protected
Button1: System.Web.UI.WebControls.Button;
Label1: System.Web.UI.WebControls.Label;
procedure OnInit(e: EventArgs); override;
private
{Private Declarations}
public
{Public Declarations}
end;
implementation
{$REGION 'Designer Managed Code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWebForm1.InitializeComponent;
begin
Include(Self.Button1.Click, Self.Button1_Click);
Include(Self.Load, Self.Page_Load);
end;
{$ENDREGION}
procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs);
begin
// TODO: Put user code to initialize the page here
end;
procedure TWebForm1.OnInit(e: EventArgs);
begin
//
// Required for Designer support
//
InitializeComponent;
inheritedOnInit(e);
end;
procedure TWebForm1.Button1_Click(sender: System.Object; e: System.EventArgs);
var
Msg: string;
Obj: TObject;
begin
Obj := Server.CreateObject('ComProject.Test');
Test(Obj).GetMsg(Msg);
Label1.Text := Msg;
end;
end.
///WebForm1.aspx unit:
<%@ Page language="c#" Debug="true" Codebehind="WebForm1.pas" AutoEventWireup="false" Inherits="WebForm1.TWebForm1" Aspcompat="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Borland Package Library 7.1">
</head>
<body ms_positioning="GridLayout">
<form runat="server">
<asp:button id=Button1
style="Z-INDEX: 1; LEFT: 238px; POSITION: absolute; TOP: 158px"
runat="server" text="Button">
</asp:button>
<asp:label id=Label1
style="Z-INDEX: 2; LEFT: 190px; POSITION: absolute; TOP: 54px"
runat="server">Label</asp:label>
</form>
</body>
</html>