jwawownt16.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. {******************************************************************************}
  2. { }
  3. { 16 bit Generic Thunks API interface Unit for Object Pascal }
  4. { }
  5. { Portions created by Microsoft are Copyright (C) 1995-2001 Microsoft }
  6. { Corporation. All Rights Reserved. }
  7. { }
  8. { The original file is: wownt16.h, released June 2000. The original Pascal }
  9. { code is: WowNT16.pas, released December 2000. The initial developer of the }
  10. { Pascal code is Marcel van Brakel (brakelm att chello dott nl). }
  11. { }
  12. { Portions created by Marcel van Brakel are Copyright (C) 1999-2001 }
  13. { Marcel van Brakel. All Rights Reserved. }
  14. { }
  15. { Obtained through: Joint Endeavour of Delphi Innovators (Project JEDI) }
  16. { }
  17. { You may retrieve the latest version of this file at the Project JEDI }
  18. { APILIB home page, located at http://jedi-apilib.sourceforge.net }
  19. { }
  20. { The contents of this file are used with permission, subject to the Mozilla }
  21. { Public License Version 1.1 (the "License"); you may not use this file except }
  22. { in compliance with the License. You may obtain a copy of the License at }
  23. { http://www.mozilla.org/MPL/MPL-1.1.html }
  24. { }
  25. { Software distributed under the License is distributed on an "AS IS" basis, }
  26. { WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
  27. { the specific language governing rights and limitations under the License. }
  28. { }
  29. { Alternatively, the contents of this file may be used under the terms of the }
  30. { GNU Lesser General Public License (the "LGPL License"), in which case the }
  31. { provisions of the LGPL License are applicable instead of those above. }
  32. { If you wish to allow use of your version of this file only under the terms }
  33. { of the LGPL License and not to allow others to use your version of this file }
  34. { under the MPL, indicate your decision by deleting the provisions above and }
  35. { replace them with the notice and other provisions required by the LGPL }
  36. { License. If you do not delete the provisions above, a recipient may use }
  37. { your version of this file under either the MPL or the LGPL License. }
  38. { }
  39. { For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html }
  40. { }
  41. {******************************************************************************}
  42. unit JwaWowNT16;
  43. {$WEAKPACKAGEUNIT}
  44. {$HPPEMIT ''}
  45. {$HPPEMIT '#include "wownt16.h"'}
  46. {$HPPEMIT ''}
  47. {$I jediapilib.inc}
  48. // (rom) get rid of warnings about "index" directive
  49. { $WARN SYMBOL_PLATFORM OFF}
  50. interface
  51. uses
  52. JwaWinType;
  53. //
  54. // 16:16 -> 0:32 Pointer translation.
  55. //
  56. // GetVDMPointer32W will convert the passed in 16-bit address
  57. // to the equivalent 32-bit flat pointer. The upper 16 bits
  58. // of the address are treated according to the value passed in
  59. // fMode: if fMode = 1, then the hiword of vp is used as a
  60. // protected mode selector. Otherwise it is used as a real mode
  61. // segment value.
  62. // The lower 16 bits are treated as the offset.
  63. //
  64. // The return value is 0 if the selector is invalid.
  65. //
  66. // NOTE: Limit checking is not performed in the retail build
  67. // of Windows NT. It is performed in the checked (debug) build
  68. // of WOW32.DLL, which will cause 0 to be returned when the
  69. // limit is exceeded by the supplied offset.
  70. //
  71. function GetVDMPointer32W(vp: LPVOID; fMode: UINT): DWORD; stdcall;
  72. {$EXTERNALSYM GetVDMPointer32W}
  73. //
  74. // Win32 module management.
  75. //
  76. // The following routines accept parameters that correspond directly
  77. // to the respective Win32 API function calls that they invoke. Refer
  78. // to the Win32 reference documentation for more detail.
  79. function LoadLibraryEx32W(lpszLibFile: LPCSTR; hFile, dwFlags: DWORD): DWORD; stdcall;
  80. {$EXTERNALSYM LoadLibraryEx32W}
  81. function GetProcAddress32W(hModule: DWORD; lpszProc: LPCSTR): DWORD; stdcall;
  82. {$EXTERNALSYM GetProcAddress32W}
  83. function FreeLibrary32W(hLibModule: DWORD): DWORD; stdcall;
  84. {$EXTERNALSYM FreeLibrary32W}
  85. //
  86. // Generic Thunk Routine:
  87. //
  88. // CallProc32W
  89. //
  90. // Transitions to 32 bits and calls specified routine
  91. //
  92. // This routine can pass a variable number of arguments, up to 32, to the
  93. // target 32-bit routine. These arguments are given to CallProc32W following
  94. // the 3 required parameters.
  95. //
  96. // DWORD cParams - Number of optional DWORD parameters (0-32)
  97. //
  98. // LPVOID fAddressConvert - Bit Field, for 16:16 address Convertion. The
  99. // optional parameters can be automatically converted
  100. // from a 16:16 address format to flat by specifying
  101. // a 1 bit in the corresponding position in this mask.
  102. // eg (bit 1 means convert parameter 1 from 16:16
  103. // to flat address before calling routine)
  104. //
  105. // DWORD lpProcAddress - 32 bit native address to call (use LoadLibraryEx32W
  106. // and GetProcAddress32W to get this address).
  107. //
  108. // Returns:
  109. // What ever the API returned on 32 bit side in AX:DX
  110. //
  111. // Error Returns:
  112. // AX = 0, more than 32 parameters.
  113. //
  114. //
  115. // The function prototype must be declared by the application source code
  116. // in the following format:
  117. //
  118. // DWORD FAR PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
  119. // DWORD fAddressConvert, DWORD cParams);
  120. //
  121. // where the value in cParams must match the actual number of optional
  122. // parameters (p1-pn) given AND the "DWORD p1, ..." must be replaced by
  123. // the correct number of parameters being passed. For example, passing 3
  124. // parameter would simply require the removal of the ... and it insertion of
  125. // "DWORD p2, DWORD p3" instead. The fAddressConvert parameter uses bit 1
  126. // for the last parameter (p3 in our example), with bit 2 for the next to last,
  127. // etc.
  128. //
  129. // Generic Thunk Routine:
  130. //
  131. // CallProcEx32W
  132. //
  133. // Transitions to 32 bits and calls specified routine
  134. //
  135. // Similar to the CallProc32W function, the CallProcEx32W is an equivalent
  136. // function that is C calling convention and allows easier and more flexible
  137. // prototyping. See the prototype below. The fAddressConvert parameter uses
  138. // bit 1 for the 1st parameter, bit 2 for the 2nd parameter, etc.
  139. //
  140. // Both CallProc32W and CallProcEx32W accept a flag OR'd with the parameter
  141. // count to indicate the calling convention of the function in 32 bits.
  142. // For example, to call a cdecl function in 32-bits with 1 parameter, it would
  143. // look like this:
  144. //
  145. // dwResult = CallProcEx32W( CPEX_DEST_CDECL | 1, 0, dwfn32, p1 );
  146. //
  147. // TODO Variable argument list wrapper!
  148. (*
  149. function CallProcEx32W(nParams, fAddressConvert, lpProcAddress: DWORD): DWORD; cdecl;
  150. {$EXTERNALSYM CallProcEx32W}
  151. *)
  152. const
  153. CPEX_DEST_STDCALL = DWORD($00000000);
  154. {$EXTERNALSYM CPEX_DEST_STDCALL}
  155. CPEX_DEST_CDECL = DWORD($80000000);
  156. {$EXTERNALSYM CPEX_DEST_CDECL}
  157. implementation
  158. const
  159. wow16lib = 'kernel32.dll';
  160. //function CallProcEx32W; external wow16lib index 517;
  161. {$IFDEF DYNAMIC_LINK}
  162. var
  163. _GetVDMPointer32W: Pointer;
  164. function GetVDMPointer32W;
  165. begin
  166. GetProcedureAddress(_GetVDMPointer32W, wow16lib, '516');
  167. asm
  168. MOV ESP, EBP
  169. POP EBP
  170. JMP [_GetVDMPointer32W]
  171. end;
  172. end;
  173. var
  174. _LoadLibraryEx32W: Pointer;
  175. function LoadLibraryEx32W;
  176. begin
  177. GetProcedureAddress(_LoadLibraryEx32W, wow16lib, '513');
  178. asm
  179. MOV ESP, EBP
  180. POP EBP
  181. JMP [_LoadLibraryEx32W]
  182. end;
  183. end;
  184. var
  185. _GetProcAddress32W: Pointer;
  186. function GetProcAddress32W;
  187. begin
  188. GetProcedureAddress(_GetProcAddress32W, wow16lib, '515');
  189. asm
  190. MOV ESP, EBP
  191. POP EBP
  192. JMP [_GetProcAddress32W]
  193. end;
  194. end;
  195. var
  196. _FreeLibrary32W: Pointer;
  197. function FreeLibrary32W;
  198. begin
  199. GetProcedureAddress(_FreeLibrary32W, wow16lib, '514');
  200. asm
  201. MOV ESP, EBP
  202. POP EBP
  203. JMP [_FreeLibrary32W]
  204. end;
  205. end;
  206. {$ELSE}
  207. function GetVDMPointer32W; external wow16lib index 516;
  208. function LoadLibraryEx32W; external wow16lib index 513;
  209. function GetProcAddress32W; external wow16lib index 515;
  210. function FreeLibrary32W; external wow16lib index 514;
  211. {$ENDIF DYNAMIC_LINK}
  212. end.