none
Type.InvokeMember returns Nothing (null) RRS feed

  • Question

  • Here is the code snippet (simplified);

    dim retVal as Object = 0

    retVal = MyObject.GetType().InvokeMember("TestMethod", BindingFlags.InvokeMethod, Nothing,

    MyObject, args)

    MyObject is inProc COM dll component.

    TestMethod return S_OK or S_FAIL

    The method is executed (I put a diagnostic Message Boxes), but the returned object is always Nothing.

    I would appreciate any suggestion on how to troubleshoot this issue

    Regards,

    Michael


    Monday, September 24, 2012 7:38 PM

Answers

  • Hello Michael,

    1. The return value from the Type.InvokeMember() method is meant to contain the return value of a COM dispinterface method.

    2. Such a return value is always designated with the IDL attributes [out, retval], e.g. :

    [id(2), helpstring("method TestMethod2")] HRESULT TestMethod2([out,retval] BSTR* strRet);

    2.1 In the above TestMethod2() example, the return value of such a method is a BSTR.

    2.2 Hence if the VB client code is as follows :

    Dim retVal2 As Object = 0
    retVal2 = MyObject.GetType().InvokeMember("TestMethod2", BindingFlags.InvokeMethod, Nothing, MyObject, Nothing)

    then retVal2 will contain the returned string.

    3. In your example, TestMethod() does not return any [out, retval] value. It is possibly declared as follows :

    [id(1), helpstring("method TestMethod")] HRESULT TestMethod(void);

    4. The actual HRESULT value is absorbed into the low-level intermediate language code and will not be returned to the caller.

    5. To determine the HRESULT returned from a COM method other than S_OK, use Try and Catch.

    5.1 For example, if TestMethod returns E_FAIL, then the VB code can be written as follows :

    Dim retVal As Object = 0
    Try
      retVal = MyObjectObj.GetType().InvokeMember("TestMethod", BindingFlags.InvokeMethod, Nothing, MyObjectObj, Nothing)
    Catch ex As Exception
      Console.WriteLine(ex.InnerException.Message)
    End Try

    5.2 You will get a message as follows :

    Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

    - Bio.


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

    Tuesday, September 25, 2012 5:18 AM

All replies

  • Hello Michael,

    1. The return value from the Type.InvokeMember() method is meant to contain the return value of a COM dispinterface method.

    2. Such a return value is always designated with the IDL attributes [out, retval], e.g. :

    [id(2), helpstring("method TestMethod2")] HRESULT TestMethod2([out,retval] BSTR* strRet);

    2.1 In the above TestMethod2() example, the return value of such a method is a BSTR.

    2.2 Hence if the VB client code is as follows :

    Dim retVal2 As Object = 0
    retVal2 = MyObject.GetType().InvokeMember("TestMethod2", BindingFlags.InvokeMethod, Nothing, MyObject, Nothing)

    then retVal2 will contain the returned string.

    3. In your example, TestMethod() does not return any [out, retval] value. It is possibly declared as follows :

    [id(1), helpstring("method TestMethod")] HRESULT TestMethod(void);

    4. The actual HRESULT value is absorbed into the low-level intermediate language code and will not be returned to the caller.

    5. To determine the HRESULT returned from a COM method other than S_OK, use Try and Catch.

    5.1 For example, if TestMethod returns E_FAIL, then the VB code can be written as follows :

    Dim retVal As Object = 0
    Try
      retVal = MyObjectObj.GetType().InvokeMember("TestMethod", BindingFlags.InvokeMethod, Nothing, MyObjectObj, Nothing)
    Catch ex As Exception
      Console.WriteLine(ex.InnerException.Message)
    End Try

    5.2 You will get a message as follows :

    Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

    - Bio.


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

    Tuesday, September 25, 2012 5:18 AM
  • Thanks a lot Lim. I added another param (with [out, retval] attribute) to that method, made a few changes and got it to work.

    Michael

    Tuesday, September 25, 2012 6:19 AM