title: C writing to file from dll, with file opened in application
body: <p>I have a file pointer exported from a dll, which is initialized(fopen) by the application and then used(fprintf) inside the dll.</p>
  
  <p>The problem is fprintf will throw an exception.</p>
  
  <p><strong>DLLFile.c</strong></p>
  
  <pre><code>#define BUILD_FOO
  #include "API.H"
  
  File *pFile;
  
  void exportedFunction()
  {
     fprintf(pFile,"This will result in an exception\n");//&lt;-This print will crash
  }
  </code></pre>
  
  <p><strong>API.H</strong></p>
  
  <pre><code>#ifdef BUILD_FOO
  #    define FOOAPI __declspec(dllexport)
  #else
  #    define FOOAPI __declspec(dllimport)
  #endif
  
  FOOAPI  extern File *pFile;
  FOOAPI  void exportedFunction();
  </code></pre>
  
  <p><strong>APLICATION.C</strong></p>
  
  <pre><code>#undef BUILD_FOO
  #include "API.H"
  void main()
  {
  pFile = fopen("path_to_folder","wt");
  fprintf(pFile , "This print will work"); // &lt;- This will be printed ok
  exportedFunction(); 
  }
  </code></pre>
  
  <p>1 From the debugging I've done, this is what I saw:</p>
  
  <p>Inside the <strong>application, fopen()</strong> assigns for <strong>pFile</strong>  an element from <strong>_iob[]</strong>.</p>
  
  <p>In the <strong>DLL</strong> when <strong>fprintf</strong> is called, it is checked that <strong>pFile</strong> is part of the <strong>_iob[]</strong>, but the <strong>_iob[]</strong> from the application seems not to be the same with the one in the DLL(they have different addresses).</p>
  
  <p>2 I have the same  use case(with the same application) and another somewhat similar DLL, and everything works ok there(the _iob[] is at the same place in the application and DLL).</p>