cristianbuse / VBA-StateLossCallback

A class that allows a callback when state is lost
MIT License
12 stars 4 forks source link

[FeatureRequest] Add a Cancel method #4

Closed thefasquelle closed 1 month ago

thefasquelle commented 3 months ago

Hi, thank you for this awesome class ! I needed a way to cancel the callback, and I think it could benefit other people too.

I did it by adding an Enabled property that is set to True in Init and checked in DataObject_Clear

Private Sub DataObject_Clear()
    If (Not m_data Is Nothing) Or (Not Enabled) Then Exit Sub 'Ignore external logic attempts

I also added a Cancel sub that set Enabled to False but that's not really necessary.

Maybe there is a better way that actually cancel the callback instead of just making it exit immediately ?

cristianbuse commented 3 months ago

Thanks @thefasquelle for the kind words and the feedback!

Your approach is probably what I would also do. The alternative would be to restore the correct IUnknown:Release and IUnknown:AddRef pointers in the virtual function table for the DataObject interface while also correcting the reference count.

Will try to do some work on this class in the next few weeks.

The biggest problem with this class is that the callback does not work when End is called while code is running - this might be something that has no solution but will see.

thefasquelle commented 3 months ago

The biggest problem with this class is that the callback does not work when End is called while code is running - this might be something that has no solution but will see.

yeah, the main reason I used this callback is for unhandled errors where user press the End button. Sadly it does not work And I would be surprised you can do it without hacking into the VBA runtime itself (like vbWatchdog does). At least it works when using the stop button, which happens a lot when your users are also developers !

cristianbuse commented 1 month ago

Thanks for the suggestion! Added a Cancel method.