Going crazy trying to access nsINavBookmarksService from C++ code!! - Mozilla

This is a discussion on Going crazy trying to access nsINavBookmarksService from C++ code!! - Mozilla ; I will admit up front I'm relatively new to XPCOM, but still, I am getting very frustrated. I have a Firefox add-on I'm writing in C++, and I need to read the existing bookmarks. No big deal, right? Except it ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Going crazy trying to access nsINavBookmarksService from C++ code!!

  1. Going crazy trying to access nsINavBookmarksService from C++ code!!

    I will admit up front I'm relatively new to XPCOM, but still, I am
    getting very frustrated.

    I have a Firefox add-on I'm writing in C++, and I need to read the
    existing bookmarks. No big deal, right? Except it crashes all over
    the place.

    First question - where am I supposed to get the IDL and H files for
    non-frozen interfaces (like nsINavBookmarksService)? There are copies
    lying all over the place on the web, but no indication which is the
    one. It seems plausible that I've grabbed bad copies of IDL
    files, and as a result I'm not calling the function I think I'm
    calling.

    Second question - is there any way, from C++, to enumerate the
    functions in a given interface? So that I could access the functions
    in nsINavBookmarksService _without_ having a copy of the IDL and H
    files? Something akin to the IDispatch interface that COM objects
    provide? Or a way to query, at run-time, the typelib contents for
    nsINavBookmarksService?

    So anyway, my code goes like this (with mucho error-checking I left
    out for brevity here):

    nsINavBookmarksService* pBook = NULL;
    CallCreateInstance("@mozilla.org/browser/nav-bookmarks-service;1",
    &pBook);
    PRInt64 menuFolder;
    pBook->GetBookmarksMenuFolder(&menuFolder);

    So far so good, pBook is non-NULL and I get '2' for the value of
    menuFolder. So now for the first crash:

    PRInt64 id;
    pBook->GetIdForItemAt(menuFolder, 0, &id);

    Calling this function crashes Firefox. It doesn't throw an exception
    or return an error, it just crashes.
    So if I comment out that line and try something else instead (that I
    saw on a web page):

    nsINavHistoryService* pHistory = NULL;
    CallCreateInstance("@mozilla.org/browser/nav-history-service;1",
    &pHistory);
    nsINavHistoryQuery* pQuery = NULL;
    pHistory->GetNewQuery(&pQuery);

    Again, we're find up to here, got nice non-NULL pointers. But if I
    try to do this:

    PRInt64 folders[3];
    folders[0] = menuFolder;
    folders[1] = toolbarFolder;
    folders[2] = unfiledFolder;
    pQuery->SetFolders(folders, 3);

    Kaboom. Firefox crashes.

    Any help or suggestions welcome!

    Thanks,
    Chris

  2. Re: Going crazy trying to access nsINavBookmarksService from C++code!!

    On Oct 20, 10:56*am, Gijs Kruitbosch wrote:
    > On 20/10/2009 17:09 PM, Chris Shearer Cooper wrote:
    >
    >
    >
    > > I will admit up front I'm relatively new to XPCOM, but still, I am
    > > getting very frustrated.

    >
    > > I have a Firefox add-on I'm writing in C++, and I need to read the
    > > existing bookmarks. *No big deal, right? *Except it crashes all over
    > > the place.

    >
    > > First question - where am I supposed to get the IDL and H files for
    > > non-frozen interfaces (like nsINavBookmarksService)? *There are copies
    > > lying all over the place on the web, but no indication which is the
    > > *one. *It seems plausible that I've grabbed bad copies of IDL
    > > files, and as a result I'm not calling the function I think I'm
    > > calling.

    >
    > > Second question - is there any way, from C++, to enumerate the
    > > functions in a given interface? *So that I could access the functions
    > > in nsINavBookmarksService _without_ having a copy of the IDL and H
    > > files? *Something akin to the IDispatch interface that COM objects
    > > provide? *Or a way to query, at run-time, the typelib contents for
    > > nsINavBookmarksService?

    >
    > > So anyway, my code goes like this (with mucho error-checking I left
    > > out for brevity here):

    >
    > > nsINavBookmarksService* pBook = NULL;
    > > CallCreateInstance("@mozilla.org/browser/nav-bookmarks-service;1",
    > > &pBook);
    > > PRInt64 menuFolder;
    > > pBook->GetBookmarksMenuFolder(&menuFolder);

    >
    > > So far so good, pBook is non-NULL and I get '2' for the value of
    > > menuFolder. *So now for the first crash:

    >
    > > PRInt64 id;
    > > pBook->GetIdForItemAt(menuFolder, 0,&id);

    >
    > > Calling this function crashes Firefox. *It doesn't throw an exception
    > > or return an error, it just crashes.
    > > So if I comment out that line and try something else instead (that I
    > > saw on a web page):

    >
    > > nsINavHistoryService* pHistory = NULL;
    > > CallCreateInstance("@mozilla.org/browser/nav-history-service;1",
    > > &pHistory);
    > > nsINavHistoryQuery* pQuery = NULL;
    > > pHistory->GetNewQuery(&pQuery);

    >
    > > Again, we're find up to here, got nice non-NULL pointers. *But if I
    > > try to do this:

    >
    > > PRInt64 folders[3];
    > > folders[0] = menuFolder;
    > > folders[1] = toolbarFolder;
    > > folders[2] = unfiledFolder;
    > > pQuery->SetFolders(folders, 3);

    >
    > > Kaboom. *Firefox crashes.

    >
    > > Any help or suggestions welcome!

    >
    > > Thanks,
    > > Chris

    >
    > So, I am by no means a C(++) expert, but you should not call createInstance on a
    > service. That's definitely the first thing that goes wrong. Second, you probably
    > don't want raw pointers, but nsCOMPtrs. I think the easiest way to get going is
    > for you to look at the actual Fx code that uses this, rather than "somewhere on
    > the web". Specifically, assuming you're working with the latest stable version
    > of Firefox (3.5), check out:http://mxr.mozilla.org/mozilla1.9.1/, specificallyhttp://mxr.mozilla.org/mozilla1.9.1/source/toolkit/components/places/....
    > and eg.http://mxr.mozilla.org/mozilla1.9.1/...onents/migrati...
    > (gotten viahttp://mxr.mozilla.org/mozilla1.9.1/search?string=nsINavHistoryServic....c&findi=\.c&filter=^[^\0]*%24&hitlimit=&tree=mozilla1.9.1
    > )
    >
    > Also,http://developer.mozilla.org/probably has info that is useful for using
    > XPCOM from C++. (for example,https://developer.mozilla.org/en/Cre...nts/Using_XPCO...
    > )
    >
    > ~ Gijs


    Perfect! Brilliant!

    Now I have to figure out why ... I'm wondering if it's some sort of
    reference-counting issue. Do you know of any documentation that helps
    COM programmers like me understand the differences in how COM and
    XPCOM do reference-counting?

    Thanks,
    Chris

+ Reply to Thread