(void) nsComponentManager::AutoRegister(nsIComponentManagerObsolete::NS_Startup, nsnull);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); registrar->AutoRegister(nsnull);
main(void){    nsresult rv;    // Initialize XPCOM    rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);    if (NS_FAILED(rv))    {        printf("ERROR: XPCOM intialization error [%x].\n", rv);        return -1;    }    // Do Autoreg to make sure our component is registered. The real way of    // doing this is running the xpcom registraion tool, regxpcom, at install    // time to get components registered and not make this call everytime.    // Ignore return value.    //    // Here we use the global component manager. Note that this will cause    // linkage dependency to XPCOM library. We feel that linkage dependency    // to XPCOM is inevitable and this is simpler to code.    // To break free from such dependencies, we can GetService() the component    // manager from the service manager that is returned from NS_InitXPCOM().    // We feel that linkage dependency to XPCOM library is inevitable.    (void) nsComponentManager::AutoRegister(nsIComponentManagerObsolete::NS_Startup, nsnull);    // Create an instance of our component    nsCOMPtr<nsISample> mysample = do_CreateInstance(NS_SAMPLE_CONTRACTID, &rv);    if (NS_FAILED(rv))    {        printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n"               "Debugging hint:\n"               "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n"               "\tsetenv NSPR_LOG_FILE xpcom.log\n"               "\t./nsTestSample\n"               "\t<check the contents for xpcom.log for possible cause of error>.\n",               rv);        return -2;    }    // Call methods on our sample to test it out.    rv = mysample->WriteValue("Inital print:");    if (NS_FAILED(rv))    {        printf("ERROR: Calling nsISample::WriteValue() [%x]\n", rv);        return -3;    }    const char *testValue = "XPCOM defies gravity";    rv = mysample->SetValue(testValue);    if (NS_FAILED(rv))    {        printf("ERROR: Calling nsISample::SetValue() [%x]\n", rv);        return -3;    }    printf("Set value to: %s\n", testValue);    nsXPIDLCString str;    rv = mysample->GetValue(getter_Copies(str));    if (NS_FAILED(rv))    {        printf("ERROR: Calling nsISample::GetValue() [%x]\n", rv);        return -3;    }    if (strcmp(str, testValue))    {        printf("Test FAILED.\n");        return -4;    }    rv = mysample->WriteValue("Final print :");    printf("Test passed.\n");    // Shutdown XPCOM    NS_ShutdownXPCOM(nsnull);    return 0;}