How can I make this code work in .net

micropathic

Well-known member
Joined
Oct 23, 2003
Messages
75
Let me just start off by saying Im a bit of a noob and I know my code is pretty. However for learning purposes I just need to figure out how to make the following code work in .net.

The code is designed to kill another instance of an app using sendmessage and the windows name. Ive searched the forum for a better alternative to using sendmessage, but had no luck with the code I found. The problem with the code right now seems to be with "lhWnd"s type as vb.net throws an invalid cast exception when I try to run it. Any suggestions?

Thanks in advance!




[VB]
Const WM_CLOSE As Integer = &H10s
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Integer





Private Sub Form_Load()

If (UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0) Then
GoTo ChangeCaptionName
Else
GoTo SkipChangeCaptionName
End If

ChangeCaptionName:

Dim lhWnd As long
Dim r As Short


Me.Text = "*" & Me.Text

lhWnd = FindWindow(vbNullString, "Rubber Stamped Sync")

If lhWnd <> 0 Then
MsgBox lhWnd

*************LINE BELOW THIS IS THE PROBLEM*********
r = SendMessage(lhWnd, WM_CLOSE, 0, 0)

MsgBox lhWnd

End If
MsgBox lhWnd
Me.Text = Mid(Me.Text, 2)


SkipChangeCaptionName:

End Sub

[/VB]
 
Your lhWnd variable is of the Long type. The API declaration for
SendMessage specifies the first parameter as an IntPtr type. You
need to either:

a.) Change lhWnds Dim to the IntPtr type, and change the declaration
of FindWindow to return an IntPtr (this is more proper, since an
hWnd is more specific as an IntPtr than just an Integer),

Or:
b.) Change lhWnd to an Integer type, and change the declaration
of SendMessage to have the first parameter (hWnd) be an
Integer.

Integers and IntPtrs are interchangeable for passing
between .NET apps and Windows APIs, but within the app itself
the types need to match up.

You should also declare r as an Integer.
 
Im assuming that you wish to close all instances of your program. Here is one other way you could do it.
It eliminates the use of GoTo and the FindWindow API.
Code:
decalre the API like you already did
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
declare a process array and fill it with each process that matches the name
Dim processes() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
loop through each returned process
For Each p As Process In processes
    send the close message to each window with the same process name
    SendMessage(p.MainWindowHandle, &H10S, 0, 0)
Next
End Sub
:)
 
Back
Top