Dot-Net
實例化 .Net COM 可見類時出現自動化錯誤
我用這個簡單的類創建了一個 COM-interop .dll:
using System.Runtime.InteropServices; namespace ClassLibrary1 { [ComVisible(true)] [Guid("795ECFD8-20BB-4C34-A7BE-DF268AAD3955")] public interface IComWeightedScore { int Score { get; set; } int Weight { get; set; } } [ClassInterface(ClassInterfaceType.None)] [Guid("9E62446D-207D-4653-B60B-E624EFA85ED5")] public class ComWeightedScore : IComWeightedScore { private int _score; public int Score { get { return _score; } set { _score = value; } } private int _weight; public int Weight { get { return _weight; } set { _weight = value; } } public ComWeightedScore() { _score = 0; _weight = 1; } }我註冊它使用: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm C:\ComClasses\Classlibrary1.dll /tlb: Classlibrary1.tlb
最後,我成功添加了對 .dll 的引用,之後 VB6 為我提供了對象的智能感知。
Private Sub Form_Load() Dim score1 As ComWeightedScore Set score1 = New ComWeightedScore score1.Score = 500 End Sub線上
Set score1=new ComWeightedScore上引發異常自動化錯誤。它幾乎不能比這更簡單……錯誤在哪裡?!
您忘記了 Regasm.exe 命令行中的 /codebase 選項。
如果沒有它,您將不得不對程序集進行強命名並使用 gacutil.exe 將其放入 GAC。客戶端機器上的好主意,而不是你的。
如果您在 64 位處理器上執行並且您的項目編譯為“CPU-Any”,您將需要僅針對 x86 進行編譯或在 64 位 COM+ 空間中註冊 dll。
32 位和 64 位 regasm 的範例:
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm “Contoso.Interop.dll” /tlb:Contoso.Interop.tlb /codebase Contoso.Interop
%windir%\Microsoft.NET\Framework64\v4.0.30319\regasm “Contoso.Interop.dll” /tlb:Contoso.Interop.tlb /codebase Contoso.Interop