none
How can I trigger a property's setter from unmanaged C++ code RRS feed

  • Question

  • Is there an easy way I can trigger a property setter from unmanaged code?

    I have a property :

    Dim _pcVirtualMemFolder As String _
          = "%GRASP3_DATAPATH%Meta\%G3_FileNameOnly%\"
    Property m_pcVirtualMemFolder As String
        Get
            return _pcVirtualMemFolder
        End Get
        Set(value As String)
            _pcVirtualMemFolder = value
        End Set
    End Property

    Can I make a delegate to the setter and send the function pointer to C++ unmanaged code?

    I can do this for a another function in my VB code:

    Public Delegate Sub pfnStringReturnsVoid_t _
    (ByVal strRunStatus As String) <DllImport(MMGRASPDLL)> Public Shared Function mmGrasp_UpdateMsg _ (ByVal pfnRunStatus As pfnStringReturnsVoid_t) As Int32 End Function

    ...

    ' Give the C++ code a function pointer
    GraspLib.mmGrasp_UpdateMsg(g_vi, AddressOf myFrmGrasp.MyMsg)

    How do I make a Delegate for the property setter?



    //========================== // Michael Fitzpatrick //==========================




    Wednesday, February 5, 2014 11:04 AM

Answers

  • If the type of myFrmGrasp is MyClass1, then in order to get the delegate of setter try this:


    Dim s As MethodInfo = GetType(MyClass1).GetMethod("set_m_pcVirtualMemFolder")
    Dim d As pfnStringReturnsVoid_t = s.CreateDelegate(GetType(pfnStringReturnsVoid_t), myFrmGrasp)
    
    ' A test:
    d.DynamicInvoke("abc") ' call setter 

    If you need the address, then use Marshal.GetFunctionPointerForDelegate(d).

    Wednesday, February 5, 2014 1:48 PM