PDA

View Full Version : How to convert this (so it works)?



Clive Richmond
4-Sep-2018, 05:05 AM
We have a method to process certain messages in the windows queue using PeekMessage. The original source was from a package published by Christian Berkhout and later modified by Garret Mott (https://support.dataaccess.com/Forums/showthread.php?1002-Modified-ProcessMessages-Package&highlight=processmessages).

Since it uses GetBuff we decided to update it for 19.1. However, the new code will eventually crash triggering an 'Access violation error.' Not sure what have missed but does anyone know how to fix it?

Old Code.

TYPE tMsg
Field tMsg.hwnd As Handle
Field tMsg.message As Integer
Field tMsg.wParam As Integer
Field tMsg.lParam As Integer
Field tMsg.Time As DWord
Field tMsg.pt As Pointer
END_TYPE

Function ProcessMessage GLOBAL Returns Boolean
Boolean bMessageFound bTemp
Pointer pMsg
Integer iPendingMessage
String sMsg

Move (False) To bMessageFound
ZeroType tMsg To sMsg
Move (AddressOf(sMsg)) To pMsg
If (PeekMessage(pMsg, 0, 0, 0, PM_REMOVE)) Begin
GetBuff From sMsg at tMsg.message To iPendingMessage
If (iPendingMessage = WM_PAINT) Begin
Move (True) To bMessageFound
Move (TranslateMessage(pMsg)) To bTemp
Move (DispatchMessage(pMsg)) To bTemp
End
End

Function_Return bMessageFound
End_Function // ProcessMessage


New Code.

Use tWinStructs.pkg

Struct tMsg
Handle hWndOwner
Integer iMessage
Integer wParam
Integer lParam
DWord dwTime
tWinPoint ltPoint
DWord dwPrivate
End_Struct

Function ProcessMessage GLOBAL Returns Boolean
tMsg ltMsg
Boolean bMessageFound bTemp
Pointer pMsg

Move (False) To bMessageFound
Move (AddressOf(ltMsg)) To pMsg
If (PeekMessage(pMsg, 0, 0, 0, PM_REMOVE)) Begin
If (ltMsg.iMessage = WM_PAINT) Begin
Move (True) To bMessageFound
Move (TranslateMessage(pMsg)) To bTemp
Move (DispatchMessage(pMsg)) To bTemp
End
End

Function_Return bMessageFound
End_Function

Focus
4-Sep-2018, 05:10 AM
Try

Move 0 to ltMsg.iMessage
Move (AddressOf(ltMsg)) To pMsg

Garret Mott
4-Sep-2018, 06:38 AM
IIRC, Andrew (Focus) did some work on it as well.

Anyway - I don't see that the Struct is being cleared as the Type was.

Michael Mullan
4-Sep-2018, 08:47 AM
before you do this:




Move (AddressOf(ltMsg)) to pMsg


you should probably do this:



Move 0 to ltMsg.hWndOwner


to explicitly set a value, and ensure that the local variable is actually initialized with a value, and hence a valid address. I'm not sure if it's a myth now or not, but I've always felt the need to Move a value to a local variable before trying to do something with it's address.

Mike Peat
4-Sep-2018, 08:56 AM
Michael

I don't think that will help: all of the members will be initialised.

Clive: how about trying changing "Pointer pMsg" to "Address pMsg". From previous (bruising! :() experience, this sometimes matters.

Mike

raveens
4-Sep-2018, 06:00 PM
Hi Clive,

For starters the STRUCT is incorrect.
See: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagmsg

Should be:


Struct tPOINT
Integer x
Integer y
End_Struct // POINT


Struct tMSG
Handle hWndOwner
Integer iMessage
Integer wParam
Integer lParam
DWord dwTime
tPOINT pt
DWord lPrivate
End_Struct


As for the code:



tMsg ltMsg
Boolean bMessageFound
Integer iVoid
Pointer pMsg


Move (False) to bMessageFound


Move (AddressOf(ltMsg)) to pMsg
If (PeekMessage(pMsg, 0, 0, 0, PM_REMOVE)) Begin
If (ltMsg.iMessage = WM_PAINT) Begin
Move (True) to bMessageFound
Move (TranslateMessage(pMsg)) to iVoid
Move (DispatchMessage(pMsg)) to iVoid
End
End


Or, Alternatively you could just do:

Send PumpMsgQueue of Desktop

Clive Richmond
5-Sep-2018, 02:03 AM
Hi Raveen,


For starters the STRUCT is incorrect.
See: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagmsg


Yes, that was it. Thanks.



Or, Alternatively you could just do:

Send PumpMsgQueue of Desktop

We do use PumpMsgQueue with modal objects. I can't remember the reason why but we had focus issues with non-modal objects and switched to using PeekMessage.