Microsoft KB Archive/941105

= Error messages after SQL Server 2005 has been running for some time on Windows Server 2003: &quot;Failed to load FileName.dll&quot; and &quot;Attempts to load any type of dll might fail within the SQL Server process with errors pointing to LoadLibrary failure&quot; =

Article ID: 941105

Article Last Modified on 12/18/2007

-

APPLIES TO


 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Workgroup Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Enterprise Edition

-



SYMPTOMS
When you run Microsoft SQL Server 2005 on a Windows Server 2003-based computer, you may receive an error message that contains the following error messages:  

Failed to load .dll

 

Attempts to load any type of dll might fail within the SQL Server process with errors pointing to LoadLibrary failure.



This problem usually occurs after SQL Server 2005 has been running for some time.

The complete error message that you receive resembles the following:

Exception Type: Microsoft.SqlServer.Management.SqlIMail.Server.Common.BaseException

Message: The read on the database failed. Reason: Failed to load Msxmlsql.dll.

Data: System.Collections.ListDictionaryI,4092,NULL,NULL,2007-04-13 09:44:11.640,DB\SQLServer5,warning,2007-04-13 09:44:13.153,

The activated proc [dbo].[sp_sysmail_activate] running on queue msdb.dbo.ExternalMailQueue output the following: 'The service queue &quot;ExternalMailQueue&quot; is currently disabled.'

Attempts to use XML related activity in TSQL fails.

Msg 6610, Level 16, State 1, Procedure sp_xml_preparedocument, Line 1

Failed to load Msxmlsql.dll.

Msg 6607, Level 16, State 3, Procedure

sp_xml_removedocument, Line 1 sp_xml_removedocument: The value supplied for parameter number 1 is invalid.

Attempts to load any type of dll might fail within the SQL Server process with errors pointing to LoadLibrary failure.

Attempts to use extended stored procedures will result in

Msg 0, Level 16, State 0, Procedure xp_ss_backup, Line 1

Cannot load the DLL C:\Program Files\Microsoft SQL Server\MSSQL\Binn\SQLsafe_ExtendedStoredProc.dll, or one of the DLLs it references. Reason: 1114(A dynamic link library (DLL) initialization routine failed.).



CAUSE
This problem occurs because no thread local storage (TLS) slot is available for the SQL Server 2005 process (Sqlservr.exe). This is a problem in Windows Server 2003.

When SQL Server 2005 unloads a DLL, the TLS slots that the DLL uses may not be released because of a problem in the TlsFree function. If SQL Server 2005 frequently loads various DLLs, and then unloads these DLLs, these DLLs use up all the available TLS slots. Eventually, SQL Server 2005 can no longer load a DLL that requires TLS slots.



Hotfix information
A supported hotfix is now available from Microsoft. However, this hotfix is intended to correct only the problem that is described in this article. Apply this hotfix only to systems that are experiencing this specific problem. This hotfix might receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Windows Server 2003 service pack that contains this hotfix.

To resolve this problem, submit a request to Microsoft Online Customer Services to obtain the hotfix. To submit an online request to obtain the hotfix, visit the following Microsoft Web site:

http://go.microsoft.com/?linkid=6294451

Note If additional issues occur or any troubleshooting is required, you might have to create a separate service request. The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. To create a separate service request, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Prerequisites
You must have Windows Server 2003 Service Pack 2 installed on the server.

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

889100 How to obtain the latest service pack for Windows Server 2003

Restart information
You do not have to restart the computer after you apply this hotfix.

Hotfix replacement information
This hotfix does not replace any other hotfixes.

File information
The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.

Windows Server 2003 with Service Pack 2, x86-based versions



WORKAROUND
You can use one of the following methods to work around this problem. We recommend that you use method 1 because the extended stored procedure feature may be removed in future releases of SQL Server.

Method 1
 Use the sp_OACreate stored procedure to determine the DLLs that SQL Server 2005 loads into the SQL Server 2005 process. To do this, follow these steps:  Open SQL Server Profiler, and then connect to the instance of SQL Server 2005. In the Trace Properties dialog box, specify the name of the trace, specify the location where SQL Server Profiler saves the trace, and then click Run.</li> After you run SQL Server Profiler for some time, analyze the trace to locate the occurrences of the sp_OACreate stored procedure.</li> Notice the first parameter of each occurrence of the sp_OACreate stored procedure.

Note The first parameter indicates the programmatic identifier (ProgID) of the OLE object or the class identifier (CLSID) of the OLE object.</li> Determine the DLL that contains this OLE object.</li></ol>

Note You can use SQL Server Profiler to monitor the instance of SQL Server 2005 when you start the SQL Server 2005 service.</li> Make sure that you have enabled the OLE Automation procedures option.

For more information about how to determine whether the OLE Automation procedures option is enabled and how to enable the OLE Automation procedures option, visit the following Microsoft Develop Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms191188.aspx

</li>  In SQL Server Management Studio, run the following statements:

Note This following code shows you how to load the Wbemdisp.dll file as an example. DROP PROC sp_Load_Dll GO CREATE PROC sp_Load_Dll AS declare @WmiServiceLocator int, @hr int exec @hr = sp_OACreate 'WbemScripting.SWbemLocator', @WmiServiceLocator output if @hr = 0 raiserror ( 'wbemdisp.dll loaded into sql address space by startup procedure sp_Load_Dll, Object Info: %d', 10 , 1 , @WmiServiceLocator) --WITH LOG WHILE 1=1 WAITFOR DELAY '01:00:00' GO EXEC sp_procoption 'sp_Load_Dll', 'startup' , 'true' GO The statements create a stored procedure. The statements also enable the stored procedure to run when the SQL Server 2005 service starts. This stored procedure performs the following operations:  Loads the Wbemdisp.dll file.</li> Keeps the Wbemdisp.dll file in memory because the statements do not exit.</li></ul>

As long as the Wbemdisp.dll file is in memory, other statements do not load the Wbemdisp.dll file again. Therefore, SQL Server 2005 does not call the TlsAlloc function and the TlsFree function for the Wbemdisp.dll file.

Note You must update the code to load the DLLs that you determined in step 1. </li> Add the statements to the sp_Load_Dll stored procedure in step 3. These statements load the DLLs that you determined in step 1.</li> Restart the SQL Server 2005 service.</li> Verify that SQL Server 2005 loads the Wbemdisp.dll file in memory. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Open the SQL Server error log file.</li> Search for the following message:

wbemdisp.dll loaded into sql address space by startup procedure sp_Load_Dll

Note This message indicates that SQL Server 2005 has loaded the Wbemdisp.dll file.</li>  At a command prompt, run the following command: tasklist /m </li> In the result that you obtain, locate the Sqlservr.exe process, and then verify that the Wbemdisp.dll file is listed.

Note If the Wbemdisp.dll file is listed, SQL Server 2005 has loaded the Wbemdisp.dll file.</li></ol> </li></ol>

Method 2
<ol> Use the sp_OACreate stored procedure to determine the DLLs that SQL Server 2005 loads into the SQL Server 2005 process. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Open SQL Server Profiler, and then connect to the instance of SQL Server 2005.</li> <li>In the Trace Properties dialog box, specify the name of the trace, specify the location where SQL Server Profiler saves the trace, and then click Run.</li> <li>After you run SQL Server Profiler for some time, analyze the trace to locate the occurrences of the sp_OACreate stored procedure.</li> <li>Notice the first parameter of each occurrence of the sp_OACreate stored procedure.

Note The first parameter indicates the programmatic identifier (ProgID) of the OLE object or the class identifier (CLSID) of the OLE object.</li> <li>Determine the DLL that contains this OLE object.</li></ol>

Note You can use SQL Server Profiler to monitor the instance of SQL Server 2005 when you start the SQL Server 2005 service.</li> <li>Create an extended stored procedure. The extended stored procedure contains a function that loads the DLLs that you determined in step 1.</li> <li>Add the extended stored procedure to SQL Server 2005.</li> <li>In SQL Server 2005, use the sp_addextendedproc system stored procedure to register the function in the extended stored procedure.</li> <li>Use the sp_procoption stored procedure to enable the extended stored procedure to automatically start when the SQL Server 2005 service starts.</li> <li>Restart the SQL Server service.</li></ol>

When SQL Server 2005 executes the function in the extended stored procedure, SQL Server 2005 loads the DLLs by calling the LoadLibrary API.

<div class="status_section">

STATUS
This is not a problem in SQL Server 2005. This is a problem in Windows Server 2003. This problem does not occur in Windows Vista.

<div class="references_section">