Quantcast
Channel: VBForums - API
Viewing all articles
Browse latest Browse all 168

How do I subclass with SetWindowLong? It fails when I try to do this.

$
0
0
Ok so here's my code in my form:
Code:

Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetLastError Lib "kernel32.dll" () As Long
Private Declare Sub SetLastError Lib "kernel32.dll" (ByVal dwErrCode As Long)

Dim OriginalProcPtr As Long

Private Sub Form_Load()
SetLastError 0
OriginalProcPtr = SetWindowLong(Form1.hwnd, AddressOf MyWindowProc, -4)
If OriginalProcPtr = 0 Then
    Print "ERROR "; CStr(GetLastError)
Else
    Print OriginalProcPtr
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
If OriginalProcPtr <> 0 Then SetWindowLong Form1.hwnd, OriginalProcPtr, -4
End Sub

Here's my callback function in my module:
Code:

Public Type WINDOWPOS
    hwnd As Long
    hWndInsertAfter As Long
    x As Long
    y As Long
    cx As Long
    cy As Long
    flags As Long
End Type

Public Sub MyWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As WINDOWPOS)
Form1.Print hwnd
Form1.Print Msg
End Sub

My problem is that the SetWindowLong function ALWAYS FAILS! When run from the VB6 IDE, I keep getting error 1413. When run from a compiled EXE it generates error 0. As you can see from reading my code, this situation can only come about when there's no error, meaning that it has found that the original procedure pointer being found is zero. But using GetWindowLong instead of the return value from SetWindowLong, one quickly finds that the original procedure's pointer is NOT zero.

This leads me to believe that there's a bug in the API function itself.
Not sure if this has anything to do with it, but I'm using Windows 7 x64, rather than Windows XP x86. Is this API function SetWindowLong specifically designed to only work in Windows XP?

Viewing all articles
Browse latest Browse all 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>