"VB.net" also talk about cross-process message hooks

Source: Internet
Author: User
Tags integer

Write to vb.net programmer ^_^

Cliché: Subclass.

We all know that you can use API functions to subclass in VB6, to handle its own form process; If you cross the process, that's troublesome, because our function is in our process (nonsense), while the target process's window's message handler function is in the target process (or nonsense), So we can only find a way to put our code into the process of the other party-and tell us what the process is getting. I'm afraid the compilation is a bit scary, so everyone writes a DLL, the principle is to put the callback function into a DLL into the other process, DLL to modify the target window default processing function-Send the message to us.

Of course, there are "alternative" point: http://www.it-berater.org/ThueDownloads/ Index.shtml has a DLL package, which contains a dssubcls.dll, which can be easily done with our work: just as simple as calling an API, and using callback functions in our programs! Oh, save yourself the trouble of writing a DLL, these benefits are enough to attract viewers?

Well, VB6 's code can be found in the downloaded Zip package, and the author provides a Notepad based instance (in the \dssubcls directory) that is not detailed in detail. The key is how to use it in vb.net-how to declare the API, how to make callbacks, see the VB6 declaration of the API used to subclass the first:

Declare Function subclass& Lib "Dssubcls" (ByVal Hwndsubclass&, _

Optional ByVal address& = 0, _

Optional ByVal oldstyle& = 0, _

Optional ByVal newstyle& = 0, _

Optional ByVal ext& = 0, _

Optional ByVal subclass& = 0)

The declaration that translates into vb.net is similar to the following (the habit dictates that I expand & into as Integer):

Declare Function Subclass Lib "Dssubcls" (ByVal hwndsubclass As Integer, Optional ByVal address As Integer = 0, Optional B Yval oldstyle As Integer = 0, Optional ByVal newstyle As Integer = 0, Optional ByVal Ext As Integer = 0, Optional ByVal Su Bclass As Integer = 0) As Integer

Isn't that good? The problem is, such a statement can use the AddressOf function in VB6 to pass in the second parameter (see the source code you downloaded), but AddressOf directly inside the vb.net is out of the question-we need to delegate a callback:

Private Delegate Function hookcallback (ByVal wmsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

This delegate corresponds to the following function:

Private Function Mcallback (ByVal wmsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

' Processing the message here

End Function

When used, it is necessary to take note of instantiating this delegate first:

Private Fix_cocd = New hookcallback (AddressOf mcallback)

At this point, FIX_COCD is our mcallback function reference, with a more intuitive point of view, FIX_COCD is a pointer to Mcallback, the equivalent of VB6 inside the AddressOf function to get the result, seemingly problem solved, So we wrote the following code to get each other's process form message:

Subclass (Handle, FIX_COCD, 0, 0, 0, 1) ' Modify the handler function

The problem is really coming! IDE hint variable type does not match!! This is true, we pass a hookcallback type as an integer, not through the check, then force the conversion? Of course, you can go and try. At this point, what I did was, modify this API declaration:

Private Declare Function Subclass Lib "Dssubcls" (ByVal Hwndsubclass as Integer, Optional ByVal address as Hookcallback = Nothing, Optional ByVal oldstyle As Integer = 0, Optional ByVal newstyle As Integer = 0, Optional ByVal Ext As Integer = 0 , Optional ByVal Subclass as Integer = 0) as Integet

Make it conform to our call? A little perverse? Not so, when you are accustomed to modifying API statements, you will find that some things have become so simple that some things need to be recognized again-for the WIN32 API.

So far, done:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.