BVB Source Codes

Write a multi-page WEB browser source code VB Show FunctionDelegator.bas Source code

Return Download Write a multi-page WEB browser source code VB: download FunctionDelegator.bas Source code - Download Write a multi-page WEB browser source code VB Source code - Type:.bas
  1. Attribute VB_Name = "modFunctionDelegator"
  2. '***************************************************************
  3. ' (c) Copyright 2000 Matthew J. Curland
  4. '
  5. ' This file is from the CD-ROM accompanying the book:
  6. ' Advanced Visual Basic 6: Power Techniques for Everyday Programs
  7. '   Author: Matthew Curland
  8. '   Published by: Addison-Wesley, July 2000
  9. '   ISBN: 0-201-70712-8
  10. '   http://www.PowerVB.com
  11. '
  12. ' You are entitled to license free distribution of any application
  13. '   that uses this file if you own a copy of the book, or if you
  14. '   have obtained the file from a source approved by the author. You
  15. '   may redistribute this file only with express written permission
  16. '   of the author.
  17. '
  18. ' This file depends on:
  19. '   References:
  20. '     VBoostTypes6.olb (VBoost Object Types (6.0))
  21. '   Files:
  22. '     None
  23. '   Minimal VBoost conditionals:
  24. '     None
  25. '   Conditional Compilation Values:
  26. '     FUNCTIONDELEGATOR_NOSTACK = 1 'eliminates support for NewDelegator
  27. '     FUNCTIONDELEGATOR_NOHEAP = 1  'eliminates support for InitDelegator
  28. '
  29. ' This file is discussed in Chapter 11.
  30. '***************************************************************
  31. Option Explicit
  32.  
  33. 'Here's the magic asm for doing the function pointer call.
  34. 'The stack comes in with the following:
  35. 'esp: return address
  36. 'esp + 4: this pointer for FunctionDelegator
  37. 'All that we need to do is remove the this pointer from the
  38. 'stack, replace it with the return address, then jmp to the
  39. 'correct function.  In other words, we're just squeezing the
  40. 'this pointer completely out of the picture.
  41. 'The code is:
  42. 'pop ecx (stores return address)
  43. 'pop eax (gets the this pointer)
  44. 'push ecx (restores the return address)
  45. 'jmp DWORD PTR [eax + 4] (jump to address at this + 4, 3 byte instruction)
  46. 'The corresponding byte stream for this is: 59 58 51 FF 60 04
  47. 'We pad these six bytes with two int 3 commands (CC CC) to get eight
  48. 'bytes, which can be stored in a Currency constant.
  49. 'Note that the memory location of this constant is not executable, so
  50. 'it must be copied into a currency variable.  The address of the variable
  51. 'is then used as the forwarding function.
  52.  
  53. #Const STACKALLOCSUPPORT = FUNCTIONDELEGATOR_NOSTACK = 0
  54. #Const HEAPALLOCSUPPORT = FUNCTIONDELEGATOR_NOHEAP = 0
  55.  
  56. Private Const cDelegateASM As Currency = -368956918007638.6215@
  57.  
  58. Private m_DelegateASM As Currency
  59.  
  60. Private Type DelegatorVTables
  61.     VTable(7) As Long 'OKQI vtable in 0 to 3, FailQI vtable in 4 to 7
  62. End Type
  63.  
  64. #If STACKALLOCSUPPORT Then
  65. 'Structure for a stack allocated Delegator
  66. Private m_VTables As DelegatorVTables
  67. Private m_pVTableOKQI As Long       'Pointer to vtable, no allocation version
  68. Private m_pVTableFailQI As Long     'Pointer to vtable, no allocation version
  69. Public Type FunctionDelegator
  70.     pVTable As Long  'This has to stay at offset 0
  71.    pfn As Long      'This has to stay at offset 4
  72. End Type
  73. #End If 'STACKALLOCSUPPORT
  74.  
  75. #If HEAPALLOCSUPPORT Then
  76. 'Structure for a heap allocated Delegator
  77. Private m_VTablesHeapAlloc As DelegatorVTables
  78. Private m_pVTableHeapAllocOKQI As Long   'Pointer to vtable, heap version
  79. Private m_pVTableHeapAllocFailQI As Long 'Pointer to vtable, heap version
  80. Private Type FunctionDelegatorHeapAlloc
  81.     pVTable As Long  'This has to stay at offset 0
  82.    pfn As Long      'This has to stay at offset 4
  83.    cRefs As Long
  84. End Type
  85. #End If 'HEAPALLOCSUPPORT
  86.  
  87. #If STACKALLOCSUPPORT Then
  88. 'Functions to initialize a Delegator object on an existing FunctionDelegator
  89. Public Function InitDelegator(Delegator As FunctionDelegator, Optional ByVal pfn As Long) As IUnknown
  90.     If m_pVTableOKQI = 0 Then InitVTables
  91.     With Delegator
  92.         .pVTable = m_pVTableOKQI
  93.         .pfn = pfn
  94.     End With
  95.     CopyMemory InitDelegator, VarPtr(Delegator), 4
  96. End Function
  97. Private Sub InitVTables()
  98. Dim pAddRefRelease As Long
  99.     With m_VTables
  100.         .VTable(0) = FuncAddr(AddressOf QueryInterfaceOK)
  101.         .VTable(4) = FuncAddr(AddressOf QueryInterfaceFail)
  102.         pAddRefRelease = FuncAddr(AddressOf AddRefRelease)
  103.         .VTable(1) = pAddRefRelease
  104.         .VTable(5) = pAddRefRelease
  105.         .VTable(2) = pAddRefRelease
  106.         .VTable(6) = pAddRefRelease
  107.         m_DelegateASM = cDelegateASM
  108.         .VTable(3) = VarPtr(m_DelegateASM)
  109.         .VTable(7) = .VTable(3)
  110.         m_pVTableOKQI = VarPtr(.VTable(0))
  111.         m_pVTableFailQI = VarPtr(.VTable(4))
  112.     End With
  113. End Sub
  114. Private Function QueryInterfaceOK(This As FunctionDelegator, riid As Long, pvObj As Long) As Long
  115.     pvObj = VarPtr(This)
  116.     This.pVTable = m_pVTableFailQI
  117. End Function
  118. Private Function AddRefRelease(ByVal This As Long) As Long
  119.     'Nothing to do, memory not refcounted
  120. End Function
  121. #End If 'STACKALLOCSUPPORT
  122.  
  123. #If HEAPALLOCSUPPORT Then
  124. 'Functions to create a refcounted version of the function pointer wrapper object
  125. Public Function NewDelegator(ByVal pfn As Long) As IUnknown
  126. Dim Struct As FunctionDelegatorHeapAlloc
  127. Dim ThisPtr As Long
  128.     If m_pVTableHeapAllocOKQI = 0 Then InitHeapAllocVTables
  129.     With Struct
  130.         ThisPtr = CoTaskMemAlloc(LenB(Struct))
  131.         If ThisPtr = 0 Then Err.Raise 7
  132.         .pVTable = m_pVTableHeapAllocOKQI
  133.         .cRefs = 1
  134.         .pfn = pfn
  135.         CopyMemory ByVal ThisPtr, Struct, LenB(Struct)
  136.         CopyMemory NewDelegator, ThisPtr, 4
  137.     End With
  138. End Function
  139. Private Sub InitHeapAllocVTables()
  140.     With m_VTablesHeapAlloc
  141.         .VTable(0) = FuncAddr(AddressOf QueryInterfaceHeapAllocOK)
  142.         .VTable(4) = FuncAddr(AddressOf QueryInterfaceFail)
  143.         .VTable(1) = FuncAddr(AddressOf AddRefHeapAlloc)
  144.         .VTable(5) = .VTable(1)
  145.         .VTable(2) = FuncAddr(AddressOf ReleaseHeapAlloc)
  146.         .VTable(6) = .VTable(2)
  147.         m_DelegateASM = cDelegateASM
  148.         .VTable(3) = VarPtr(m_DelegateASM)
  149.         .VTable(7) = .VTable(3)
  150.         m_pVTableHeapAllocOKQI = VarPtr(.VTable(0))
  151.         m_pVTableHeapAllocFailQI = VarPtr(.VTable(4))
  152.     End With
  153. End Sub
  154. Private Function QueryInterfaceHeapAllocOK(This As FunctionDelegatorHeapAlloc, riid As Long, pvObj As Long) As Long
  155.     With This
  156.         pvObj = VarPtr(.pVTable)
  157.         .cRefs = .cRefs + 1
  158.         .pVTable = m_pVTableHeapAllocFailQI
  159.     End With
  160. End Function
  161. Private Function AddRefHeapAlloc(This As FunctionDelegatorHeapAlloc) As Long
  162.     With This
  163.         .cRefs = .cRefs + 1
  164.         AddRefHeapAlloc = .cRefs
  165.     End With
  166. End Function
  167. Private Function ReleaseHeapAlloc(This As FunctionDelegatorHeapAlloc) As Long
  168.     With This
  169.         .cRefs = .cRefs - 1
  170.         ReleaseHeapAlloc = .cRefs
  171.         If .cRefs = 0 Then
  172.             'Don't try to step over FreeBuffer, we're freeing
  173.            'This, and the debugger could die.
  174.            CoTaskMemFree VarPtr(.pVTable)
  175.         End If
  176.     End With
  177. End Function
  178. #End If 'HEAPALLOCSUPPORT
  179.  
  180. Private Function QueryInterfaceFail(ByVal This As Long, riid As Long, pvObj As Long) As Long
  181.     pvObj = 0
  182.     QueryInterfaceFail = E_NOINTERFACE
  183. End Function
  184.  
  185. Private Function FuncAddr(ByVal pfn As Long) As Long
  186.     FuncAddr = pfn
  187. End Function
  188.  
  189.  
downloadFunctionDelegator.bas Source code - Download Write a multi-page WEB browser source code VB Source code
Related Source Codes/Software:
TeeChart5.ocx controls the download and packaged many VB instance is called - Download TeeChart5.ocx the report control and many... 2012-02-18
University teaching material management system +VB papers - +VB Tianjin University teaching material managemen... 2012-02-18
9 VB6+SQL2000 database system source program - 9 VB6+SQL2000 instance of the database system of t... 2012-02-18
A VB program, get Web pages all links and pictures - Busy to nothing, a VB program, used to get all lin... 2012-02-18
QQ2010 Landers VB high simulation, parameter set - QQ2010 simulation of Lander, written in VB languag... 2012-02-18
VB-like QQ pull Auto Hide window - QQ auto hide-like form, based on VB source code, a... 2012-02-18
VB array of particle systems and calculation examples - An instance of the particle system, particle syste... 2012-02-18
VB gets Windows serial number - VB getting Windows serial number, simple examples ... 2012-02-18
VB implementation as dragging the title bar of the control form - VB realizing control acts as a drag the title bar ... 2012-02-18
VB Setup is performed automatically when you start the program - VB sets the procedure that is executed automatical... 2012-02-18
A small favorites - A small favorites & have spent Released for a long... 2015-04-14
Mini network school - If has been connected to the Internet, can correct... 2015-04-14
Vb to write a simple diary records program - This is a small program written in VB. Is a diary ... 2015-04-14
EasyFlexGrid source - EasyFlexGrid source 2015-04-14
S - the Grid code - S - the Grid code 2015-04-14
LED_LCD digital display control - An LED/LCD digital display controls, support custo... 2015-04-14
Data grid drop-down list box control - Data grid drop-down list box control 2015-04-14
Support for true color icon list control - An open source of great support for true color ico... 2015-04-14
A small volume control controls - A small volume control controls, support 7 kinds o... 2015-04-14
A beautiful page Tab controls - This is a beautiful page Tab controls, somewhat MS... 2015-04-14

 Back to top