2011-08-28 108 views
5

當我創建了這個簡單的類一個COM互操作的.dll:自動化錯誤實例化一個.net COM可見的類

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給了我對對象的intellisense。

Private Sub Form_Load() 
    Dim score1 As ComWeightedScore 

    Set score1 = New ComWeightedScore 
    score1.Score = 500 

End Sub 

在行Set score1=new ComWeightedScore上引發異常自動化錯誤。

它不可能比這更簡單...錯誤在哪裏?

+0

爲什麼你說這個錯誤是在分配int還是long?構造函數調用失敗了。如果您從構造函數的主體中刪除賦值,它仍會失敗嗎?此外,提供更多的信息和細節你得到的錯誤。 –

回答

7

你忘了/代碼庫選項在Regasm.exe命令行中。

如果沒有它,您必須使用gacutil.exe將程序集強名稱並放入GAC。客戶端機器上的好主意,只是不在你的機器上。

3

如果您正在運行64位處理器並將項目編譯爲'CPU-Any',則需要僅針對x86進行編譯或在64位COM +空間中註冊dll。包括32位和64位的regasm

實施例:

%WINDIR%\ Microsoft.NET \框架\ v4.0.30319 \ regasm 「Contoso.Interop.dll」 /tlb:Contoso.Interop.tlb /代碼庫的Contoso .Interop

%WINDIR%\ Microsoft.NET \ Framework64 \ v4.0.30319 \ regasm 「Contoso.Interop.dll」 /tlb:Contoso.Interop.tlb /代碼庫Contoso.Interop