title: How to write test doubles for Windows API calls that pass arguments by address
body: <p>I have a number of working mock classes that replace actual calls to the Windows API.  However, I am struggling to put together something for a function that utilises <code>FormatMessageW</code> when using the <code>FORMAT_MESSAGE_ALLOCATE_BUFFER</code> flag.</p>
  
  <p>When using the <code>FormatMessageW</code> function I am first declaring my buffer as follows:</p>
  
  <pre><code>wchar_t * buffer = nullptr;
  </code></pre>
  
  <p>I then pass the buffer by address as the <code>lpBuffer</code> argument (expected type is <code>LPWSTR</code>):</p>
  
  <pre><code>reinterpret_cast&lt;::LPWSTR&gt;(&amp;buffer)
  </code></pre>
  
  <p>The Windows API function will automatically create a buffer of the correct size.</p>
  
  <p>I go on further with the buffer by stripping out line breaks, converting from wide characters to multibyte characters etc.</p>
  
  <p>In order to fully unit test the clean up of the output buffer I am attempting to mock the <code>FormatMessageW</code> call by having the function simply return a predefined string (which will be a member of the mock object).</p>
  
  <p>To simplify the problem, the following code attempts to replicate my problem:</p>
  
  <pre><code>// represents my mock class
  class mocker
  {
  
  public:
      // takes a wchar_t pointer and attempts to reassign it
      int mockFunction(wchar_t * buffer)
      {
          // assigns local copy of wchar_t pointer!
          buffer = &amp;message[0];
  
          return message.length();
      }
  
  protected:
  
      std::wstring message = L"test";
  };
  
  // test code
  mocker mocking;
  wchar_t * buffer = nullptr;
  auto size = mocking.mockFunction(&amp;buffer);
  
  // at this point buffer is still null
  // but I want the buffer to point to L"test"
  </code></pre>
  
  <p>Is there a way to achieve my aim of redirecting the pointer to an existing <code>std::wstring</code> without changing anything but the implementation of <code>int mockFunction(wchar_t * buffer)</code>?</p>