Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How VB.NET declares API

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly describes how VB. NET API declaration, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian with everyone to understand.

We all know that in VB6 can use API functions to subclass, to handle their own form procedures; if cross-process, this is troublesome, because our function in our process (nonsense), and the target process window message handling function in the target process (or nonsense), so we can only find a way to put our code into the other process to execute-and to tell our process what message. I am afraid that writing assembly is a bit scary, so everyone writes DLL, the principle is to put the callback function into a DLL injected into the other process, DLL to modify the default processing function of the target window-send the message to us.

Of course, there is also an "alternative" point: http://www.it-berater.org/ThueDownloads/index.shtml has a DLL package above, which contains a dssubcls.dll, with which we can easily complete our work: as simple as calling an API, and using callback functions in our programs! Hehe, after saving yourself the trouble of writing DLL, these benefits are enough to attract the audience, right?

Well, VB6 code you can download compressed package found, the author provides a notebook-based example (in the\dssubcls directory), very detailed without a detailed description. The key is how to use it in VB. NET-how to declare APIs, how to make callbacks, see VB6 declarations for subclassed APIs 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 translation to VB. NET declarative API looks something like this (convention dictates that I expand & to As Integer):

Declare Function SubClass Lib "dssubcls" (ByVal HwndSubclass As Integer, Optional ByVal Address As Integer = 0, 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 Integer

Isn't that great? The problem is that such a declaration can be passed in VB6 using the Addressof function to pass the second parameter (see the source code you downloaded), but in VB. NET it is not directly Addressof-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 delegation corresponds to the following function:

Private Function mCallback(ByVal wMsg As Integer,

ByVal wParam As Integer, ByVal lParam As Integer) As Integer

'Get the message processed here

End Function

When using, you need to pay attention to instantiate 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, equivalent to VB6 inside the Addressof function to get the result, seems to solve the problem, so we wrote the following code to get each other's process form message:

SubClass(Handle, fix_COCD, 0, 0, 1) 'modify handler

The questions really came one after another! IDE prompt variable type mismatch!! This is true. We pass a HookCallBack type as an Integer, which cannot be checked, so why not force the conversion? Sure, you can try it. So what I did was I changed this API statement:

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

Thank you for reading this article carefully. I hope that the article "How to declare API in VB. NET" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report