tags:

views:

89

answers:

1

i got one COM component and want to access from C#.I created a interface in my C# program and want to access that component with its CoClass.When i created a object it gives me __ComObject which is a RuntimeCallabelWrapper.

I want to know when it returns me a object and it create a MethodTable for me, then when i cast this interface to another interface to call method of other interface i adds method of second interface or create a new MethodTable for that? Below is the code:

[Guid("169D0491-E149-4BB3-932E-A42C4F8D3478")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface IInventory
{
    void Buy(int quantity, float rate);
    void Sell(int quantity, float rate);
    int GetStock();
}

[Guid("8B08A7ED-8D46-41B2-B8D9-35F4CF37F75B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAccount
{
    void Credit(double amount);
    void Debit(double amount);
    double GetBalance();
}


[Guid("F8E3E05A-CAFA-4CBD-BC42-DF9328729B01")]
[ComImport]
class Trader{}

public static void Main(string[] args)
{
    IInventory inv = (IInventory) new Trader(); // this gives me RCW
    inv.Buy(qty, 150);

    IAccount acc = (IAccount) inv;   // ?
    Console.WriteLine("Current balance: {0}", acc.GetBalance());
}
A: 

IInventory and IAccount are completely unrelated to each other (except that they are both inherited from IUnknown) and you can't cast from one to another. Unless class Trader doesn't implement both of them you can't cast it too.

sharptooth
You might be able to - COM allows for multiple inheritance
1800 INFORMATION
Does it apply specifically to this case?
sharptooth
There is not information to tell - you need to look to the object that is implementing the interface(s). You use QueryInterface to ask it what interfaces it supports
1800 INFORMATION
The casting in the code sample is a syntactic sugar for calling QueryInterface
1800 INFORMATION
I totally agree. Asked because I thought that you've noticed something that assured you of actual presence of multiple inheritance.
sharptooth