OK that works - I got XPCOM to inialize. Does that mean it is 'talking' with the
currently running Netscape/Mozilla application or is this a separate entity? I am
trying to write a C++ application that will communicate with the current
Netscape browser to tell it to load another page in the top-most browser window. This
seems to be much harder than I thought - I am probably missing some fundimental point
that simplifies the whole thing.
I have the following code (which fails to get the window):
try {
nsCOMPtr<nsIThread> thread_check;
// xpcom appears to assert if already initialized
// Is there an official way to determine this?
if (NS_FAILED(nsIThread::GetMainThread(getter_AddRefs(thread_check))))
{
printf("XPCOM not initialized yet!\n");
#if 1
const char *path = "C:\\Program Files\\Netscape\\Netscape 6";
#else
const char *path =
"D:\\Netscape\\Mozilla\\mozilla.v0.9.9\\mozilla\\dist\\WIN32_O.OBJ\\bin";
#endif
nsCOMPtr<nsILocalFile> componentPath;
NS_NewLocalFile(path, PR_TRUE, getter_AddRefs(componentPath));
//-initXPCOM
nsIServiceManager *serviceManager=0;
nsResult = NS_InitXPCOM2(&serviceManager, componentPath,
nsnull);
if (NS_FAILED(nsResult)) throw Sprintf("%s:%d
nsResult:%x",__FILE__,__LINE__,nsResult);
else printf("%s:%d
nsResult:%x\n",__FILE__,__LINE__,nsResult);
nsResult =
nsIThread::GetMainThread(getter_AddRefs(thread_check));
if (NS_FAILED(nsResult)) throw Sprintf("%s:%d
nsResult:%x",__FILE__,__LINE__,nsResult);
else printf("%s:%d
nsResult:%x\n",__FILE__,__LINE__,nsResult);
//-nsIWindowsMediator
nsCOMPtr<nsIWindowMediator> mediator=0;
mediator = do_GetService(NS_RDF_DATASOURCE_CONTRACTID_PREFIX
"window-mediator");
if (mediator == 0) throw Sprintf("%s:%d
mediator:0x%p",__FILE__,__LINE__,mediator);
else printf("%s:%d
mediator:0x%p\n",__FILE__,__LINE__,mediator);
//-nsIDOMWindowInternal
nsCOMPtr<nsIDOMWindowInternal> window=0;
nsResult =
mediator->GetMostRecentWindow(nsnull,getter_AddRefs(window));
// <---------------- Fails: window is always 0
if (NS_FAILED(nsResult) || (window==0)) throw Sprintf("%s:%d
window:0x%p nsResult:%x",__FILE__,__LINE__,window,nsResult);
else printf("%s:%d window:0x%p
nsResult:%x\n",__FILE__,__LINE__,window,nsResult);
//-nsIDOMNavigator
nsIDOMNavigator* navigator=0;
nsResult = window->GetNavigator(&navigator);
if (NS_FAILED(nsResult)) throw Sprintf("%s:%d
nsResult:%x",__FILE__,__LINE__,nsResult);
else printf("%s:%d
nsResult:%x\n",__FILE__,__LINE__,nsResult);
nsString userAgent;
nsResult = navigator->GetUserAgent(userAgent);
if (NS_FAILED(nsResult)) throw Sprintf("%s:%d
nsResult:%x",__FILE__,__LINE__,nsResult);
else printf("%s:%d
nsResult:%x\n",__FILE__,__LINE__,nsResult);
printf("User Agent: '%p'\n",userAgent.get());
}
}
catch (const char *e) {printf("\n\nException was raised:\n%s\n\n",e);}
catch (...) {printf("Exception was raised!\n");}
nsResult = NS_ShutdownXPCOM(nsnull);
if (NS_FAILED(nsResult)) return(printf("NS_ShutdownXPCOM() ==
[%x]\n",nsResult));
else printf("NS_ShutdownXPCOM() OK\n");
On Thu, 02 May 2002 14:25:40 +1000, Mark Hammond <[EMAIL PROTECTED]> wrote:
> RL Clippard wrote:
> > Does "nsResult = NS_InitXPCOM2(nsnull, nsnull, nsnull);" have to supply the
> > directory to the Netscape components or does it 'know'?
>
> On Windows you have to tell it. Linux knows. I can't recall the
> details of exactly why, but this was certainly the case 18 months ago.
> I guess it is still the same.
>
> > Also my version of
> > Netscape (6.2) xpcom.dll does not allow NS_InitXPCOM2() - I assumed this is
> > just a minor issue so I am using the xpcom.dll that came with mozilla.9.9.
> >
> > //-nsLocalFile
> > nsCOMPtr<nsILocalFile> componentPath=0;
> > componentPath= do_CreateInstance(NS_LOCAL_FILE_CONTRACTID,&nsResult);
> >
> > const char *path = "C:\\Program Files\\Netscape\\Netscape 6";
> > // Where Netscape is installed
> > nsResult = componentPath->InitWithPath(path);
> >
> > PRBool isDirectory;
> > nsResult = componentPath->IsDirectory(&isDirectory);
> > printf("%s ---<%s>--- a directory\n",path,isDirectory?"IS":"IS NOT");
> > // This works (is indeed a directory)
> >
> > //-initXPCOM
> > nsIServiceManager *serviceManager=0;
> > nsResult = NS_InitXPCOM2(&serviceManager, componentPath, nsnull);
> > // This fails! ---> 0x80004005
>
> This is from the PyXPCOM code (mozilla/extensions/python/xpcom)
>
> ....
> nsCOMPtr<nsIThread> thread_check;
> // xpcom appears to assert if already initialized
> // Is there an official way to determine this?
> if
> (NS_FAILED(nsIThread::GetMainThread(getter_AddRefs(thread_check)))) {
> // not already initialized.
>
> // We need to locate the Mozilla bin directory.
> #ifdef XP_WIN
> // On Windows this by using "xpcom.dll"
>
> char landmark[MAX_PATH+1];
> HMODULE hmod = GetModuleHandle("xpcom.dll");
> if (hmod==NULL) {
> PyErr_SetString(PyExc_RuntimeError, "We dont appear to be
> linked against xpcom.dll!?!?");
> return PR_FALSE;
> }
> GetModuleFileName(hmod, landmark,
> sizeof(landmark)/sizeof(landmark[0]));
> char *end = landmark + (strlen(landmark)-1);
> while (end > landmark && *end != '\\')
> end--;
> if (end > landmark) *end = '\0';
>
> nsCOMPtr<nsILocalFile> ns_bin_dir;
> NS_NewLocalFile(landmark, PR_FALSE, getter_AddRefs(ns_bin_dir));
> nsresult rv = NS_InitXPCOM2(nsnull, ns_bin_dir, nsnull);
> #else
> // Elsewhere, Mozilla can find it itself (we hope!)
> nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
> #endif // XP_WIN
> if (NS_FAILED(rv)) {
> ....
>
> Mark.
>