I nead to send an "interface" from C++ to a managed .NET object RRS feed

  • Question

  • I have a Win32 application that loads a C++ DLL. This C++ DLL is compiled with IJW support to let me call Managed objects.  This worksperfectly and there are no problems here.  My problem is the opposite way.  I nead, from my .NET (c#)code to call back to the Win32 program.  How do I manage this?  PInvoke and COM is out of the Question.  It must be using an Interface or soething like that.


    Ole Morten Heien
    HD Software

    ------------------------------------------- Ole Morten Heien HD Software / Advisor AS -------------------------------------------
    Monday, November 22, 2010 3:06 PM

All replies

  • Hello Ole Morten,


    1. >> I nead, from my .NET (c#)code to call back to the Win32 program. 

    1.1 There is a very advanced .NET technique that can be used to achieve this. It is well explained by Adam Nathan in his great book : .NET and COM - The Complete Interoperability Guide chapter 19.

    1.2 First, in whatever way you receive the C++ unmanaged function pointer, treat it as an IntPtr.

    1.3 To invoke the unmanaged function pointer in managed code, you have to use the MSIL calli instruction.

    1.4 Unfortunately, there is no built-in language support in C# that can generate such code using an IntPtr.

    1.5 One way around this limitation is to use Reflection Emit to dynamically emit a function that performs the calli instruction. After generating such a function, simply invoke it.

    1.6 If you are able to, acquire a copy of Adam Nathan's book and refer to Chapter 19 for details on how this can be written in C#.


    2. Here are some links that may be useful :


    The above link points to an example of using Reflection Emit to dynamically generate an assembly and a function within that assembly.


    The above link provides documentation on the calli MSIL instruction.


    - Bio.


    Monday, November 22, 2010 6:51 PM
  • Well, this sounds wery complicated and I cant understand that the only way is to call from C++ to .NEt and not the other way out - of the box.

    I mean, when I calll a .NET method I just type Object::MethodName().  I would be surpriced if there was no way to pass an c++ Interface/class to an object.  Of course, I hear what you say and I must admit, Im kind of disapointing of the IJW stuff if this is true.


    ------------------------------------------- Ole Morten Heien HD Software / Advisor AS -------------------------------------------
    Monday, November 22, 2010 10:35 PM
  • Hello Ole Morten,


    >> Well, this sounds wery complicated and I cant understand that the only way is to call from C++ to .NEt and not the other way out - of the box.

    1. I must hasten to add that Adam Nathan's book applied to the .NET 1.0 Framework. Hence things may have changed by now.

    2. Perhaps one of the Microsoft Staff or Moderators reading this post can help to clarify.

    - Bio.


    Tuesday, November 23, 2010 2:03 AM
  • lol, .NET 1.0 was releaseed - uhm - somewhere in 2002.  I really hope there have been some upgrades since then ;-)

    MS/Mod guys - You are very welcome to join this thread ;-)




    ------------------------------------------- Ole Morten Heien HD Software / Advisor AS -------------------------------------------
    Tuesday, November 23, 2010 6:44 AM
  • Hello Ole Morten,


    1. Note that the change I was referring to is in the area of language support, as in C#, VB.NET, etc, not in the area of low-level CLR functionality.

    2. That is, there may be new programming language support today to provide for invoking unmanaged functions (e.g. Win32 callback functions) without using PInvoke (albeit I'm not familiar with these).

    3. Back in the .NET 1.0 days, it was technically possible to achieve such an invokation but programmatically one has to use Reflection Emit to accomplish this.

    - Bio.


    Tuesday, November 23, 2010 4:15 PM
  • Hello Ole Morton,


    1. Here is a link to something you might find useful :

    1.1 It talks about the use of the Marshal.GetDelegateForFunctionPointer() function to invoke an unmanaged function. It appears to work well.

    1.2 I used Marshal.GetDelegateForFunctionPointer() to invoke an unmanaged functionA() (from my own unmanaged DLL) that returns the address of an internal (unexported) functionB().

    1.3 I could invoke functionB() from the C# code. Pretty cool.

    1.2 I'm not sure if it will solve your current issue (with the restriction of not using P/Invoke). But give it a try.


    Best of luck,



    Tuesday, November 23, 2010 6:12 PM
  • Ok. Using DLLImport is of course a solution here, but it complicates a lot because then I have to worry about linking LIB files etc.  You see, when I in my Clarion Win32 program define an Interface, I can pass this to the C++ DLL and then in C++ I can call directly to this interface.  I was hoping that I could send a C++ interface into my C# object and then let my C# program be able to call the methods in this interface.  If I understand you correct, this is actually not possible.  Isnt that a bit strange?  I mean, MS has created the IJW to let us call into the .NET world.  Does this mean that the parameter types here is restricted to simple types only?

    I was hoping I could send an interface, but I guess this is too much to ask for ;-)

    ------------------------------------------- Ole Morten Heien HD Software / Advisor AS -------------------------------------------
    Wednesday, November 24, 2010 10:22 AM



    Thank you for your question. We're doing research on this issue. It might take some time before we get back to you.

    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, November 27, 2010 2:44 AM
  • Well, seems to me like it would be easier to write COM visible code in C#

    Why not just code a c# interface.... ?


    Monday, November 29, 2010 4:14 PM
  • COM: requires COM Registration

    C# Interface:  How can that help?  How can a C# program use a C# interface to call into a C++ DLL?

    ------------------------------------------- Ole Morten Heien HD Software / Advisor AS -------------------------------------------
    Wednesday, December 1, 2010 7:46 AM