I'm trying to get a list of all components registered with the XPCOM runtime. I'm using nsIComponentRegistrar::EnumeratorContractIDs for that, but am having a weird problem: while it seems that all contract IDs are returned, at one point the GetNext() call to the enumerator fails even though the HasMoreElements() call returned TRUE.
Here's a stripped down test case that shows the problem:
---------------------------------------------------------------- #include "mozilla-config.h" #include "nsCOMPtr.h" #include "nsIComponentRegistrar.h" #include "nsIServiceManager.h" #include "nsISimpleEnumerator.h" #include "nsISupportsPrimitives.h" #include "nsString.h" #include "nsXPCOM.h"
void regdump() {
nsCOMPtr<nsIComponentRegistrar> registrar;
NS_GetComponentRegistrar(getter_AddRefs(registrar));
nsCOMPtr<nsISimpleEnumerator> enumerator;
registrar->EnumerateContractIDs(getter_AddRefs(enumerator));
PRBool more = PR_FALSE;
enumerator->HasMoreElements(&more);
while (more == PR_TRUE) {
nsCOMPtr<nsISupports> element;
nsresult rv = enumerator->GetNext(getter_AddRefs(element));
if (NS_FAILED(rv)) {
printf("Failed to get next contract ID from enum!\n");
break;
}
nsCOMPtr<nsISupportsCString> contractId(
do_QueryInterface(element));
nsCAutoString str;
contractId->GetData(str);
printf("%s\n", PromiseFlatCString(str).get());
enumerator->HasMoreElements(&more);
}
}int main(int argc, char* argv[]) {
nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
if (NS_FAILED(rv)) {
return -1;
}
regdump();
rv = NS_ShutdownXPCOM(nsnull);
if (NS_FAILED(rv)) {
return -1;
}
return 0;
}
----------------------------------------------------------------I've tested this code (well, similar code) on both Win2K and MacOSX, with both Mozilla 1.6 and Mozilla 1.7b, and using both debug and non-debug builds. The failure is always reproducible.
Without specifing a bin directory for XPCOM, the failure appears when trying to get the 48th component. When I specify the bin dir, I get many more components before the enumeration fails. That's why I suspect the list I get is actually complete, but the simple enumerator fails to tell me so.
The same happens for nsIComponentRegistrar::EnumerateCIDs() BTW.
I must be doing something incredibly stupid :-P Can anyone point out what it is?
Cheers, Chris _______________________________________________ Mozilla-xpcom mailing list [EMAIL PROTECTED] http://mail.mozilla.org/listinfo/mozilla-xpcom
