Window object, how to get WinAPI HWND?

1,007 views
Skip to first unread message

Jari Pennanen

unread,
Jun 23, 2010, 8:13:28 AM6/23/10
to Chromium-extensions
Hi!

I've created a NPAPI plugin and extension that vertical maximizes new
windows (only on Windows 7), it currently relies on the shady fact
that new window created is currently active. It would be more optimal
if there were a way to get WINAPI HWND from the chrome Window object
somehow.

Most important lines of code in my plugin are three lines in here:
http://github.com/Ciantic/chrome-vertmax/blob/master/plugin/np_win7vmax.c#L78

But as one can see, I have to now rely on little "Sleep" (Very bad),
and "GetForegroundWindow" (Less bad), but it works... I could get rid
of the Sleep and GetForegroundWindow if there were a way to get the
Window HWND from the chrome Window javascript object.

Plugin & Extension source code: http://github.com/Ciantic/chrome-vertmax
The install page: http://ciantic.github.com/chrome-vertmax/

Daniel Wagner-Hall

unread,
Jun 24, 2010, 9:28:42 AM6/24/10
to Jari Pennanen, Chromium-extensions
You can get the HWND using the setwindow function.

When NP_GetEntryPoints is called, set the NPPluginFuncs's arg's field
setwindow to a function with signature: NPError SetWindow(NPP
instance, NPWindow *window);

My example:

NPError WINAPI NP_GetEntryPoints(NPPluginFuncs *plugin_funcs) {
plugin_funcs->setwindow = SetWindow;
}

NPError SetWindow(NPP instance, NPWindow *window) {
HWND hwnd = (HWND)window->window;
if (hwnd != NULL) {
....
}
}

the NPWindow on windows will be an object with field named window
which is an HWND. On other platforms, this is different. The first
time SetWindow is called, window->window will probably be NULL, so
check for that too. If it contains a non-NULL value, it contains the
current (latest) window for the plugin object.

> --
> You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
> To post to this group, send email to chromium-...@chromium.org.
> To unsubscribe from this group, send email to chromium-extens...@chromium.org.
> For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
>

Jari Pennanen

unread,
Jun 24, 2010, 12:23:08 PM6/24/10
to Chromium-extensions
Hi!

On 24 kesä, 16:28, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> NPError SetWindow(NPP instance, NPWindow *window) {
>   HWND hwnd = (HWND)window->window;
>   if (hwnd != NULL) {
>     ....
>   }

setWindow is called only once on my system. It does not seem to be
called again when I create new windows in chrome, and thus I get *one*
HWND (have not really tested what HWND it is)... The HWND should
differ between chrome windows.

Can I ask chrome to call GetEntryPoints, that way I could get more
HWND's, right?

Btw, even if I can get the "current window HWND" it really doesn't
answer the question can we somehow determine the HWND of particular
window id (the Window object id in javascript)... I very much
appreciate your answer though.

Daniel Wagner-Hall

unread,
Jun 24, 2010, 9:14:23 PM6/24/10
to Jari Pennanen, Chromium-extensions
I guess you have the plugin in the background page? The SetWindow will
give you the HWND of the window of the NPAPI object. You can either:

1) Inject an NPAPI object (with zero height and width, perhaps) into
every tab, and grab the HWNDs when SetWindow is called for each of
them.
2) Traverse the tree of HWNDs to find a parent process's HWND, and
find its children. Each tab has a HWND with class name
Chrome_RenderWidgetHostHWND and Spy++ will help you work out what's
going on - use http://msdn.microsoft.com/en-us/library/ff468919(v=VS.85).aspx
to traverse the hierarchy. Note that this is horrible and brittle.

There currently isn't a way to find an arbitrary window's HWND (I can
see why this would be useful - perhaps add a feature request to
http://crbug.com/new)

Jari Pennanen

unread,
Jun 25, 2010, 3:34:24 AM6/25/10
to Chromium-extensions
On 25 kesä, 04:14, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> Note that this is horrible and brittle.

Yes well, I think I will stick with my current method for now, thanks
though :)

> There currently isn't a way to find an arbitrary window's HWND (I can
> see why this would be useful - perhaps add a feature request tohttp://crbug.com/new)

Here it is http://code.google.com/p/chromium/issues/detail?id=47497
Reply all
Reply to author
Forward
0 new messages