Fix for GPF Hooking the Pring Dialog

Summary

Id: 1419.2
Type: Bug Bug
Current Status: Resolved

Detail

4 Jun 2003 Open Uwe Wiemer

Thanks for your answer. I have attached a text file with the new Print-DLG structure and how to assign the hook address to the hook-members of the "new" structure. It should be self-explanatory. If not, please drop me a line. I found the solution to this problem somewhere in a vb-newsgroup - I guess. I wanted to send you that posting too but couldn't find it any more. As far as I remember Klaus H. Probst (vbbox.com) posted the answer to that problem.

'!!!!!!!!!!!!!!!!!! New structure due to misalignment problems, !!!!!!!!!!!!!!!!'!!!!!!!!!!!!!!!!!! that's why we don't use the structure below !!!!!!!!!!!!!!!!'***************Declarations for the Print-Dialogs************************'***************This one here causes some troubles (GPF!)*****************'Private Type TPRINTDLG' lStructSize As Long' hwndOwner As Long' hDevMode As Long' hDevNames As Long' hdc As Long' flags As Long' nFromPage As Integer' nToPage As Integer' nMinPage As Integer' nMaxPage As Integer' nCopies As Integer' hInstance As Long '<-- Misalignment starts here, with this member of the UDT !!!!!' lCustData As Long' lpfnPrintHook As Long' lpfnSetupHook As Long' lpPrintTemplateName As String' lpSetupTemplateName As String' hPrintTemplate As Long' hSetupTemplate As Long'End Type 'Type TPRINTDLG'~~~~~~~~~~~~ Declarations for the Print-Dialog for use with VB6 ~~~~~~~~~~~~~~~'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'~~~~~~ Due to alignment problems (32 bit alignment) we have to ~~~~~~~~~~~~~~~~~'~~~~~~~use a slightly different PrintDlg-Structure !!!!!! ~~~~~~~~~~~~~~~~~~~~~~Private Type TPRINTDLG_VB lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long hdc As Long flags As Long nFromPage As Integer nToPage As Integer nMinPage As Integer nMaxPage As Integer nCopies As Integer ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 'Here starts the trouble/the misalignment. 'Each long of an UDT has to start at a 4-Byte (=32-Bit) boundary. 'The new micro-processors can access such a long var. with one operation. 'VB adds padding bytes so that the long var starts at such a 4-Byte 'boundary, hence VB shifts the offsets of the vars. in the UDT. 'The C-DLL expects the hook address at a certain offset and since 'VB has added some padding bytes the hook address the C-Dll receives 'is wrong. That causes a GPF. To avoid that we divide the long vars. into 'high and low integer vars; we especially divide the hook address in a 'high and low integer. ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! hInstanceLow As Integer hInstanceHigh As Integer lCustDataLow As Integer lCustDataHigh As Integer lpfnPrintHookLow As Integer 'low word of lpfnPrintHook lpfnPrintHookHigh As Integer 'high word of lpfnPrintHook lpfnSetupHookLow As Integer lpfnSetupHookHigh As Integer lpPrintTemplateNameLow As Integer lpPrintTemplateNameHigh As Integer lpSetupTemplateNameLow As Integer lpSetupTemplateNameHigh As Integer hPrintTemplateLow As Integer hPrintTemplateHigh As Integer hSetupTemplateLow As Integer hSetupTemplateHigh As IntegerEnd Type 'TPRINTDLG_VB'~~~~~~~~~~~~ Declarations for the Print-Dialog for use with VB6 ~~~~~~~~~~~~~~~'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'~~~~~~ Due to alignemnt problems (32 bit alignment) we have to ~~~~~~~~~~~~~~~~~'~~~~~~~use a slightly different PrintDlg-Structure !!!!!! ~~~~~~~~~~~~~~~~~~~~~~lngHookAdr = GetMyHookFunctionAdr(AddressOf MyPrintDlgHook) 'since we have divided the Long-Pointer of the hook address into 'a high and a low word - due to bit-alignment problems with VB 'and the PrintDlg-strucuture - we have to divide the hook address 'into a high and and a low word .lpfnPrintHookHigh = lngHookAdr / 65536 .lpfnPrintHookLow = lngHookAdr Mod 65536
22 Jun 2003 Resolved Steve McMahon steve@vbaccelerator.com

Fixed alignment in a similar way to that described above. The actual deployed code uses byte arrays and CopyMemory rather than integers and math to prevent problems with unsigned values but is otherwise the same.