Question

I have a class:

public class A
{
public delegate void SetTextDel(string value);
public void Test()
{
      SetTextDel setText = someInterface.SetText;
      icom.Set(setText);
}
}

[ComVisible(true), Guid("81C99F84-AA95-41A5-868E-62FAC8FAC263"),   InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface Icom
    {
        void Set(Delegate del);
    }

[ComVisible(true)]
    [Guid("6DF6B926-8EB1-4333-827F-DD814B868097")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(Icom))]
    public class B : Icom
    {
         Set(Delegate del)
         {
              del.DynamicInvoke("some text");
         }
    }

And I get targetinvocation exception in Set(Delegate del). Is there a better way to forward delegate as parameter? Or I making some mistake here that I not seeing. What am I trying to do is to pass someInterface.SetText this method as parameter. Thanks for helping.

Was it helpful?

Solution

The following works:

public class A
{
    public delegate void SetTextDel(string value);

    void TestSetText(string value)
    {
        MessageBox.Show(value);
    }

    public void Test(Icom icom)
    {
        SetTextDel del = TestSetText;
        icom.Set(del);
    }
}

[ComVisible(true), Guid("81C99F84-AA95-41A5-868E-62FAC8FAC263"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface Icom
{
    void Set(Delegate del);
}

[ComVisible(true)]
[Guid("6DF6B926-8EB1-4333-827F-DD814B868097")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(Icom))]
public class B : Icom
{
    public void Set(Delegate del)
    {
        del.DynamicInvoke("some text");
    }
}

private void buttonTest_Click(object sender, EventArgs e)
{
    var a = new A();
    var b = new B();

    a.Test(b);
}

Next, if you're looking to pass a callback function from C++ to Icom.Set, the following would work too:

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void SetTextDel([MarshalAs(UnmanagedType.BStr)] string value);

[ComVisible(true), Guid("81C99F84-AA95-41A5-868E-62FAC8FAC263"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface Icom
{
    void Set([MarshalAs(UnmanagedType.FunctionPtr)] SetTextDel del);
}

[ComVisible(true)]
[Guid("6DF6B926-8EB1-4333-827F-DD814B868097")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(Icom))]
public class B : Icom
{
    public void Set(SetTextDel del)
    {
        del("some text");
    }
}

Make sure to compile both of your C# and C++ projects as 32-bit code. The C++ callback function should be declared as such:

static HRESULT __stdcall SetTextDelCallback(BSTR value)
{
    return S_OK;
}

Finally, the proper way to implement this, IMO, is to simply define a callback interface (like ISetCallback below) and pass an object implementing such interface, rather than a delegate. ISetCallback can be implemented in any language, either C# or C++:

[ComVisible(true), Guid("2FE5D78D-D9F2-4236-9626-226356BA25E7")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISetCallback
{
    void OnSetText(string value);
}

public class A : ISetCallback
{
    public void OnSetText(string value) // ISetCallback
    {
        MessageBox.Show(value);
    }

    public void Test(Icom icom)
    {
        icom.Set(this);
    }
}

[ComVisible(true), Guid("81C99F84-AA95-41A5-868E-62FAC8FAC263"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface Icom
{
    void Set(ISetCallback callback);
}

[ComVisible(true)]
[Guid("6DF6B926-8EB1-4333-827F-DD814B868097")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(Icom))]
public class B : Icom
{
    public void Set(ISetCallback callback)
    {
        callback.OnSetText("some text");
    }
}

private void buttonTest_Click(object sender, EventArgs e)
{
    var a = new A();
    var b = new B();

    a.Test(b);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top