主题:[讨论][千古奇案]几乎相同的代码,一个可以运行,另一个却不行!?
[code=c]
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private preWndProc As Long
Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal lParam As Long, ByVal wParam As Long) As Long
Form1.Label1.Caption = Msg
WndProc = CallWindowProc(preWndProc, hwnd, Msg, wParam, lParam)
End Function
Public Function SubClassing(ByVal hwnd As Long) As Long
If hwnd <> 0 Then
preWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End If
SubClassing = 0
End Function
Public Function UnSubClassing(ByVal hwnd As Long) As Long
If hwnd <> 0 Then
Call SetWindowLong(hwnd, GWL_WNDPROC, preWndProc)
End If
UnSubClassing = 0
End Function
[/code]
上面的是运行不了的代码。一运行VB6就出错退出,生成的exe文件竟然被瑞星当作病毒杀掉。
下面的是可以运行的另一个工程的代码
[code=c]
Option Explicit
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim oldproc As Long
Public Function RegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
oldproc = SetWindowLong(hwnd, -4, AddressOf WinProc)
End If
End Function
Public Function unRegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
SetWindowLong hwnd, -4, oldproc
End If
End Function
Public Function WinProc(ByVal hwnd As Long, ByVal msg As Long, ByVal lpara As Long, ByVal wpara As Long) As Long
Form1.Label1.Caption = msg
WinProc = CallWindowProc(oldproc, hwnd, msg, lpara, wpara)
End Function
[/code]
比较两组代码,区别仅仅在于
1、API声明为public和private
2、函数命名
难道这两个原因会有影响么?[em10][em10]
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private preWndProc As Long
Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal lParam As Long, ByVal wParam As Long) As Long
Form1.Label1.Caption = Msg
WndProc = CallWindowProc(preWndProc, hwnd, Msg, wParam, lParam)
End Function
Public Function SubClassing(ByVal hwnd As Long) As Long
If hwnd <> 0 Then
preWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End If
SubClassing = 0
End Function
Public Function UnSubClassing(ByVal hwnd As Long) As Long
If hwnd <> 0 Then
Call SetWindowLong(hwnd, GWL_WNDPROC, preWndProc)
End If
UnSubClassing = 0
End Function
[/code]
上面的是运行不了的代码。一运行VB6就出错退出,生成的exe文件竟然被瑞星当作病毒杀掉。
下面的是可以运行的另一个工程的代码
[code=c]
Option Explicit
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim oldproc As Long
Public Function RegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
oldproc = SetWindowLong(hwnd, -4, AddressOf WinProc)
End If
End Function
Public Function unRegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
SetWindowLong hwnd, -4, oldproc
End If
End Function
Public Function WinProc(ByVal hwnd As Long, ByVal msg As Long, ByVal lpara As Long, ByVal wpara As Long) As Long
Form1.Label1.Caption = msg
WinProc = CallWindowProc(oldproc, hwnd, msg, lpara, wpara)
End Function
[/code]
比较两组代码,区别仅仅在于
1、API声明为public和private
2、函数命名
难道这两个原因会有影响么?[em10][em10]