2017-03-28 17 views
0

私は現在、C#とCOMインターフェイスを掘り下げています。 C#はCOMの後に来たので、C#のドキュメントはまばらです(おそらく私たちはそれを修正することができます)。私はthe C# compiler can give informative error messagesを発見しました。エラーメッセージからメソッドシグネチャのC#構文バージョンを読み取り、クラスに追加することができます。これはIAdviseSinkで機能しましたが、IBindCtxでは機能しません。C#でクラシックCOMインターフェイスIBindCtxを宣言するには?

私は[MarshalAs(UnmanagedType.LPWStr)]が動作するはず使用してその2バイトベースの文字列をNULL終端であるC++での構文はHRESULT RevokeObjectParam([in] LPOLESTR pszKey);LPOLESTRあるRevokeObjectParam(string a)最後の方法のためのエラーを取得しています。しかし、エラーメッセージが表示されます。

 * Class1.cs(25,14,25,40): error CS0539: 'IBindCtx.RevokeObjectParam' in explicit interface declaration is not a member of interface 
    * Class1.cs(6,18,6,24): error CS0535: 'ComInterfacesInCSharp.Class1' does not implement interface member 'System.Runtime.InteropServices.ComTypes.IBindCtx.RevokeObjectParam(string)' 

どうすれば修正できますか? Visual Studioに完全なコードをコピーすることができます(クラスライブラリプロジェクトを作成する)。

using System.Runtime.InteropServices.ComTypes; 
using System.Runtime.InteropServices; 

namespace ComInterfacesInCSharp 
{ 
    public class Class1 : System.Runtime.InteropServices.ComTypes.IBindCtx 
    { 
     void IBindCtx.RegisterObjectBound(object obj) { } 
     void IBindCtx.RevokeObjectBound(object obj) { } 
     void IBindCtx.ReleaseBoundObjects() { } 
     void IBindCtx.SetBindOptions(ref System.Runtime.InteropServices.ComTypes.BIND_OPTS opts) { } 
     void IBindCtx.GetBindOptions(ref System.Runtime.InteropServices.ComTypes.BIND_OPTS opts) { } 
     void IBindCtx.GetRunningObjectTable(out System.Runtime.InteropServices.ComTypes.IRunningObjectTable tab) { } 
     void IBindCtx.RegisterObjectParam(string s, object obj) { } 
     void IBindCtx.GetObjectParam(string s, out object obj) { } 
     void IBindCtx.EnumObjectParam(out System.Runtime.InteropServices.ComTypes.IEnumString enumString) { } 

     /* Problem here https://msdn.microsoft.com/en-us/library/windows/desktop/ms693771(v=vs.85).aspx 
     * C++ Syntax is 
     * HRESULT RevokeObjectParam([in] LPOLESTR pszKey); 
     * LPOLESTR is a null terminated 2 byte based string so UnmanagedType.LPWStr ought to work 
     * 
     */ 
     //void IBindCtx.RevokeObjectParam(string a) { } 
     void IBindCtx.RevokeObjectParam([MarshalAs(UnmanagedType.LPWStr)] string a) { } 

     /* 
     * Compile errors are 
     * Class1.cs(25,14,25,40): error CS0539: 'IBindCtx.RevokeObjectParam' in explicit interface declaration is not a member of interface 
     * Class1.cs(6,18,6,24): error CS0535: 'ComInterfacesInCSharp.Class1' does not implement interface member 'System.Runtime.InteropServices.ComTypes.IBindCtx.RevokeObjectParam(string)' 
     */ 
    } 
} 

ちなみに、C#でこれらのインターフェイスを詳しく説明しているWebリソースがあれば、それは素晴らしいことでしょう!

+0

あなたが尋ねるものは明確ではありません。つまり、インタフェースメソッドRevokeObjectParamは 'int'を返します。参照してください:[IBindCtx](https://referencesource.microsoft.com/#mscorlib/system/runtime/interopservices/ComTypes/ibindctx.cs,83034c257e1bd273) – TnTinMn

+0

@TnTinMn:あなたが与えたリンクは素晴らしいです!それは私の質問に答えるもので、正しい方法の署名であった。今私は確かにマイクロソフトからのものです。 –

答えて

1

あなたが与えたリンクは素晴らしいです!それは私の質問に答えるもので、正しい方法の署名であった。今私は確かにマイクロソフトからのものです。

ソースコードの定義の多くはMicrosoft's Reference Source siteですが、アセンブリで定義されているメソッドのシグネチャを検出するための唯一のオプションではありません。

Visual Studio's Object Browserを学んでください。表示メニュー - >オブジェクトエクスプローラ(またはF2キーを押します)。

インターフェイスを実装する特別なケースでは、Visual Studioにインターフェイスを自動的に実装させることができます。インターフェイス名を右クリックし、「Implement Interface」を選択します。

enter image description here

関連する問題