INF: Enabling DLL-based COM Object Execution Outside SQL ServerID: Q198891
|
SQL Server 6.5 and later provides the capability of loading and executing custom Component Object Model (COM) objects through a set of OLE Automation stored procedures or through extended stored procedures. DLL-based COM objects are by default loaded as an in process server, which means the COM object(s) are not only loaded within the SQL Server process memory address space, but have full access to this memory address space. Therefore, a COM object loaded in the SQL Server process space must adhere to the same rules as any DLL file. There is a potential that a COM object could overwrite memory within the SQL Server process or leak resources, causing instability. If there is suspicion that a COM object(s) may be affecting the robustness of the SQL Server process, you may want to use the steps in this article to instantiate the COM object outside the SQL Server process space. Implementation of the Distributed Component Object Model's (DCOM) specification of "Location Transparency" into the operating system has provided the ability to execute a DLL-based COM object outside the SQL Server process space.
The COM process of running the DLL outside of the SQL Server process is called Remoting. Remoting requires that another executable be a surrogate process in place of the SQL Server executable. The default executable used by the DCOM Service Control Manager (Rpcss.exe) is named Dllhost.exe. The DCOM support structure uses the Dllhost.exe file to load the DLL into its process space and then uses proxy/stub pairs to marshal the requested interface(s) transparently back to the client, which in this case is the SQL Server. This executable can accept multiple interface/method requests concurrently. After the interface use is complete, the DCOM Service Control Manager (SCM) manages the clean up and unloading of the Dllhost.exe file. COM objects should not be expected to retain state information in between instantiations.
In order for this article to work correctly, the system must be running a DCOM enabled operating system. This would be either Windows NT 4.0 Service Pack Two (or higher), Windows 98 or Windows 95 with the DCOM add-in installed in the case of SQL Server version 7.0 Desktop Edition. The following steps can apply to any DLL-based COM object that is being created in the SQL Server process space, whether it is being instantiated through sp_OACreate or an extended stored procedure.
DECLARE @object int
DECLARE @hr int
EXEC @hr = sp_OACreate 'SQLOLE.SQLServer', @object OUT, 4
HRESULT hr = CoCreateInstance(CLSID_Test, NULL, CLSCTX_LOCAL_SERVER,
IID_IUnknown, (void**)&piunknown);
OLTEComponent.Object
The programmatic identifier can be used to find the class identifier for a COM object. Open the Registry Editor (Regedit.exe) and under the HKEY_CLASSES_ROOT key use the Find facility to locate a key with the name of your <OLEComponent.Object>. You will find it at other levels, but it should be located at the level directly below the HKEY_CLASSES_ROOT. Once located, click the plus sign in front of the key name and you should see a subkey called CLSID. Click that folder to see the values within that key. On the right-hand side of the screen is a title named "(Default)". The data for that key should be in the following form:
{59F929A0-74D8-11D2-8CBC-08005A390B09}
Make a note of this value or copy it to Notepad, and include the brackets.
AppID
{59F929A0-74D8-11D2-8CBC-08005A390B09}
The application identifier AppID is used by DCOM to associate the DLL with an executable file.
DllSurrogate
Leave the Data column blank for this value. Because the data column is blank, this informs DCOM to run the default executable file, Dllhost.exe and load the COM object within its process space.
DCOMCNFG
Press the ENTER key to open the Distributed COM Configuration Properties dialog box. Click the Default Properties tab, and make sure that Enable Distributed COM on this computer is selected. If it is not, select it and click the Apply button to enable.OLE Automation Error Information
HRESULT: 0x80040154
Source: ODSOLE Extended Procedure
Description: Class not registered
OLE Automation Error Information
HRESULT: 0x80070005
Source: ODSOLE Extended Procedure
Description: Access is denied.
OLE Automation Error Information
HRESULT: 0x80080005
Source: ODSOLE Extended Procedure
Description: Server execution failed
WAITFOR DELAY '000:00:20'
Run the script and immediately go to the command prompt and run the Tlist.exe file and note the Dllhost.exe PID. Re-execute the Tlist.exe, passing the PID as a parameter. This shows the DLLs that are loaded within the Dllhost.exe process space. The DLL-based COM object should be listed as running within this process. Once the script returns, re-executing Tlist.exe reveals that the Dllhost.exe process is no longer running.
C:\>tlist dllhost
275 dllhost.exe
CWD: C:\NT40\system32\
CmdLine: C:\NT40\System32\dllhost.exe {00000514-0000-0010-8000-00AA006D2EA4}
-Embedding
VirtualSize: 19180 KB PeakVirtualSize: 19180 KB
WorkingSetSize: 1780 KB PeakWorkingSetSize: 1780 KB
NumberOfThreads: 3
278 Win32StartAddr:0x01001920 LastErr:0x00000000 State:Waiting
215 Win32StartAddr:0x00001b5e LastErr:0x00000000 State:Waiting
253 Win32StartAddr:0x00001b60 LastErr:0x000000cb State:Waiting
4.0.1381.105 shp 0x01000000 dllhost.exe
4.0.1381.130 shp 0x77f60000 ntdll.dll
4.0.1381.121 shp 0x77dc0000 ADVAPI32.dll
4.0.1381.133 shp 0x77f00000 KERNEL32.dll
4.0.1381.133 shp 0x77e70000 USER32.dll
4.0.1381.115 shp 0x77ed0000 GDI32.dll
4.0.1381.131 shp 0x77e10000 RPCRT4.dll
4.0.1381.117 shp 0x77b20000 ole32.dll
6.0.8267.0 shp 0x78000000 MSVCRT.dll
0x1f310000 msado15.dll
2.30.4265.1 shp 0x766f0000 OLEAUT32.dll
4.0.1381.72 shp 0x77bf0000 rpcltc1.dll
With the SQL Server version 7.0 Desktop Edition running on Windows 95 or Windows 98 workstations the "32-bit Modules Loaded" within the Microsoft System Information application tool can be used during the execution to see the loading\unloading of the Dllhost.exe file and the COM object DLL during this test. This tool is accessed by clicking the Start button, pointing to Programs, pointing to Accessories and then selecting System Tools.http://www.microsoft.com/comNOTE: Because of security limitations, Windows 95 or Windows 98 does not support starting a DLLSurrogate process and loading a COM DLL by a remote client. Therefore, the COM object must exist within the Running Object Table (ROT) and be running/loaded if it is to be available for use by a remote client computer. The steps in this article can be used to help isolate COM objects when they are suspected to be the cause of instability in the SQL Server. Make sure that each component is tested thoroughly running out of process to ensure consistent behavior. Performance difference in instantiating a COM object in the SQL Server process and outside the process space varies. Also, some COM objects were not built to be Remoted and can leak resources. Test thoroughly before implementing the steps in this article for something other than a troubleshooting step. The following article is an example of remoting a COM object that can cause a resource leak:
Q197426 BUG: Handle Leak when Passing ADO Objects Between ProcessesNOTE: SQL Server 6.5, by default, operates with Single Thread Apartment (STA) model and handles initialization of COM objects on a separate internal thread. In this model, one thread is selected to control the creation of all the OLE objects within the SQL Server process and to proxy back to all client connections needing access to this COM object. Because this is handled internally by the SQL Server, the object's persistence cannot be guaranteed between instantiations of the COM object.
Q194661: SQL Server COM Object Persistence ModelFurther reading on running DLL-based COM object(s) within DLL Surrogates can be found in the following sources:
Q180780: How Sp_OA Procedures Extension to SQL Server Is Implemented
http://www.microsoft.com/comThe DCOM Add-In for Windows 95 is shipped with the SQL Server 7.0 media and the file is named Dcom95.exe. The file can also be downloaded from: the www.microsoft.com/oledev website.
http://www.msdn.microsoft.com
Additional query words: ActiveX, DCE, RPC, ATL, COM+, MTS, memory, resource, leak
Keywords : kbole SSrvProg SSrvStProc SSrvTran_SQL SSrvVisB kbSQLServ650 kbSQLServ700
Version : winnt:6.5,7.0
Platform : winnt
Issue type : kbinfo
Last Reviewed: June 9, 1999