PR_LoadStaticLibrary("XPCOMComponentGlue", sGlueSymbols);
nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result,                              nsIFile* binDirectory,                              nsIDirectoryServiceProvider* appFileLocationProvider){    if (!gXPCOMHasGlobalsBeenInitalized)        return NS_ERROR_NOT_INITIALIZED;    nsresult rv = NS_OK;     // We are not shutting down    gXPCOMShuttingDown = PR_FALSE;#ifdef NS_BUILD_REFCNT_LOGGING    nsTraceRefcnt::Startup();#endif    // Establish the main thread here.    rv = nsIThread::SetMainThread();    if (NS_FAILED(rv)) return rv;    // Startup the memory manager    rv = nsMemoryImpl::Startup();    if (NS_FAILED(rv)) return rv;#ifdef XP_UNIX    NS_StartupNativeCharsetUtils();#endif    NS_StartupLocalFile();    StartupSpecialSystemDirectory();    PR_LoadStaticLibrary("XPCOMComponentGlue", sGlueSymbols);    // Start the directory service so that the component manager init can use it.    rv = nsDirectoryService::Create(nsnull,                                    NS_GET_IID(nsIProperties),                                    (void**)&gDirectoryService);    if (NS_FAILED(rv))        return rv;    nsCOMPtr<nsIDirectoryService> dirService = do_QueryInterface(gDirectoryService, &rv);    if (NS_FAILED(rv))        return rv;    rv = dirService->Init();    if (NS_FAILED(rv))        return rv;    // Create the Component/Service Manager    nsComponentManagerImpl *compMgr = NULL;    if (nsComponentManagerImpl::gComponentManager == NULL)    {        compMgr = new nsComponentManagerImpl();        if (compMgr == NULL)            return NS_ERROR_OUT_OF_MEMORY;        NS_ADDREF(compMgr);        PRBool value;        if (binDirectory)        {            rv = binDirectory->IsDirectory(&value);            if (NS_SUCCEEDED(rv) && value)                gDirectoryService->Set(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, binDirectory);            //Since people are still using the nsSpecialSystemDirectory, we should init it.            nsCAutoString path;            binDirectory->GetNativePath(path);            nsFileSpec spec(path.get());            nsSpecialSystemDirectory::Set(nsSpecialSystemDirectory::Moz_BinDirectory, &spec);        }        if (appFileLocationProvider) {            rv = dirService->RegisterProvider(appFileLocationProvider);            if (NS_FAILED(rv)) return rv;        }        rv = compMgr->Init();        if (NS_FAILED(rv))        {            NS_RELEASE(compMgr);            return rv;        }        nsComponentManagerImpl::gComponentManager = compMgr;        if (result) {            nsIServiceManager *serviceManager =                NS_STATIC_CAST(nsIServiceManager*, compMgr);            NS_ADDREF(*result = serviceManager);        }    }    nsCOMPtr<nsIMemory> memory;    NS_GetMemoryManager(getter_AddRefs(memory));    // dougt - these calls will be moved into a new interface when nsIComponentManager is frozen.    rv = compMgr->RegisterService(kMemoryCID, memory);    if (NS_FAILED(rv)) return rv;    rv = compMgr->RegisterService(kComponentManagerCID, NS_STATIC_CAST(nsIComponentManager*, compMgr));    if (NS_FAILED(rv)) return rv;#ifdef GC_LEAK_DETECTOR  rv = NS_InitLeakDetector();    if (NS_FAILED(rv)) return rv;#endif    // 2. Register the global services with the component manager so that    //    clients can create new objects.    // Registry    nsIFactory *registryFactory = NULL;    rv = NS_RegistryGetFactory(&registryFactory);    if (NS_FAILED(rv)) return rv;    NS_DEFINE_CID(kRegistryCID, NS_REGISTRY_CID);    rv = compMgr->RegisterFactory(kRegistryCID,                                  NS_REGISTRY_CLASSNAME,                                  NS_REGISTRY_CONTRACTID,                                  registryFactory, PR_TRUE);    NS_RELEASE(registryFactory);    if (NS_FAILED(rv)) return rv;    // Category Manager    {      nsCOMPtr<nsIFactory> categoryManagerFactory;      if ( NS_FAILED(rv = NS_CategoryManagerGetFactory(getter_AddRefs(categoryManagerFactory))) )        return rv;      NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID);      rv = compMgr->RegisterFactory(kCategoryManagerCID,                                    NS_CATEGORYMANAGER_CLASSNAME,                                    NS_CATEGORYMANAGER_CONTRACTID,                                    categoryManagerFactory,                                    PR_TRUE);      if ( NS_FAILED(rv) ) return rv;    }    for (int i = 0; i < components_length; i++)        RegisterGenericFactory(compMgr, &components[i]);    rv = nsComponentManagerImpl::gComponentManager->ReadPersistentRegistry();#ifdef DEBUG        if (NS_FAILED(rv)) {        printf("No Persistent Registry Found.\n");            }#endif    if ( NS_FAILED(rv) || CheckAndRemoveUpdateFile()) {        // if we find no persistent registry, we will try to autoregister        // the default components directory.        nsComponentManagerImpl::gComponentManager->AutoRegister(nsnull);                // If the application is using a GRE, then,         // auto register components in the GRE directory as well.        //        // The application indicates that it's using an GRE by        // returning a valid nsIFile when queried (via appFileLocProvider)        // for the NS_GRE_DIR atom as shown below        //        if ( appFileLocationProvider ) {            nsCOMPtr<nsIFile> greDir;            PRBool persistent = PR_TRUE;            appFileLocationProvider->GetFile(NS_GRE_DIR, &persistent, getter_AddRefs(greDir));            if (greDir)            {                rv = nsComponentManagerImpl::gComponentManager->AutoRegister(greDir);                if (NS_FAILED(rv))                {                    NS_ERROR("Could not AutoRegister GRE components");                    return rv;                }            }        }    }        // Pay the cost at startup time of starting this singleton.    nsIInterfaceInfoManager* iim = XPTI_GetInterfaceInfoManager();    NS_IF_RELEASE(iim);    nsCOMPtr<nsIEventQueueService> eventQService(do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv));    if ( NS_FAILED(rv) ) return rv;    rv = eventQService->CreateThreadEventQueue();    if ( NS_FAILED(rv) ) return rv;    // Notify observers of xpcom autoregistration start    NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_OBSERVER_ID,                                   nsnull,                                  NS_XPCOM_STARTUP_OBSERVER_ID);        return rv;}