我要用程序2取得程序1的消息,但每次消息产生时,就出现内存地址不能为READ的错误,请问程序哪里的错,要怎么写才能正确程序1:
用TIME控件每10秒为MSFlexGrid加入一条记录(内容随意)窗口标题为Form1程序2:
模块:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongType YearType  '要查找的窗口句柄类
    MainWindow As Long '主窗口
    Rttitle As String * 256 '窗口标题
    BottWindow As Long '按键窗口
    MsgWindow As Long '消息窗口
End TypePublic FindYear As YearType
Public YearProcessID As Long  
Public YearThreadID As Long   
Public YearMsgHook As Long    
Public InstallHookED As Integer 
Public MsgNumber As Long Public Const WH_GETMESSAGE = 3
Public Const WH_CALLWNDPROC = 4
'消息处理过程
Public Function YearProcess(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'Form1.MSFlexGrid2.AddItem MsgNumber & vbTab & ncode & vbTab & wParam & vbTab & iparamYearProcess = 0
sgNumber 1
Call CallNextHookEx(YearMsgHook, ncode, wParam, iparam)
End Function窗体部份:
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "FindProcess"
   ClientHeight    =   3150
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3150
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   2280
      TabIndex        =   3
      Text            =   "Text1"
      Top             =   120
      Width           =   2295
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
      Height          =   2535
      Left            =   0
      TabIndex        =   2
      Top             =   600
      Width           =   4695
      _ExtentX        =   8281
      _ExtentY        =   4471
      _Version        =   393216
      Appearance      =   0
   End
   Begin VB.CommandButton Command2 
      Caption         =   "取消挂接"
      Height          =   375
      Left            =   1200
      TabIndex        =   1
      Top             =   120
      Width           =   975
   End
   Begin VB.CommandButton Command1 
      Caption         =   "挂接程序"
      Height          =   375
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   975
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
FindYear.MainWindow = FindWindow(vbNullString, "Form1")
If FindYear.MainWindow > 0 Then
    Text1.Text = FindYear.MainWindow
    YearThreadID = GetWindowThreadProcessId(FindYear.MainWindow, YearProcessID)
    If YearThreadID > 0 Then
        YearMsgHook = SetWindowsHookEx(WH_GETMESSAGE, AddressOf YearProcess, App.hInstance, YearThreadID)
        InstallHookED = 1
    Else
    MsgBox "ThreadID没有找到"
    End If
Else
MsgBox "Form1窗口没有找到"
End If
End SubPrivate Sub Command2_Click()
If InstallHookED = 1 Then
    UnhookWindowsHookEx (YearMsgHook)
    InstallHookED = 0
End IfEnd SubPrivate Sub Form_Load()
InstallHookED = 0
End SubPrivate Sub Form_Unload(Cancel As Integer)
If InstallHookED = 1 Then
    UnhookWindowsHookEx (YearMsgHook)
    InstallHookED = 0
End If
End Sub