【发布时间】:2014-01-28 20:31:23
【问题描述】:
我被要求为一个旧的 delphi 程序创建一个 .Net dll。我正在尝试使用 COM Callable Wrapper 来执行此操作,但是当它尝试加载 dll 时,我一直收到错误消息(很笼统,例如“我无法加载 dll”)。以下是技术文档的内容:
The DLL only needs to export one function under the name 'AUTHORIZE'.
function Authorize(InXml: PChar): PChar; stdcall;
(Delphi syntax. May be different in other languages.)
这是我的 CCW 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ComCallableWrapper
{
[Guid("C3FD922A-FB44-47B1-9C0C-8F7FAF57098B")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IAuthorizer
{
[DispId(1)]
string Authorize(string lnpInXml);
}
[ProgId("ComCallableWrapper.Authorizer")]
[ClassInterface(ClassInterfaceType.None)]
public class Authorizer : IAuthorizer
{
public Authorizer()
{
}
public string Authorize(string lnpInXml)
{
return "Approved!";
}
}
}
我还在运行 delphi 程序的计算机上运行此命令“regasm /tlb:ComCallableWrapper.tlb ComCallableWrapper.dll /codebase”。
我一直在google上做一些关于delphi如何调用dll上的函数的研究,我发现至少有两种方式:
function Authorize(lnpInXml: pchar): pchar; stdcall; external 'DLLName.dll';
和
oleObject := CreateOleObject('ComCallableWrapper.Authorizer');
ShowMessage(oleObject.Authorize('Approved?'));
看起来 COM 的工作方式有点不同。有没有办法让我的 CCW 像第一种方式一样工作?
问候。
【问题讨论】:
-
第一种方法看起来像是调用平面 C DLL,而不是 COM DLL。您将无法在 .NET 中创建这样的 DLL。
-
实例化 COM DLL 不起作用的可能原因有 50 亿个。为了让它变得有趣,微软让它们都返回基本相同的错误代码。您可以使用 Microsoft OLE/COM 对象查看器实例化您的对象吗?还是使用 .NET 代码?在处理 Delphi 方面的问题之前从那里开始。