none
Pass BSTR from C# to IDISPATCH Method in VC++ COM RRS feed

  • Question

  • I've to call a VC++ COM method using InvokeMember(C#) has a BSTR* parameter. When I tried to use string[], I'm getting invalid type COM error. How to create the parameter equivalent to BSTR* in C# and pass it to InvokeMember. Thanks
    Sunday, January 8, 2012 5:14 PM

Answers

  • Hello SoccerFan1,

    1. I assume that the "params" BSTR pointer is an "out" parameter returned from the PerformAction() method to the caller.

    2. To pass any parameters by reference, you must use the version of the Type.InvokeMember() method that takes an array of ParameterModifier types.

    3. For the following COM method :

    STDMETHODIMP CTestCOMClass01::PerformAction(BSTR* params)
    {
    	// TODO: Add your implementation code here
    	*params = ::SysAllocString(L"Hello World");
    
    	return S_OK;
    }
    
    

    a sample C# code would be as follows :

    static void DoTest()
    {
        TestCOMClass01Class obj = new TestCOMClass01Class();
    
        object[] Parameters = new object[1];
        Parameters[0] = "";
    
        // Initialize a ParameterModifier with the number of parameters.
        ParameterModifier p = new ParameterModifier(1);
    
        // Pass the 1st parameter by reference.
        p[0] = true;
    
        // The ParameterModifier must be passed as the single element
        // of an array.
        ParameterModifier[] mods = { p };
    
        obj.GetType().InvokeMember("PerformAction", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, obj, Parameters, mods, null, null);
    
        Console.WriteLine("{0:S}", Parameters[0]);
    }
    
    


    - Bio.

     


    Please visit my blog : http://limbioliong.wordpress.com/
    • Proposed as answer by Paul Zhou Monday, January 9, 2012 6:26 AM
    • Marked as answer by Paul Zhou Monday, January 16, 2012 4:15 AM
    Monday, January 9, 2012 6:20 AM

All replies

  • In order to receive a BSTR (BSTR*), you pass a StringBuilder (I think that's the name; if not it is StringStream -sorry about the mixup in my head between .net and C++).  Is this what you are looking for?
    Jose R. MCP
    Sunday, January 8, 2012 6:25 PM
  • I tried stringbuilder and Memorystream, didn't work. Anything else I could try? Thanks
    Sunday, January 8, 2012 10:42 PM
  • Show the COM object's IDL.
    Jose R. MCP
    Monday, January 9, 2012 1:41 AM
  • In the ODL file

    [id(1)] boolean PerformAction(BSTR* params);

     

     

     

    Thanks

    Monday, January 9, 2012 3:19 AM
  • Hello SoccerFan1,

    1. I assume that the "params" BSTR pointer is an "out" parameter returned from the PerformAction() method to the caller.

    2. To pass any parameters by reference, you must use the version of the Type.InvokeMember() method that takes an array of ParameterModifier types.

    3. For the following COM method :

    STDMETHODIMP CTestCOMClass01::PerformAction(BSTR* params)
    {
    	// TODO: Add your implementation code here
    	*params = ::SysAllocString(L"Hello World");
    
    	return S_OK;
    }
    
    

    a sample C# code would be as follows :

    static void DoTest()
    {
        TestCOMClass01Class obj = new TestCOMClass01Class();
    
        object[] Parameters = new object[1];
        Parameters[0] = "";
    
        // Initialize a ParameterModifier with the number of parameters.
        ParameterModifier p = new ParameterModifier(1);
    
        // Pass the 1st parameter by reference.
        p[0] = true;
    
        // The ParameterModifier must be passed as the single element
        // of an array.
        ParameterModifier[] mods = { p };
    
        obj.GetType().InvokeMember("PerformAction", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, obj, Parameters, mods, null, null);
    
        Console.WriteLine("{0:S}", Parameters[0]);
    }
    
    


    - Bio.

     


    Please visit my blog : http://limbioliong.wordpress.com/
    • Proposed as answer by Paul Zhou Monday, January 9, 2012 6:26 AM
    • Marked as answer by Paul Zhou Monday, January 16, 2012 4:15 AM
    Monday, January 9, 2012 6:20 AM