【发布时间】:2010-10-18 18:16:57
【问题描述】:
我有一个用 Delphi 7 编写的旧版应用程序。我们正在向该应用程序添加新模块。这些模块使用 Visual Studio 2010、.NET 4、C# 编写,并通过 COM 公开给应用程序。
我已经成功定义了一个类,注册了程序集,导出了类型库,将类型库导入Delphi,在Delphi中创建了COM客户端并执行了模块。现在到了棘手的部分:我想将 another 对象(如上所述已定义-registered-exported-blah-blah-blah)作为参数传递给第一个模块上的方法。 p>
.NET
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("11111111-1111-1111-1111-AAAAAAAAAAAA")]
public interface IUserMaintenance
{
bool AssignUser(IUserInfo);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("11111111-1111-1111-1111-BBBBBBBBBBBB")]
public class UserMaintenance: IUserMaintenance
{
private IUserInfo _UserInfo;
public bool AssignUser(IUserInfo userInfo)
{
_UserInfo = userInfo;
LoadUser();
}
private void LoadUser()
{
//load user data from database using _UserInfo.UserName
}
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("22222222-2222-2222-2222-AAAAAAAAAAAA")]
public interface IUserInfo
{
void Initialize(string userName);
string UserName { get; }
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("22222222-2222-2222-2222-BBBBBBBBBBBB")]
public class UserInfo: IUserInfo
{
public string UserName { get; private set };
public void Initialize(string userName)
{
UserName = userName;
}
}
假设我有实现每个接口的单独类,并且包含这些类的程序集成功编译和注册,我将类型库导入 Delphi,创建 UserMaintenance_TLB.pas 和 UserInfo_TLB.pas。然而,我看到了一些意想不到的东西:虽然我在 .NET 中定义的接口存在(以“I”开头的接口),但 Delphi 生成了另一组接口(以“_”开头的接口)。 Delphi 完全不使用我在 .NET 中声明的 I 接口。
德尔福
UserMaintenance_TLB.pas
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
IUserMaintenance = interface;
IUserMaintenanceDisp = dispinterface;
_UserMaintenance = interface;
_UserMaintenanceDisp = dispinterface;
UserInfo_TLB.pas
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
IUserInfo = interface;
IUserInfoDisp = dispinterface;
_UserInfo = interface;
_UserInfoDisp = dispinterface;
Delphi 也创建了相应的 Delphi 类型:
// *********************************************************************//
// OLE Server Proxy class declaration
// Server Object : TUserMaintenance
// Help String :
// Default Interface: _UserMaintenance
// Def. Intf. DISP? : No
// Event Interface:
// TypeFlags : (2) CanCreate
// *********************************************************************//
TUserMaintenance = class(TOleServer)
private
FIntf: _UserMaintenance;
function GetDefaultInterface: _UserMaintenance;
protected
procedure InitServerData; override;
function Get_ToString: WideString;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect; override;
procedure ConnectTo(svrIntf: _UserMaintenance);
procedure Disconnect; override;
function Equals(obj: OleVariant): WordBool;
function GetHashCode: Integer;
function GetType: _Type;
WordBool AssignUser(IUserInfo userInfo);
property DefaultInterface: _UserMaintenance read GetDefaultInterface;
property ToString: WideString read Get_ToString;
published
end;
我想做的是创建一个 TUserMaintenance 实例,然后将一个 TUserInfo 实例传递给它。然而,有两件事是显而易见的:Delphi 类 TUserInfo 没有实现 IUserInfo,DefaultInterface 是 Delphi 生成的新接口 _UserInfo。我不能将我的 UserInfo 变量声明为 IUserInfo 类型,因为 TUserInfo 没有实现该接口。我也不能将其声明为 _UserInfo 类型,因为 UserMaintenance.LoadUser 需要一个 IUserInfo 实例。
诚然,这是我实际问题的一个非常简化的示例,但我认为它充分说明了问题。
所以我的问题是:有什么方法可以强制 Delphi 中的接口类型与 .NET 中声明的接口保持一致?还是有另一种方法可以将 UserInfo 的实例传递给 UserMaintenance?
【问题讨论】:
标签: delphi .net-4.0 c#-4.0 com-interop delphi-7