KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q50700: C Run Time Can’t Be Used in DLL Initialization with CRTLIB.DLL

Article: Q50700
Product(s): See article
Version(s): 5.10
Operating System(s): OS/2
Keyword(s): ENDUSER | SR# G891005-116 | mspl13_c
Last Modified: 30-NOV-1989

Question:

I am writing a DLL with a start-up routine written in C.

The start-up routine is getting a protection violation within a
sprintf call (the first call to a C run-time function).

Can the start-up routine call the C Run Time Library?  Why is this
bombing?

This works correctly if I link the .DLL with LLIBCDLL.LIB and use the
appropriate .OBJ files.

Response:

In general, you cannot call the run time library from a multithreaded
.DLL initialization routine (one that uses CRTLIB.DLL).

The reason is that C_INIT() doesn't completely initialize CRTLIB.DLL;
this is not initialized until the .EXE start-up code is executed.

You still must call C_INIT() from within your .DLL initialization
routine in order to perform other necessary initializations.

There are two possible workarounds:

1.  Write your own functions to replace sprintf().

2.  Don't use user initialization; instead, call an initialization
    routine in your .DLL from the beginning of your .EXE. Since
    your code in your .EXE does not execute until after the .EXE's
    start-up is executed, CRTLIB.DLL will be initialized by the time
    you attempt to initialize your .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.