none
Creating an event in a native c dll and handling in managed vb.dot RRS feed

  • Question

  • Has anyone actually got an event passed from a c++ dll (native) to an event handler in vb.net ?

    How do you raise them in the dll, and how do you define the reception end in vb.net (it's also compact framework)

    Anyone any ideas or examples ?

    Thursday, April 26, 2012 12:03 PM

Answers

  • Hello chrispink,

    1. One way you can achieve this is via the use of callback functions involving managed delegates.

    2. The delegate serves as a managed function that gets called by unmanaged code (C++).

    3. A pointer to the delegate function can be obtained by using Marshal.GetFunctionPointerForDelegate().

    4. You have to create an exported C++ function to pass the pointer to the managed delegate function to unmanaged code.

    5. The unmanaged code can then invoke the function just like any function pointer.

    6. An example of this can be found here :

    Delegates As Callbacks Part 2

    http://limbioliong.wordpress.com/2011/06/19/delegates-as-callbacks-part-2/

    7. The code example in the above link, however, uses C# and not VB.net.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by The Thinker Thursday, April 26, 2012 7:33 PM
    • Marked as answer by chrispink Friday, April 27, 2012 7:02 AM
    Thursday, April 26, 2012 1:50 PM

All replies

  • Hello chrispink,

    1. One way you can achieve this is via the use of callback functions involving managed delegates.

    2. The delegate serves as a managed function that gets called by unmanaged code (C++).

    3. A pointer to the delegate function can be obtained by using Marshal.GetFunctionPointerForDelegate().

    4. You have to create an exported C++ function to pass the pointer to the managed delegate function to unmanaged code.

    5. The unmanaged code can then invoke the function just like any function pointer.

    6. An example of this can be found here :

    Delegates As Callbacks Part 2

    http://limbioliong.wordpress.com/2011/06/19/delegates-as-callbacks-part-2/

    7. The code example in the above link, however, uses C# and not VB.net.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by The Thinker Thursday, April 26, 2012 7:33 PM
    • Marked as answer by chrispink Friday, April 27, 2012 7:02 AM
    Thursday, April 26, 2012 1:50 PM
  • You may want ot explain what you are trying to do because there may be better ways of doing it.  What you want can be done but two things must happen

    1)  The event handler has to be in static memory with public access.

    2)  You must pass a pointer to the event handler to the dll every time you call the dll.

    A dll doesn't stay resident in memory.  After a dll finishes excuting it is no longer in memory and everytime you call the dll it get reload in memory.  You can get a dll to stay in memory but you have to load the dll into memory before calling the dll.  Your program would havve to read the binary dll file into allocated inside you program.  The task is not trivial.


    jdweng

    Thursday, April 26, 2012 1:51 PM