Hallo,
ich möchte mit einer Anwendung (Projekt1) eine andere Anwendung (Projekt2) steuern (z.B. in Projekt2 eine Prozedur starten).
Ich habe auf der Grundlage eines VB6 Beispieles die entsprechenden Anwendungen (Code weiter unten) aufgebaut.
Für einen bestimmten Zeitraum funktioniert das Ganze. Nach einiger Zeit erhalten ich jedoch in Projekt2 (Destination) immer wieder die Fehlermeldung
"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt".
Kann mir jemand sagen wo der Fehler liegt? Oder sollte man in VB.net einen grundsätzlich anderen Ansatz für die Aufgabe wählen?
Bei Interesse würde ich gerne beide Projekte komplett zumailen.
[u]Projekt1
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _
lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByRef lParam As Integer) As Integer
Const WM_COPYDATA As Integer = &H4AS
Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal _
eventArgs As System.EventArgs) Handles Command3.Click
Call SendData()
End Sub
Private Function SendData() As Boolean
Dim DesthWnd As Integer
DesthWnd = FindWindow(vbNullString, "Destination")
If DesthWnd = 0 Then
MsgBox("Das Zielfenster wurde nicht gefunden" & vbCrLf & "Bitte" & _
"Starten sie erst das andere Projekt!")
Else
Call SendMessage(DesthWnd, WM_COPYDATA, Me.Handle.ToInt32, 3)
End If
End Function
End Class [u]Projekt2
Option Strict Off
Option Explicit On
Friend Class Form2
Inherits System.Windows.Forms.Form
Private Declare Function CallWindowProc Lib "user32" Alias _
"CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hWnd As Integer, _
ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) _
As Integer
Private Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal _
msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal lVal As _
SubClassProcDelegate) As Integer
Const GWL_WNDPROC As Integer = -4
Const WM_COPYDATA As Integer = &H4AS
Dim PrevWndProc As Integer
Dim s As Integer
Private Sub Form2_Load(ByVal eventSender As System.Object, ByVal eventArgs _
As System.EventArgs) Handles MyBase.Load
Call Init(Me.Handle.ToInt32)
End Sub
Public Sub Color_Change()
If Me.Text1.BackColor.ToString = "Color [Red]" Then Me.Text1.BackColor _
= System.Drawing.Color.White Else Me.Text1.BackColor = _
System.Drawing.Color.Red
End Sub
Private Function WindowProc(ByVal hWnd As Integer, ByVal Msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
If Msg = WM_COPYDATA Then
Call Color_Change()
End If
WindowProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)
End Function
Public Sub Init(ByRef hWnd As Integer)
'UPGRADE_WARNING: Einen Delegaten für AddressOf WindowProc hinzufügen
' Klicken Sie hier für weitere Informationen:
' 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1048"'
If PrevWndProc = 0 Then
PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End If
End Sub
End Class Danke |