初始情況
我之前使用了 .NET Framework 中的組件物件模型 ( COM ),以便在 Delphi 中使用我的 C# 類別庫,如本YouTube教程中所示,效果非常好。但是,我現在想改用 .NET Core 6。
按照微軟官方教程“Expose .NET Core components to COM”,我創建了這個介面
using System.Runtime.InteropServices;
[ComVisible(true)]
[Guid(6775d93d-cf14-42b9-83db-425bac0acf4d)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServer
{
public string getHelloWorld();
}
和這堂課。
using System.Runtime.InteropServices;
[ComVisible(true)]
[Guid(d62bf794-d29e-4fcd-8637-994490264a93)]
public class Server : IServer
{
public string getHelloWorld()
{
return "Hello World";
}
}
在構建專案之后,我執行了這個命令來向 COM 注冊我所有暴露的 .NET 物件。
regsvr32 MyProjectName.comhost.dll
The command executed successfully. I now tried to import the created .dll file in Delphi, like I did before with the class libraries written in .NET Framework (See timestamp of YouTube tutorial). However, Delphi runs into an error because it can't load the type library.
Edit 1: Godzilla-type breaking change
According to the official documentation...
Unlike in .NET Framework, there is no support in .NET Core or .NET 5 for generating a COM Type Library (TLB) from a .NET assembly. The guidance is to either manually write an IDL file or a C/C header for the native declarations of the COM interfaces. If you decide to write an IDL file, you can compile it with the Visual C SDK's MIDL compiler to produce a TLB.
... there is no support in .NET Core for generating a typelib.
Edit 2: MIDL compiler
However, there is still a way to manually produce a TBL file by using the MIDL compiler. As you can read up in the official documentation, the MIDL compiler processes an IDL file to generate a type library. With that beeing said, I created this IDL file by using this guide.
[
uuid(a93d03e0-9adf-4a2d-93e3-0ccfc2d8e1a6),
lcid(0x0409),
version(1.0),
]
library MyProjectName
{
importlib("stdole.tlb");
[
uuid(16292ce5-ccd1-405d-9a70-ace42152bb15),
oleautomation,
dual
]
interface IServer: IUnknown {
HRESULT getHelloWorld([out, string] IServer** string);
};
};
However, I get an error when I execute the following command to generate the .tlb file.
midl myProjectName.idl /tlb myProjectName.tlb
> midl : command line error MIDL1005 : cannot find C preprocessor cl.exe
Issue: I searched for cl.exe on my Computer but couldn't find anything.
Q.: Does anybody have an idea on how I could solve my problem? I highly appreciate any kind of help, cheers!
uj5u.com熱心網友回復:
如果您不介意切換到不同的方法,您可以使用 NativeAOT 從 .Net 編譯本機庫。你可以在這里找到一個例子
它似乎計劃在 .Net 7 中可用,但您已經可以開始在 .Net 6 中使用它。
uj5u.com熱心網友回復:
使用 MVC 創建 API 并從 Delphi 應用程式呼叫它不是更容易嗎?這樣您就可以避免 DLL 的所有問題。
uj5u.com熱心網友回復:
也許這個工具會幫助你:
https ://github.com/dspace-group/dscom
C:\> dotnet tool install dscom -g
C:\> dscom tlbexport myassembly.dll
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/443938.html
