Firefox information via DDE

By | March 16, 2012

For a while I’ve been looking into how to work out what browser URL’s a user has open with some code. Internet Explorer is easy – I’ll post that code later – but Firefox and others, that’s where it gets interesting.

From my searching there seems to be two ways to do this – DDE or by using looking through some child windows. This article isn’t going to tell you how to achieve this aim as I haven’t looked too closely at the second option but I have looked at the first and can write about it. But why ? for the sole reason that whilst trying to find this out I found many questions, but no clear answers and very little code. Everything in this article has been pieced together from the many to produce this one. Hopefully it will be helpful.

What really helped was finding this article, which referenced the NDde project (link) which allows DDE to be used in C#/etc. I’ve not dealt with DDE before, so it was all new to me but diving into the source code gave me pointers towards the correct part of MSDN – the DDE Management Library. From here I could start crafting a C++ solution that fitted my needs.

And this is where it all went wrong ! This link details how DDE works with Internet Explorer, but I have been unable to find a single official reference for Firefox. In particular are the topics WWW_GetWindowInfo and WWW_ListWindows – this would allow an application to get URL’s and information for each tab or browser window.

Problem is, Firefox doesn’t appear to support WWW_ListWindows and it doesn’t matter what parameter you pass to WWW_GetWindow, it only returns the active tab for the current window (and if no Firefox window is the active one, then the last window that was active). And what’s more, the return is in ANSI encoding rather than unicode. The string that is returned will give you the title of the window/tab/page in addition to the URL.

Here’s a win32 console application (compiles in VS210) that will return the one Firefox DDE result. Sorry about the lack of indenting, need to work out my code tags 😉

#include <Windows.h>
#include <stdio.h>
#include <conio.h>

void wmain(int argc, wchar_t** argv)
{
DWORD dwApp = 0;

if (DdeInitialize(&dwApp, NULL, APPCLASS_STANDARD, 0) != DMLERR_NO_ERROR) {
return;
}

HSZ server = DdeCreateStringHandle(dwApp, L”Firefox”, CP_WINUNICODE);
HSZ topic = DdeCreateStringHandle(dwApp, L”WWW_GetWindowInfo”, CP_WINUNICODE);

HCONV conv = DdeConnect(dwApp, server, topic, NULL);
if (conv == 0L) {
DdeUninitialize(dwApp);
return;
}

HSZ url = DdeCreateStringHandle(dwApp, L”URL”, CP_WINUNICODE);

HDDEDATA pData = DdeClientTransaction(NULL, 0, conv, url, CF_TEXT, XTYP_REQUEST, 500, NULL);
if (pData == 0) {
DdeUninitialize(dwApp);
return;
}

DWORD dwData = DdeGetData(pData, NULL, 0, 0);
if (dwData > 0) {
BYTE* p = (BYTE*) malloc(dwData);
if (p != NULL) {
DWORD dwRes = DdeGetData(pData, p, dwData, 0);
if (dwRes > 0) {
printf(“ANSI URL = %s,\n”, (char*) p);
}

free(p);
}
}

DdeFreeStringHandle(dwApp, url);
DdeFreeStringHandle(dwApp, server);
DdeFreeStringHandle(dwApp, topic);

DdeDisconnect(conv);

DdeUninitialize(dwApp);
_getch();
}

I’ll post how to get information from IE soon [which no doubt exists somewhere else on the web] and need to investigate more into getting information from windows classes. Right now it looks like the best and most reliable to get URL information would be to use a plugin.

Category: C++