Wednesday 15 January 2014

c++ - When to use SafeArrayAccessData to lock a SAFEARRAY -


I have a question about using SafeArrayAccessData to lock SAFEARRAY, which has been passed by managed code . Our code here is the diverse code passed with a string array managed code. During the review of the code, someone suggests using SafeArrayAccessData / SafeArrayUnAccessData, but he is not sure about why and what are the benefits. Can you share some of your experiences? Thanks!

  STDMETHODIMP base :: method 1 (different values, VARIANT_BOOL results) {CComSafeArray & lt; BSTR & gt; Id; Ids.Attach (values.parray); Unsigned full size = id Gatecount (); (Unsigned int i = 0; i & lt; size; ++ i) {// use id [i] here} // ...}    

Well, always :) You need to get a reference to array content.

But you use a friendly C ++ wrapper class. CComSafeArray & lt; & Gt; The template already does this for you, so you should not help. It uses SafeArrayLock () in Attach () method, which provides secure error access to secure content such as Index. And automatically unlock with your destructor, it runs at the end of your method. Locking otherwise makes sure that the array is thread-safe and you can not be removed while accessing it. Your current code has a very low risk, but it fits perfectly into the better-safe-sorry principles of automation.

No comments:

Post a Comment