3

I have a COM object in C# and a Silverlight application(escalated privileges) which is a client to this COM object.

COM object:

[ComVisible(true)]
public interface IProxy
{
    void Test(int[] integers);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Proxy : IProxy
{
    [ComVisible(true)]
    public void Test(int[] integers)
    {
        integers[0] = 999;
    }    
}

Silverlight client:

dynamic proxy = AutomationFactory.CreateObject("NevermindComProxy.Proxy");

int[] integers = new int[5];
proxy.Test(integers);

I excpect the integers[0] == 999, but the array is intact.

How to make the COM object modify the array?

UPD Works for non-silverlight apps. Fails for Silverlight. How to fix for silverlight?

3
  • Why are you using COM as an interoperability engine between 2 .NET assemblies? Maybe you should use Assembly.Load instead? Commented Sep 20, 2010 at 11:23
  • The goal is to use native code from silverlight application. So I do silverlight -> COMProxy -> native code. As far as I understand silverlight can't pinvoke native code from its sandbox. Please, correct me if I'm wrong. Commented Sep 20, 2010 at 11:34
  • this is beyond my knowledge, sorry. Good luck in finding an answer! Commented Sep 21, 2010 at 8:44

1 Answer 1

1

The short answer is you need to pass the array by ref (see the note in AutomationFactory just above the example [arrays are passed by value in C#]) - The problem then is, SL will barf with an argument exception when you call proxy.Test(ref integers) (I don't why). The work around is that SL will pass an array by ref if the method takes an object by ref, so this works...

[ComVisible(true)]
public interface IProxy
{
    void Test( ref object integers);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Proxy : IProxy
{
    [ComVisible(true)]
    public void Test(ref object intObj)
    {
        var integers = (int[])intObj;
        integers[0] = 999;
    }
}

And with the SL code adding ref like:

dynamic proxy = AutomationFactory.CreateObject("NevermindComProxy.Proxy");

var integers = new int[5];
proxy.Test( ref integers);

Drop the ref from either the caller or the interface definition and it won't update the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.