Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.3 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: k32exp.c
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of K32EXP.C
  13. //----------------------------------------------------------------------------
  14. /*
  15. K32EXP.C -- Get32ProcAddress
  16. Win32 code to import by ordinal from KERNEL32.DLL in Windows 95
  17. Andrew Schulman
  18. andrew@ora.com
  19. http://www.ora.com/windows/
  20. ftp://ftp.ora.com/pub/examples/windows/win95.update/schulman.html
  21. August 1995
  22. After I wrote Unauthorized Windows 95 (IDG Books, 1994), KERNEL32.DLL
  23. stopped exporting undocumented Win32 functions such as VxDCall() and
  24. GetpWin16Lock() by name. The functions discussed in *Unauthorized*
  25. continue to be exported by ordinal (for example, VxDCall is
  26. KERNEL32.1 and GetpWin16Lock is KERNEL.93). However, KERNEL32 does
  27. not allow imports by ordinal (Message from debug version:
  28. "GetProcAddress: kernel32 by id not supported").
  29. This module provides GetK32ProcAddress() to support import by ordinal
  30. from KERNEL32. There's nothing undocumented in here, except for the
  31. ordinal numbers themselves. GetModuleHandle() returns the address of
  32. the executable image (see Matt Pietrek in *Microsoft Systems Journal*,
  33. September 1995, p. 20), and the image is documented in the PE (Portable
  34. Executable) file format.
  35. */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <windows.h>
  39. #include "k32exp.h"
  40. #define ENEWHDR 0x003CL /* offset of new EXE header */
  41. #define EMAGIC 0x5A4D /* old EXE magic id: 'MZ' */
  42. #define PEMAGIC 0x4550 /* NT portable executable */
  43. #define GET_DIR(x) (hdr->OptionalHeader.DataDirectory[x].VirtualAddress)
  44. DWORD WINAPI GetK32ProcAddress(int ord)
  45. {
  46. static HANDLE hmod = 0;
  47. IMAGE_NT_HEADERS *hdr;
  48. IMAGE_EXPORT_DIRECTORY *exp;
  49. DWORD *AddrFunc;
  50. WORD enewhdr, *pw;
  51. int did_load = 0;
  52. BYTE *moddb;
  53. if (hmod == 0) // one-time static init
  54. hmod = GetModuleHandle("KERNEL32");
  55. if (hmod == 0) // still
  56. return 0;
  57. moddb = (BYTE *) hmod;
  58. pw = (WORD *) &moddb[0];
  59. if (*pw != EMAGIC)
  60. return 0;
  61. pw = (WORD *) &moddb[ENEWHDR];
  62. enewhdr = *pw;
  63. pw = (WORD *) &moddb[enewhdr];
  64. if (*pw != PEMAGIC)
  65. return 0;
  66. hdr = (IMAGE_NT_HEADERS *) pw;
  67. // Note: offset from moddb, *NOT* from hdr!
  68. exp = (IMAGE_EXPORT_DIRECTORY *) (((DWORD) moddb) +
  69. ((DWORD) GET_DIR(IMAGE_DIRECTORY_ENTRY_EXPORT)));
  70. AddrFunc = (DWORD *) (moddb + (DWORD) exp->AddressOfFunctions);
  71. // should verify that e.g.:
  72. // GetProcAddress(hmod, "VirtualAlloc") == GetK32ProcAddress(710);
  73. ord--; // table is 0-based, ordinals are 1-based
  74. if (ord < (int)exp->NumberOfFunctions)
  75. return ((DWORD) (moddb + AddrFunc[ord]));
  76. else
  77. return 0;
  78. }
  79. #ifdef STANDALONE
  80. void fail(const char *s) { puts(s); exit(1); }
  81. main(int argc, char *argv[])
  82. {
  83. printf("KERNEL32!VxDCall = %08lX\n",
  84. GetK32ProcAddress(VXDCALL_ORD));
  85. printf("KERNEL32!GetpWin16Lock = %08lX\n",
  86. GetK32ProcAddress(GETPWIN16LOCK_ORD));
  87. }
  88. #endif