Windows API

pitaridis

Well-known member
Joined
Nov 18, 2005
Messages
63
Programming Experience
10+
I would like to have some help because I have been trying to convert the following code from VB6 to VB.NET but I have not achieved it yet.

VB.NET:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_GETSELITEMS = &H191
Dim ItemIndexes() As Long, x As Integer, iNumItems As Integer

Private Sub Command1_Click()

iNumItems = List1.SelCount
If iNumItems Then
ReDim ItemIndexes(iNumItems - 1)
SendMessage List1.hwnd, LB_GETSELITEMS, iNumItems, ItemIndexes(0)
End If
For x = 0 To iNumItems - 1
List2.AddItem List1.List(ItemIndexes(x))
Next x
End Sub

My problem is the lParam param which is type of Any and I can not find a way to set a VB.NET equivalent data type in order to make this work. I have done it 2 years ago because I wanted to make exactly the same thing but I have not this project to see how I did it.

The above example uses the windows api to get the selected items of a listbox. I need to make it like this because I have the window handle and I have to use this information to get the selected items.

Thanks
 
I find this application a good starting point to get any API declarations: ActiveVB - ApiViewer (en) (remember to set syntax to VB.Net in options)
The 'Any' data type in VB now is Object, but you should use the exact types you require, and in case you require multiple different uses you should overload the declaration specifically. This as a general rule of programming to prevent accidentally putting things where they don't belong.
 
Thank you for your suggestion but the declaration of the function in the application is the following.

VB.NET:
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

The problem is that the last parameter of the SendMessage function has a lot of forms. Some times it is handled as number and some other times as string or as array of strings. In my case the function should define this parameter as an array of string. I have searched a lot but I did not find something. 2 years ago I had found the solution for this problem but unfortunately I can not find the project that I had used this code. I would really appreciate if someone help me to find a solution for my problem.
 
As I said, I recommend you overload the declaration with the exact signatures you need in all specific cases.
The suggested signature is the generic one, where you may marshal the values by pointers, actually IntPtr would be a better type than Int32 in many cases.
For SendMessage that is used in different contexts and allows various types of arguments you may overload the function to accept those specifically, for example where lParam could be String type and you pass a String value directly to the function call.
 
To pass a string variable is an easy thing. How could I pass an array of strings as parameter? In this case the SendMessage expects a pointer to the array of string but I do not know how to do it in VB.NET.
 
Last edited:
Have you tried declaration the parameter type as String array?
 
I have tried and the program crashes.
 
What message are you sending?
 
Back
Top