Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / April 2007



Tip: Looking for answers? Try searching our database.

Trying to catch Messages in a window other then my current application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edmond3CD@gmail.com - 18 Apr 2007 19:23 GMT
I'm a bit new to subclassing and here is what I'm trying to do.

I have an application which reads data from two other windows, called
Trade1 and Trade2.  I use some OCR stuff which basically scraps those
screens for data.

Now, everything works fine, but I have a problem in that I have a very
annoying 'flicker' happening.

What I'm trying to do, is to subclass API SendMessage to prevent the
flicker on the 2 windows (Not my current VB6 Application).  I want the
Trade1 and Trade2 Windows to never get focus, but to allow scraping if
required).

Here is what I've got so far:
Code from :  http://www.developerfusion.co.uk/show/49/2/

'// variable that stores the previous message handler
Public ProcOld As Long
Public Const GWL_WNDPROC = (-4)
'// Windows API Call for catching messages
Public Declare Function SetWindowLong Lib "user32" Alias
"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal
dwNewLong As Long) As Long
'// Windows API call for calling window procedures
Public Declare Function CallWindowProc Lib "user32" Alias
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long,
ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As
Long

Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
   Select Case iMsg
   Case else
       '// Ignore all messages for the moment
   End Select
   '// pass all messages on to VB and then return the value to
windows
   WindowProc = CallWindowProc(procOld, hWnd, iMsg, wParam, lParam)
End Function

Form Code

'// form_load event. Catch all those messages!
Private Sub Form_Load()
   On Error Resume Next
   '// saves the previous window message handler. Always restore this
value
   '// AddressOf command sends the address of the WindowProc
procedure
   '// to windows
   ProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

'// form_queryunload event. Return control to windows/vb
Private Sub Form_Unload(Cancel As Integer)
   '// give message processing control back to VB
   '// if you don'//t do this you WILL crash!!!
   Call SetWindowLong(hWnd, GWL_WNDPROC, procOld)
End Sub

' --------------------------------------------

However, the above code only seems to set the trapping within the
current application.
I've been trying all day to modify the above code and get it to also
allow me to intercept the messages on Trade1 and Trade2 window, but so
far no luck.

Is what I'm trying to do possible, and can the above code be modified
to make this happen?
MikeD - 18 Apr 2007 20:14 GMT
> I'm a bit new to subclassing and here is what I'm trying to do.
>
[quoted text clipped - 15 lines]
> Is what I'm trying to do possible, and can the above code be modified
> to make this happen?

Nope and nope. VB doesn't support out-of-process subclassing.

Signature

Mike
Microsoft Visual Basic MVP

VBSam - 18 Apr 2007 20:52 GMT
Is there any way to prevent the system from sending a WM_NCACTIVATE command
to an application which is already running on a computer?
Kevin Provance - 19 Apr 2007 02:22 GMT
Yes.  See my reply to the OP.

- Kev

| Is there any way to prevent the system from sending a WM_NCACTIVATE command
| to an application which is already running on a computer?
Kevin Provance - 19 Apr 2007 02:22 GMT
Yoou're going to need something like Spyworks (www.desaware.com) for out of
process subclassing.

- Kev

| I'm a bit new to subclassing and here is what I'm trying to do.
|
[quoted text clipped - 67 lines]
| Is what I'm trying to do possible, and can the above code be modified
| to make this happen?
mr_unreliable - 19 Apr 2007 17:36 GMT
hi Edmond,

Rather than subclassing, I suggest that you read up on "hooking".

http://msdn2.microsoft.com/en-us/library/ms644959.aspx#whgetmessagehook

More specifically, the wh_getmessage hook.  If it looks like
hooking will work for you, then I have a couple of other
suggestions:

1. With wh_getmessage, an enormous number of messages will be
flowing through your winproc, and so you will need to make it
as short and efficient as you can.  Even so, your system is
likely to slow down.

2. After you figure out the best way to deal with the "flicker",
then "get-the-hook-out" of your code.  There is no way you want
to run with a hook inserted all the time.

cheers, jw

> I'm a bit new to subclassing and here is what I'm trying to do.
>
[quoted text clipped - 67 lines]
> Is what I'm trying to do possible, and can the above code be modified
> to make this happen?
Karl E. Peterson - 19 Apr 2007 18:15 GMT
> More specifically, the wh_getmessage hook.

I've never used one of those, but it actually sounds useful for a project in my
queue.  It appears that would require a standard export if the hook is
out-of-process, but can just use a AddressOf callback if it's in-process?
Signature

.NET: It's About Trust!
http://vfred.mvps.org

mr_unreliable - 19 Apr 2007 18:39 GMT
>> More specifically, the wh_getmessage hook.
>
> I've never used one of those, but it actually sounds useful for a project in my
> queue.  It appears that would require a standard export if the hook is
> out-of-process, but can just use a AddressOf callback if it's in-process?

I would hesitate to provide any un-vetted suggestions to the
famous Karl Peterson, but as best I recall an addressof will do it.

Consider the more well-known cbt hook.  That works with a normal
callback address.

And take a look at this:

http://www.codeguru.com/cpp/com-tech/shell/comments.php/c4509/?thread=52714

That is c++ code, but it looks like he is just providing a normal
callback address, to get look at all the system messages.

cheers, jw
Karl E. Peterson - 19 Apr 2007 18:50 GMT
>>> More specifically, the wh_getmessage hook.
>>
[quoted text clipped - 4 lines]
> I would hesitate to provide any un-vetted suggestions to the
> famous Karl Peterson,

Please don't!  I certainly enjoy helping others here, but as much as anything I hang
out to *learn* as well!  :-)

> but as best I recall an addressof will do it.
>
> Consider the more well-known cbt hook.  That works with a normal
> callback address.

Yep, I've used those, too.  But never really had the need to for WH_GETMESSAGE
before.  Could be really useful for a problem I'm running into, though.

> And take a look at this:
>
> http://www.codeguru.com/cpp/com-tech/shell/comments.php/c4509/?thread=52714
>
> That is c++ code, but it looks like he is just providing a normal
> callback address, to get look at all the system messages.

I'm going to have play with this!  Thanks for the push.  :-)
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Dick Grier - 19 Apr 2007 19:59 GMT
Hi Karl,

AFAIK, you need a helper DLL.  I have used SpyWorks for this.  I remember
that Appleman, and perhaps someone else, had a free version -- but I haven't
been able to locate it online.

Dick

Signature

Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Karl E. Peterson - 19 Apr 2007 20:01 GMT
> AFAIK, you need a helper DLL.  I have used SpyWorks for this.  I remember
> that Appleman, and perhaps someone else, had a free version -- but I haven't
> been able to locate it online.

I'm pretty sure that's for out-of-process tasks, though, right?
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Dick Grier - 20 Apr 2007 20:36 GMT
Hi Karl,

I'm pretty sure that's for out-of-process tasks, though, right?
<<

Right.  Sorry, I though that was the topic.

Dick

Signature

Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Kevin Provance - 23 Apr 2007 03:41 GMT
Yes

| > AFAIK, you need a helper DLL.  I have used SpyWorks for this.  I remember
| > that Appleman, and perhaps someone else, had a free version -- but I haven't
| > been able to locate it online.
|
| I'm pretty sure that's for out-of-process tasks, though, right?
Ken Halter - 19 Apr 2007 22:14 GMT
> Please don't!  I certainly enjoy helping others here, but as much as
> anything I hang out to *learn* as well!  :-)

Even though you've mentioned "too much hassle" regarding vbaccelerator
stuff, here's one with (and get this) no extra dependencies <g>

Win32 Hooks in VB - The vbAccelerator Hook Library
http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/vbAccelerator_Hook_Lib
rary/article.asp


Sample code is included, but, just in case you miss the "too much hassle"
aspect, here's a sample that uses the DLL (you build) above.

Using a Journal Record Hook to Capture Mouse and Key Events from any System
Window
http://vbaccelerator.com/home/VB/Code/Libraries/Hooks/Journal_Record_Hooks/artic
le.asp


Signature

Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm

Karl E. Peterson - 19 Apr 2007 22:32 GMT
Hi Ken --

>> Please don't!  I certainly enjoy helping others here, but as much as
>> anything I hang out to *learn* as well!  :-)
[quoted text clipped - 4 lines]
> Win32 Hooks in VB - The vbAccelerator Hook Library
> http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/vbAccelerator_Hook_Lib
rary/article.asp

Just *love* the intro!

 "When a subclass isn't tough enough for the job, its time to move to an even
lower-level and more disruptive technique."

<LOL!>

Maybe I'm missing something, but later down the page, he also says:

 "Before you get excited, though, VB on its own cannot be used to create a
system-wide hook. This is because the hook procedure must reside within a Windows
DLL, and VB cannot create these beasts (because you cannot specify to export the
HookProc function)."

Note, I'm not necessarily after a systemwide hook, but I think this drives home my
original point about this stuff mainly working in-process with "pure VB" solutions.

> Sample code is included, but, just in case you miss the "too much hassle"
> aspect, here's a sample that uses the DLL (you build) above.
>
> Using a Journal Record Hook to Capture Mouse and Key Events from any System
> Window
> http://vbaccelerator.com/home/VB/Code/Libraries/Hooks/Journal_Record_Hooks/artic
le.asp

This /is/ kind of interesting, though.  I haven't picked it apart yet, but
definitely looks like a way to get at those global UI events.  :-)

Thanks...   Karl
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Ken Halter - 19 Apr 2007 23:09 GMT
> Maybe I'm missing something, but later down the page, he also says:
>
>  "Before you get excited, though, VB on its own cannot be used to create a
> system-wide hook. This is because the hook procedure must reside within a
> Windows DLL, and VB cannot create these beasts (because you cannot specify
> to export the HookProc function)."

I believe he's talking about creating your own "all new" hook... the code
supplied relies on hooks the OS already supports, which are contained in the
"beasts" that come with the OS.... but, yeah <g> He has a pretty good sense
of humor.... fwiw, the code is pretty easy to implement, but I've had to
reboot a time or two because of a bug on my side.

TaskManager isn't enough when you improperly hook the creation of new
windows <g> Just another reason I like PCs that have a reset button <g>

Signature

Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm

Juergen Thuemmler - 22 Apr 2007 22:51 GMT
> This /is/ kind of interesting, though.  I haven't picked it apart yet, but
> definitely looks like a way to get at those global UI events.  :-)

Have a look also at
http://allapi.mentalis.org/vbexamples/list.php?category=MISC, DSCWPMSG,
DSSUBCLS

Juergen.
Scott Seligman [MSFT] - 19 Apr 2007 19:40 GMT
>>> More specifically, the wh_getmessage hook.
>>
[quoted text clipped - 15 lines]
>That is c++ code, but it looks like he is just providing a normal
>callback address, to get look at all the system messages.

A WH_GETMESSAGE hook pointed at another process (or all processes) needs
to live in a standard DLL. The code in your link is for a DLL.

As far as I know, there's no way to create this sort of hook completely
in VB. Generally the DLL needs to be written in C or C++. Of course a VB
app can be used to setup the hook and provide any UI.

Signature

Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.

mr_unreliable - 19 Apr 2007 20:51 GMT
Go here:

  http://allapi.mentalis.org/vbexamples/list.php?category=MISC

and look for: "dscwpmsg.dll"

cheers, jw
Karl E. Peterson - 19 Apr 2007 21:14 GMT
> Go here:
>
>   http://allapi.mentalis.org/vbexamples/list.php?category=MISC
>
> and look for: "dscwpmsg.dll"

Definitely not a VB-authored DLL, there...?  (Not saying it's not a solution, just
not a native one.  Still, nice to know it's freely available.)
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Steve - 20 Apr 2007 16:04 GMT
> > Go here:
>
[quoted text clipped - 7 lines]
> .NET: It's About Trust!
>  http://vfred.mvps.org

Falling into the "not worth the hassle" category would be this little
tid bit which would allow you to create a standard windows dll (not
ActiveX) with VB.
http://www.windowsdevcenter.com/pub/a/windows/2005/04/26/create_dll.html

Steve
Karl E. Peterson - 20 Apr 2007 17:47 GMT
>>> Go here:
>>
[quoted text clipped - 9 lines]
> ActiveX) with VB.
> http://www.windowsdevcenter.com/pub/a/windows/2005/04/26/create_dll.html

Yeah, I messed with that one a few years ago.  Wasn't pretty.  At all.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

DanS - 20 Apr 2007 22:06 GMT
>> Go here:
>>
[quoted text clipped - 5 lines]
> solution, just not a native one.  Still, nice to know it's freely
> available.)

There are a few different DLL's for hooks there. I tried them out for my
shell replacement project. The one thing I didn't like about them is that I
was looking for a lean DLL, and not an 'encompass all' solution.

I used Power Basic v8 to make a DLL for global mouse hooking only, and it
worked out quite well. The DLL created is a standard DLL and using it in my
VB project was just as any other API call.

At $200 PB is a little steep for just a hobbyist, amd I'm not suggesting
you buy it for your one need, but the compiler seems pretty decent. Not as
RAD as VB though.
Karl E. Peterson - 20 Apr 2007 22:47 GMT
> "Karl E. Peterson" <karl@mvps.org> wrote in
>> Definitely not a VB-authored DLL, there...?  (Not saying it's not a
[quoted text clipped - 12 lines]
> you buy it for your one need, but the compiler seems pretty decent. Not as
> RAD as VB though.

Yeah, I've got PB here, too.  It's one of my first thoughts, when this subject comes
up.  I get frustrated with it, generally, but it definitely fills the need in cases
like this!
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Kevin Provance - 23 Apr 2007 03:39 GMT
Yes.

If you want to talk out of procee subclass Karl, that happens to be one of
my fields of knowledge.  Write anytime, I'd be happy to return the favour.

- Kev

| > More specifically, the wh_getmessage hook.
|
| I've never used one of those, but it actually sounds useful for a project in my
| queue.  It appears that would require a standard export if the hook is
| out-of-process, but can just use a AddressOf callback if it's in-process?
Karl E. Peterson - 25 Apr 2007 18:50 GMT
> "Karl E. Peterson" <karl@mvps.org> wrote...
>>> More specifically, the wh_getmessage hook.
[quoted text clipped - 7 lines]
> If you want to talk out of procee subclass Karl, that happens to be one of
> my fields of knowledge.  Write anytime, I'd be happy to return the favour.

That's cool, Kevin.  I'll keep that in mind!  (It's actually an in-proc thing I'm
battling at the moment!)

Thanks...   Karl
Signature

.NET: It's About Trust!
http://vfred.mvps.org

mr_unreliable - 20 Apr 2007 18:22 GMT
One final side-light concerning hooking, i.e., one way
to hook WITHOUT calling set hook.

Back in 2001, as a result of microsoft's having lost the lawsuit
over allegedly "secret" api interfaces, microsoft then published
a listing of the previously secret "Undocumented-and-Unsupported"
interfaces here:

  http://msdn2.microsoft.com/en-us/library/ms807073.aspx

Among those interfaces were two interfaces called:

  - RegisterShellHook
  - DeRegisterShellHook

These routines provide a "back-door" way of hooking.  Just as
a speculation, but I suspect that ms was itself using this way
of hooking in shell32.dll (without the separate dll).

cheers, jw

p.s. On the same page as mentioned above, microsoft declares
those interfaces (ALL the ones mentioned) as "deprecated".
I don't know if I fully understand what "deprecated" means,
but I gather it means: "here are the interfaces, and what they
do, but DON'T USE THEM -- because we could withdraw them at
any time.  This makes for a somewhat "hollow victory" for
those winners of that lawsuit...
Karl E. Peterson - 20 Apr 2007 18:57 GMT
> One final side-light concerning hooking, i.e., one way
> to hook WITHOUT calling set hook.
[quoted text clipped - 14 lines]
> a speculation, but I suspect that ms was itself using this way
> of hooking in shell32.dll (without the separate dll).

Some interesting stuff, there, too!

How's about HSHELL_RUDEAPPACTIVATEED?!? <LOL>

> p.s. On the same page as mentioned above, microsoft declares
> those interfaces (ALL the ones mentioned) as "deprecated".
[quoted text clipped - 3 lines]
> any time.  This makes for a somewhat "hollow victory" for
> those winners of that lawsuit...

Yeah, means these may or may not be in Vista.  Ya just never know.

Thanks...   Karl
Signature

.NET: It's About Trust!
http://vfred.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.