title: Writing into stream passed to a DLL
body: <p>I have an issue writing into a stream, passed as a FILE* to a function which is contained in a DLL. Compiler: Visual C++, all versions 2005 to 2015. The precise errors vary; the VS 2015 version is given below. The problem is identical for Win32 and x64 targets.</p>
  
  <p>Trying to boil down the problem, consider this function:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void TestPrintf(FILE *TestFile)
  {
      fprintf(TestFile, "Hello World");
  }
  </code></pre>
  
  <p>Now, another function will be calling the above function in the following way:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void TestPrintf(FILE *TestFile);
  void AnyFunction( void )
  {
      FILE *TestFile;
      fopen_s( &amp;TestFile, "PrintTest.txt", "wt");
      TestPrintf(TestFile);
  }
  </code></pre>
  
  <p>The first function is part of a DLL. Depending on  the settings in
  Properties /  C/C++  / Code Generation   during compilation of both the DLL and the program containing the caller, I get the following error messages:</p>
  
  <p>Caller compiled for Multithread Debug (static):<br>
      DLL compiled for either Multithread Debug (static) or Multithread Debug DLL:  </p>
  
  <pre><code>    Debug assertion failed   
      File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp  
      Line: 980  
      Expression: __acrt_first_block == header  
  </code></pre>
  
  <p>DLL compiled for Multithread (static):  </p>
  
  <pre><code>    Debug assertion failed in the same file as before, but now  
      Line: 892  
      Expression: is_block_type_valid(header-&gt;block_use)  
  </code></pre>
  
  <p>DLL compiled for Multithread DLL:  </p>
  
  <pre><code>    Again another variant of the same. Now it is:  
      Line: 888  
      Expression: _CrtlsValidHeapPointer(block)  
  </code></pre>
  
  <p>Caller compiled for Multithread Debug DLL:<br>
      DLL compiled for either Multithread (static) or Multithread Debug (static):  </p>
  
  <pre><code>    same messages as above  
  </code></pre>
  
  <p>DLL compiled for Multithread DLL:  </p>
  
  <pre><code>    Yet another variant of the same. Now it is:  
      HEAP[FilePrintTest.exe]: Invalid address specified to RtlValidateHeap( 
      01060000, 0107B7A0 )
  </code></pre>
  
  <p>DLL compiled for Multithread Debug DLL:  </p>
  
  <pre><code>    This is error-free.  
  </code></pre>
  
  <p>Caller compiled for Release (either Multithread or Multithread DLL):  </p>
  
  <pre><code>all four versions of the DLL go error-free.  
  </code></pre>
  
  <p>The difference between VS versions seems to be that VS 2015 first writes the output as intended and crashes afterwards, whereas VS 2005 crashes already while writing the output.     </p>
  
  <p>If the function <B>TestPrintf</B> is not in a DLL, but in a static library, there is no such problem (of course, there will be linker messages about library collisions, but the test program works nonetheless). However, unfortunately, I do need the writing function inside the DLL, and I must be able to call it from anywhere.  </p>
  
  <p>All of the above applies also to the case that the caller is in a console programme and the stream is <I>stdout</I>.  </p>
  
  <p>So my question is: Is there a stupid mistake on my side? If not, is there a way to work around this problem?</p>
  
  <p>I would be grateful for suggestions!<br>
  Martin</p>