micropathic Posted December 2, 2003 Posted December 2, 2003 Let me just start off by saying I'm 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 window's name. I've 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! 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 Quote
*Experts* Bucky Posted December 2, 2003 *Experts* Posted December 2, 2003 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 lhWnd's 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Experts* mutant Posted December 2, 2003 *Experts* Posted December 2, 2003 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. '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 :) Quote
Recommended Posts