KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q50336: Passing a Stream File Pointer (FILE *) Between DLLs

Article: Q50336
Product(s): See article
Version(s): 5.10
Operating System(s): OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 17-JUL-1990

Question:

Regarding single-thread DLLs built with the C 5.10 single-thread
DLL support library LLIBCDLL.LIB:

My main() function calls a function in my first DLL, which fopen()s a
buffered stream file. The function in my first DLL calls a function
in my second DLL, passing the stream pointer (FILE *) as an argument.
When the function in my second DLL attempts to fread() the stream for
the first time (which causes the allocation of the stream buffer), a
General Protect fault (segment violation Trap 13 or 0xD) occurs. What
went wrong, and how can I work around the problem?

Response:

This procedure cannot be done with LLIBCDLL-created DLLs because the
second DLL has no way to know where the internal C run-time library
data structures are, which in this case would be the data structures
for the file stream that was opened in the first DLL.

To share file streams between DLLs or between an .EXE and DLLs, use
the dynamically linked, multithread support C run-time function
CRTLIB.DLL. The correct way to do this is to have one of your DLLs
export a version of the fread() function and/or fopen() function. The
fopen() and fread() that get called must be in the same DLL.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.