【发布时间】:2017-04-09 12:02:03
【问题描述】:
我对 Delphi 很陌生。我们尝试将 C++ 或 C# dll 导入 Delphi。我们的要求是将库作为 COM 对象导入。由于 DLL 中有多个类和结构,我们不希望它 P/Invoke 方法。 我们尝试用 C++/C# 和 MFC ActiveX(ocx 组件)构建一个 DLL。 我一直在关注这些链接来创建一个 DLL:
- http://www.codeproject.com/Articles/505791/Writing-Simple-COM-ATL-DLL-for-VS
- https://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-net-com-assembly/
- https://john.nachtimwald.com/2012/04/08/wrapping-a-c-library-in-comactivex/
和这个链接 (http://wiki.freepascal.org/LazActiveX) 来导入我创建的 TLIB。 我创建的所有 DLL 都到了我在 Delphi 中创建对象的停止点。
我在 C# 中创建的是:
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace CSharpSampleLib
{
[ComVisible(true),
Guid("CDBFD892-7473-4AC4-8B44-D75A828599AD")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ICSharpCom
{
[DispId(1)]
void Init();
[DispId(2)]
int AddNumbers(int num1, int num2);
[DispId(3)]
String StringConcat(String num1, String num2);
}
[Guid("D51F086B-B593-436D-8900-92CDC1E427CE"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface CSharpCom_Events
{
}
[Guid("4AB74C5C-6F3C-4B15-9E00-5174551B50A2"),
ClassInterface(ClassInterfaceType.None)]
[ProgId("Sample.CSharpCom")]
[ComVisible(true)]
public class CSharpCom : ICSharpCom
{
public CSharpCom() { }
public void Init() { }
public int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
public String StringConcat(String str1, String str2)
{
return str1 + str2;
}
}
}
我们尝试了两个选项来调用我们导入的类型库。
program CSharpDemo;
uses comobj, CSharpSampleLib_1_0_TLB;
var
objCsLib1:ICSharpCom;
objCsLib2:OLEVariant;
begin
objCsLib1:=CoCSharpCom.Create; // Method 1
objCsLib2:= CreateOleObject('CSharpSampleLib.CSharpCom'); //Method 2
end.
我们确保该库已注册并在 Wow6432Node>CLSID> 中找到了 GUID
我希望这是创建对象的正确方法。我可以知道我在这里缺少什么吗?
【问题讨论】:
-
错误信息已经够清楚了吧?
-
@DavidHeffernan 感谢您的评论。但我认为并不清楚错误,我认为该类已注册。
-
系统没有这样做。为什么不呢。
-
顺便说一句,你似乎没有在任何地方初始化 com
-
@geisterfurz007。谢谢!下次我会确定的