How to properly call IDispatch::Invoke with a required BSTR* parameter

后端 未结 1 776
猫巷女王i
猫巷女王i 2021-01-22 07:58

There are many examples of how to call IDispatch::Invoke with a BSTR* parameter. I have this working with many other \"SomeType*\" parameter but no matter what I try, I either

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 08:10

    You were all on the right track. @HansPassant comment pointed me to the "forehead-slap" "eureka" moment with his surmising about it blowing up on a different parameter. The 1st and 2nd parameters are of a different type. Passed in reverse for ::Invoke they are actually the 2nd and 3rd parameters. So it was actually blowing up on my "1st" parameter with a type mismatch.

    In my initial proof-of-concept version I was manually passing parameters and I was purposefully passing the parameters correctly in reverse order as required by IDispatch::Invoke. In the process of converting this to a more generic approach looping through an array of parameters passed from the caller I reversed the parameter order AFTER the call to IDispatch::Invoke when I am returning them to the calling app, however, I forgot to reverse them on the way in prior to the call to Invoke.

    Holy "you-know-what" the errors are so esoteric to someone without a ton of experience with this stuff!

    Once I fixed the order of parameters everything behaved exactly as expected. "Variation 1" from my question was, of course, the correct way to handle the BSTR* parameter. For clarity, here is the correct way to initialize a variant parameter for a BSTR* parameter being called by IDispatch::Invoke (in my case there are 2 other parameters that are not shown here)

    VARIANTARG* v = new VARIANTARG[3];
    //...Init my first 2 args IN REVERSE (not shown here)
    
    //Init my third arg which is the BSTR* parameter
    VariantInit(v[0]);
    BSTR val = SysAllocString(L"");
    v[0].vt = VT_BSTR | VT_BYREF;
    v[0].pbstrVal = &val;
    

    0 讨论(0)
提交回复
热议问题