Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,806
Office Version
  1. 2016
Platform
  1. Windows
Hi dear excellers,

I have put together this project which subclasses userforms (can be extended to other windows) and I would like to know if it works as expected for trapping window messages. I am particularly interested to know if the project is stable enough. Meaning: It dosen't shuts down the entire excel application if an unhandled error occurs while the form(s) is (are) subclassed or when pressing the Break, Reset or the Design Mode buttons in the VBE ... Subclassing is notoriously limited in vba if not outright inoperational. Hopefully, with this technique, the crashings will be overcome.😇

Clicking the 'Raise Error' button on the first form or right-clicking its titlebar should cause an intentional error to help the testings.

Basically, the project makes use of two dlls that I have written and compiled in TwinBasic (one dll is for x32bit excel and the other one is for x64bit) ... I have embedded the dlls binary data in two modules as base64 strings, just like reources for portability reasons ... The code automatically takes care of everything from decoding the base64 strings to extracting the dll bytes and saving them to the temp directory as dll files.

Please, try resizing, moving the forms as well as right-clicking the form's titlebar to see the action.

Thanks.


File for download:
SubclassDll_ VBA_x32_x64.xlsm



Here is the event handler for the first form :
VBA Code:
Private Sub oSubclass_MessageReceived( _
    hwnd As LongPtr, _
    uMsg As Long, _
    wParam As LongPtr, _
    lParam As LongPtr, _
    dwRefData As LongPtr, _
    bDiscardMessage As Boolean, _
    lReturnValue As LongPtr _
)
    Const WM_SETCURSOR = &H20, WM_GETMINMAXINFO = &H24, WM_CONTEXTMENU = &H7B
    Select Case uMsg
        Case WM_SETCURSOR      'Hover the mouse over the form.
            Debug.Print Me.Name, Format(Now, "hh:mm:ss")
        Case WM_GETMINMAXINFO  'Resize the form.
            Call CheckMinMaxInfo(lParam, bDiscardMessage)
        Case WM_CONTEXTMENU    'Right-click the form titlebar to raise error..
            Debug.Print 1 / 0  'Raising an error shows that excel doesn't crash (doesn't shut down) !!
    End Select
   
End Sub


For the second form :
VBA Code:
Private Sub oSubclass_MessageReceived( _
    hwnd As LongPtr, _
    uMsg As Long, _
    wParam As LongPtr, _
    lParam As LongPtr, _
    dwRefData As LongPtr, _
    bDiscardMessage As Boolean, _
    lReturnValue As LongPtr _
)
   
    Const WM_SYSCOMMAND = &H112, SC_MOVE = &HF012&, WM_NCLBUTTONUP = &HA2
    Const WM_SETCURSOR = &H20, WM_NCMOUSELEAVE = &H2A2
    Select Case uMsg
        Case WM_SYSCOMMAND             'Move the form
            If wParam = SC_MOVE Then
                Label1 = "Sorry, this form is unmovable."
                bDiscardMessage = True 'abort message.
            End If
        Case WM_NCLBUTTONUP            'move the form
            Label1 = ""
        Case WM_SETCURSOR
            If GetAsyncKeyState(VBA.vbKeyLButton) = 0& Then
                Label1 = ""
            End If
         Case WM_NCMOUSELEAVE
            If GetParent(WndFromPoint(hwnd)) <> hwnd Then
               Label1 = "": Call MonitorMouse
            End If
    End Select
   
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi Jaafar:
First, thank you for sharing your knowledge.
You should put your contributions in the articles section so that they do not get lost in the immensity of the threads.


I announce the results of the tests in:
- Windows 11
- Excel 2019
- SO 64 bits, Processor x64


1. resizing Form
OK

2. When clic on form title, the message appears: "Sorry, this form is unmovable."
OK

3. moving the forms as well as right-clicking the form's titlebar to see the action.
OK

4. Raise error. The error occurs and excel does not crash.
OK

:)
 
Upvote 0
Hi Jaafar,

Windows 11
Excel 2021, 64-bit installation

Click the Show UserForm button and Excel exits after about 10 seconds. Event Viewer shows the following error:

Faulting application name: EXCEL.EXE, version: 16.0.18025.20104, time stamp: 0x66f31e2c
Faulting module name: ntdll.dll, version: 10.0.22621.4111, time stamp: 0x518e67bb
Exception code: 0xc0000374
Fault offset: 0x000000000010caa9
Faulting process ID: 0x0x404C
Faulting application start time: 0x0x1DB1350E97EBD6A
Faulting application path: C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE
Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll
Report ID: 86f1ed13-9b19-4308-bf07-e1635980cf41
Faulting package full name:
 
Upvote 0
@DanteAmor
Thank you for testing and glad to hear it worked for you as expected.

@John_w
Does it still crash if you comment out the line: "Call bas_API.HookX64(hwnd)" in the CSubclasser calss code ?

Public Function SubclassWnd(hwnd As LongPtr, Optional dwRefData As LongPtr) As Boolean
#If Win64 Then
Const NULL_PTR = 0^
#Else
Const NULL_PTR = 0&
#End If
Dim hProcAddr As LongPtr
If Not GetProp(hwnd, "Sunbclassed") Then
#If Win64 Then
If GetModuleHandle(sDllOutputPath) = NULL_PTR Then
'Call bas_API.HookX64(hwnd)
End If
#End If
hLib = LoadDll(sDllOutputPath)

Thanks for testing.
 
Upvote 0
Does it still crash if you comment out the line: "Call bas_API.HookX64(hwnd)" in the CSubclasser calss code ?

No, the form is displayed and I can test it.

On clicking the Show UserForm button the TwinBasic logo is displayed for a 3-4 seconds.

Resizable form
I can move, resize, and maximise the form.

Right-click the title bar and the expected division by zero error message occurs.

Click Show Form 2 button
The Unmovable form is displayed after about 3 seconds. Click on title bar and it displays "Sorry, this form is unmovable."

Click Raise Error button
Expected Error 91 message occurs. Click Debug on the error message and can continue code execution from the next line. Can also click Reset, Break and Design mode buttons in the VBE and Excel remains open. I can also edit and add code in the VBE.

In all tests Excel and the VBE are stable.
 
Upvote 0
@John_w

Glad to hear that the code is stable when an error occurs and that the application doesn't shut down.

On clicking the Show UserForm button the TwinBasic logo is displayed for a 3-4 seconds.
Looks like the code for skipping the twinbasic logo (in x64bit) is the culprit. I have looked up the Exception code: 0xc0000374 (STATUS_HEAP_CORRUPTION) error you posted in your previous post on google and I read that it could be related to installed com addins or antivirus software.
 
Upvote 0
Hi Jaafar,

My Excel has no COM add-ins installed. My antivirus software is Microsoft Defender.
 
Upvote 0
Hi Jaafar, no problem, I'm pleased to help.

I'm afraid the update still crashes Excel on clicking the Show UserForm button.

The error is:

Faulting application name: EXCEL.EXE, version: 16.0.18025.20104, time stamp: 0x66f31e2c
Faulting module name: ntdll.dll, version: 10.0.22621.4111, time stamp: 0x518e67bb
Exception code: 0xc0000374
Fault offset: 0x000000000010caa9
Faulting process ID: 0x0x4348
Faulting application start time: 0x0x1DB14334CECA79E
Faulting application path: C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE
Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll
Report ID: b7d07bfa-73a6-4a5d-9474-42f111cc46b8
Faulting package full name:
Faulting package-relative application ID:

If it's any help, the above Application Error is accompanied by 2 Information events from Windows Error Reporting:

Fault bucket , type 0
Event Name: APPCRASH
Response: Not available
Cab Id: 0

Problem signature:
P1: EXCEL.EXE
P2: 16.0.18025.20104
P3: 66f31e2c
P4: StackHash_9e51
P5: 10.0.22621.4111
P6: 518e67bb
P7: c0000374
P8: PCH_B5_FROM_ntdll+0x00000000000A0C84
P9:
P10:


Fault bucket 1739933301241113602, type 4
Event Name: APPCRASH
Response: Not available
Cab Id: 0

Problem signature:
P1: EXCEL.EXE
P2: 16.0.18025.20104
P3: 66f31e2c
P4: StackHash_9e51
P5: 10.0.22621.4111
P6: 518e67bb
P7: c0000374
P8: PCH_B5_FROM_ntdll+0x00000000000A0C84
P9:
P10:
 
Upvote 0
Hi. This is great - try as I might, I could not get it to crash!

I'm on Windows 11.
Microsoft® Excel® for Microsoft 365 MSO (Version 2409 Build 16.0.18025.20030) 64-bit
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top