IDispatchを介して古いスクールのCOMオブジェクトに接続するアプリをC#で作成しています。私はこの種のコードでこれを行います:
public sealed class Attachments
{
Object comObject;
Type type;
private readonly static Attachments _instance = new Attachments();
public static Attachments Instance { get { return _instance; } }
private Attachments()
{
type = Type.GetTypeFromProgID("WinFax.Attachments");
if (type == null)
throw new ArgumentException("WinFax Pro is not installed.");
comObject = Activator.CreateInstance(type);
}
public Int16 Count()
{
Int16 x = (Int16) type.InvokeMember("Count",
BindingFlags.InvokeMethod,
null,
comObject,
null);
return x;
}
....
このIDispatchインターフェイスのメソッドの1つは、LPDISPATCHを返します。これは、IDispatchへのロングポインターです。これは別のCOMオブジェクト、ProgIdWinFax.Attachmentです。(WinFax.Attachmentsは、WinFax.Attachmentオブジェクトのコレクションを管理します。)
C#で、そのLPDISPATCHに対応するCOMオブジェクトのメソッドを呼び出すにはどうすればよいですか?私はこのようなことをすることができますか?
Object o = type.InvokeMember("MethodReturnsLpdispatch",
BindingFlags.InvokeMethod,
null,
comObject,
null);
Type t2 = Type.GetTypeFromProgID("WinFax.Attachment"); // different ProgId !!
Object x = t2.InvokeMember("MethodOnSecondComObject",
BindingFlags.InvokeMethod,
null,
o,
null);