[Pharo-dev] How to listen for windows messages?

phil at highoctane.be phil at highoctane.be
Tue Feb 28 07:29:53 EST 2017


Hi,

Pharo is itself a Windows application (under Windows of course).

So Window event loop is picking up the events and transforming them into
Pharo events.

You can check this in https://github.com/pharo-project/pharo-vm

in opensmalltalk-vm\platforms\win32\vm\sqWin32Window.c

https://github.com/pharo-project/pharo-vm/blob/master/opensmalltalk-vm/platforms/win32/vm/sqWin32Window.c

Like L235...

LRESULT CALLBACK MainWndProcW(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)

You'll see that we stick a lot of stuff into evt->... in various ways.

There is a default fallback at the end.

default:
/* Unprocessed messages may be processed outside the current
module. If firstMessageHook is non-NULL and returns a non
zero value, the message has been successfully processed */
if(firstMessageHook)
if((*firstMessageHook)(hwnd, message, wParam, lParam))
return 1;
return DefWindowProcW(hwnd,message,wParam,lParam);

{


So, if firstMessageHook exists, one can do whatever.

Now, a Windows manager may need other stuff and so on.
What you are looking at is how to hook this VM side with more of the
general Windows system.

These hooks are globals, so should be accessible.

/****************************************************************************/
/* Message Processing */
/****************************************************************************/
/* The last dispatched event. It is used for the event processing
mechanism. */
MSG *lastMessage = NULL;
/* The entry to the message hooks called from the window procedure.
If another module requires to process messages by itself, it should
put its message procedure in this place. */
messageHook firstMessageHook = 0;
/* The entry to a pre-message hook. Can be used to intercept any messages
to the squeak main window. Useful for modules that wish to be notified
about certain messages before they are processed. */
messageHook preMessageHook = 0;

Never played with these but any Windows integration is of interest to me.

So, go ahead. Nothing would resist focused work.

Be aware that there is another VM underway with a cleanup of all of this
but this is not released yet.

Keep us posted.
Phil



On Tue, Feb 28, 2017 at 9:33 AM, Torsten Bergmann <astares at gmx.de> wrote:
>
> Hi,
>
> I guess what you want to achieve will not be an easy task for you if you
are new to programming.
> But it is always good to have a goal and if you have time to learn I'm
pretty sure you will master
> it.
>
> Even when OS-Windows inspire you for automizing tasks in other Windows
processes (like autoit) you
> should not start with my OS-Windows project directly as contributing to
it requires knowledge
> on your side on Smalltalk as well as Win32 C programming.
>
> First start with learning Smalltalk and Pharo
>   - http://files.pharo.org/books/
>   - http://pharo.pharocloud.com/pharobooks
>   - http://stephane.ducasse.free.fr/FreeBooks.html
>
> After having an idea about Smalltalk and Pharo you should try to learn
UFFI which
> is Pharos unified foreign function interface and ability to call C DLL's.
>
>
https://ci.inria.fr/pharo-contribution/view/Books/job/PharoBookWorkInProgress/lastSuccessfulBuild/artifact/book-result/UnifiedFFI/
>
> Sideways start learning about the Win32 API and C as this helps to better
understand
> how things are done on the Windows side. There are many, many tutorials
out there.
> Try to call or wrap some simple DLL functions on you own first.
>
> There is a nice and tiny C compiler if you want to try out some C samples
> http://www.pellesc.de/index.php?page=download&lang=en
>
> With this initial knowledge you can try to understand why and how
OS-Windows was done.
> I wrote many tests - just add a breakpoint and run them to debug through
the code to understand.
> Google for the API descriptions of the Win32 functions that are called to
get an understanding
> how Pharo and C get connected.
>
> Also check how you work with callbacks in UFFI (as this would be required
for a hook).
>
> If you are then still interested in interception windows messages you
should start reading here
>
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
>
> Primarily one has to wrap SetWindowsHookEx API function and friends and
wrap a good API
> for them in Pharo.
>
> Hope that helps to get started.
>
> Regards
> Torsten
>
> > Gesendet: Dienstag, 28. Februar 2017 um 06:04 Uhr
> > Von: lw1990 <lukewallace1990 at gmail.com>
> > An: pharo-dev at lists.pharo.org
> > Betreff: [Pharo-dev] How to listen for windows messages?
> >
> > In the OS-Windows package, there exists the ability to use the windows
api in
> > Pharo.
> > This is very powerful, and I intend to add more of the windows api into
it
> > (it's only partially implemented).
> >
> > This appears to all have been done with DllCalls (FFI calls) so far.
> >
> > A big part of interacting with windows is listening to/intercepting and
> > responding to windows messages.
> > For example, every time a user presses a key on the keyboard, a windows
> > message will happen in the background for that key. If Pharo was aware
of
> > these messages, then Pharo could do things in response to hotkeys
pressed on
> > Windows (outside of a pharo window).
> >
> > It would also make reacting to events potentially nicer. Like making a
> > windows-desktop-manager in Pharo that can tell when a new window is
created
> > by windows (maybe they opened notepad). It would certainly be better
than an
> > infinite loop or timer of 'get active window and compare to last active
> > window and see if it changed'. Instead it would be 'when receive the
> > WM_MESSAGE for new window created, notify Pharo so it can react'.
> >
> > How can I set up a windows message hook in Pharo?
> > Please keep in mind I'm very new to programming, but I work from home
and
> > have lots of time to learn :-)
> >
> >
> >
> > --
> > View this message in context:
http://forum.world.st/How-to-listen-for-windows-messages-tp4936285.html
> > Sent from the Pharo Smalltalk Developers mailing list archive at
Nabble.com.
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20170228/1a641a9e/attachment.html>


More information about the Pharo-dev mailing list