home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 December (Special) / DOSV2002_12.iso / utility / tcl230ja95.lzh / source.lzh / dll / sysres.c < prev    next >
C/C++ Source or Header  |  2001-02-05  |  5KB  |  174 lines

  1. /*-------------------------------------------------------------------------
  2.   sysres.c
  3.   GetFreeSystemResources
  4.   Kazubon 1999
  5.   cf. http://www2.justnet.ne.jp/~tyche/samples/sysres.html
  6.       http://ftp.digital.com/pub/published/oreilly/windows/win95.update/k32exp.html
  7. ---------------------------------------------------------------------------*/
  8.  
  9. #include <windows.h>
  10.  
  11. HINSTANCE (WINAPI *LoadLibrary16)(LPCSTR) = NULL;
  12. void (WINAPI *FreeLibrary16)(HINSTANCE) = NULL;
  13. DWORD (WINAPI *GetProcAddress16)(HINSTANCE, LPCSTR) = NULL;
  14. void (WINAPI *QT_Thunk)(DWORD) = NULL;
  15. BOOL (WINAPI *pGetSystemPowerStatus)(LPSYSTEM_POWER_STATUS) = NULL;
  16.  
  17. static BOOL bInitSysres = FALSE;
  18. static HMODULE hmodKERNEL32 = NULL;
  19. static HMODULE hmodUSER16 = NULL;
  20. DWORD dwGetFreeSystemResources = 0;
  21.  
  22. #define ENEWHDR  0x003CL    /* offset of new EXE header */
  23. #define EMAGIC   0x5A4D     /* old EXE magic id:  'MZ'  */
  24. #define PEMAGIC  0x4550     /* NT portable executable */
  25.  
  26. #define GET_DIR(x)  (hdr->OptionalHeader.DataDirectory[x].VirtualAddress)
  27.  
  28. /*------------------------------------------------
  29.   get a procedure address in DLL by ordinal
  30. --------------------------------------------------*/
  31. FARPROC GetProcAddressByOrdinal(HMODULE hModule, int ord)
  32. {
  33.     IMAGE_NT_HEADERS *hdr;
  34.     IMAGE_EXPORT_DIRECTORY *exp;
  35.     DWORD *AddrFunc;
  36.     WORD enewhdr, *pw;
  37.     int did_load = 0;
  38.     BYTE *moddb;
  39.  
  40.     moddb = (BYTE *)hModule;
  41.     pw = (WORD *) &moddb[0];
  42.     if (*pw != EMAGIC) return 0;
  43.     pw = (WORD *) &moddb[ENEWHDR];
  44.     enewhdr = *pw;
  45.     pw = (WORD *) &moddb[enewhdr];
  46.     if (*pw != PEMAGIC) return 0;
  47.     hdr = (IMAGE_NT_HEADERS *) pw;
  48.     
  49.     exp = (IMAGE_EXPORT_DIRECTORY *) (((DWORD) moddb) +
  50.         ((DWORD) GET_DIR(IMAGE_DIRECTORY_ENTRY_EXPORT)));
  51.     AddrFunc = (DWORD *) (moddb + (DWORD) exp->AddressOfFunctions);
  52.     
  53.     ord--;
  54.     if ((DWORD)ord < exp->NumberOfFunctions)
  55.         return ((FARPROC) (moddb + AddrFunc[ord]));
  56.     else return 0;
  57. }
  58.  
  59. /*----------------------------------------------------
  60.   load 16bit "USER.EXE" and get procedure address of
  61.   "GetFreeSystemResources"
  62. ------------------------------------------------------*/
  63. void InitSysres(void)
  64. {
  65.     if(bInitSysres) return;
  66.     
  67.     bInitSysres = TRUE;
  68.     
  69.     if(!(GetVersion() & 0x80000000)) return;
  70.     
  71.     if(hmodKERNEL32 == NULL)
  72.         hmodKERNEL32 = LoadLibrary("KERNEL32.dll");
  73.     if(hmodKERNEL32 == NULL) return;
  74.     
  75.     (FARPROC)LoadLibrary16 = GetProcAddressByOrdinal(hmodKERNEL32, 35);
  76.     (FARPROC)FreeLibrary16 = GetProcAddressByOrdinal(hmodKERNEL32, 36);
  77.     (FARPROC)GetProcAddress16 = GetProcAddressByOrdinal(hmodKERNEL32, 37);
  78.     (FARPROC)QT_Thunk = GetProcAddress(hmodKERNEL32, "QT_Thunk");
  79.     if(LoadLibrary16 == NULL || FreeLibrary16 == NULL ||
  80.         GetProcAddress16 == NULL || QT_Thunk == NULL)
  81.     {
  82.         FreeLibrary(hmodKERNEL32); hmodKERNEL32 = NULL; return;
  83.     }
  84.     
  85.     hmodUSER16 = LoadLibrary16("USER.EXE");
  86.     if(hmodUSER16 < (HMODULE)32)
  87.     {
  88.         FreeLibrary(hmodKERNEL32); hmodKERNEL32 = NULL;
  89.         hmodUSER16 = NULL; return;
  90.     }
  91.     
  92.     dwGetFreeSystemResources = GetProcAddress16(hmodUSER16,
  93.         "GetFreeSystemResources");
  94.     if(dwGetFreeSystemResources == 0)
  95.     {
  96.         FreeLibrary(hmodKERNEL32); hmodKERNEL32 = NULL;
  97.         FreeLibrary16(hmodUSER16); hmodUSER16 = NULL;
  98.     }
  99. }
  100.  
  101. /*----------------------------------------------------
  102.   free libraries
  103. ------------------------------------------------------*/
  104. void EndSysres(void)
  105. {
  106.     if(hmodKERNEL32) FreeLibrary(hmodKERNEL32); hmodKERNEL32 = NULL;
  107.     if(hmodUSER16) FreeLibrary16(hmodUSER16); hmodUSER16 = NULL;
  108. }
  109.  
  110. /*----------------------------------------------------
  111.   call 16bit "GetFreeSystemResources" by Thunk-down
  112. ------------------------------------------------------*/
  113. int GetFreeSystemResources(WORD wSysRes)
  114. {
  115.     volatile char dummy[64];
  116.     WORD wTmp;
  117.     
  118.     dummy[0] = 0;
  119.     
  120.     if(dwGetFreeSystemResources == 0) InitSysres();
  121.     if(dwGetFreeSystemResources == 0) return 0;
  122.     
  123.     __asm {
  124.         push wSysRes
  125.         mov edx, dwGetFreeSystemResources
  126.         call QT_Thunk
  127.         mov wTmp, ax
  128.     }
  129.     return (int)wTmp;
  130. }
  131.  
  132. void InitBatteryLife(void)
  133. {
  134.     DWORD ver;
  135.     
  136.     ver = GetVersion();
  137.     if((!(ver & 0x80000000) && LOBYTE(LOWORD(ver)) < 5)) // NT 4
  138.         return;
  139.     
  140.     if(hmodKERNEL32 == NULL)
  141.         hmodKERNEL32 = LoadLibrary("KERNEL32.dll");
  142.     if(hmodKERNEL32 == NULL) return;
  143.     
  144.     (FARPROC)pGetSystemPowerStatus =
  145.         GetProcAddress(hmodKERNEL32, "GetSystemPowerStatus");
  146. }
  147.  
  148. int GetBatteryLifePercent(void)
  149. {
  150.     SYSTEM_POWER_STATUS sps;
  151.     
  152.     if(pGetSystemPowerStatus == NULL) InitBatteryLife();
  153.     
  154.     if(pGetSystemPowerStatus)
  155.     {
  156.         if(pGetSystemPowerStatus(&sps))
  157.         {
  158.             BYTE b;
  159.             b = sps.BatteryLifePercent;
  160.             return (int)b;
  161.         }
  162.         else return 255;
  163.     }
  164.     return 255;
  165. }
  166.  
  167. void FreeBatteryLife(void)
  168. {
  169.     if(hmodKERNEL32) FreeLibrary(hmodKERNEL32);
  170.     hmodKERNEL32 = NULL;
  171.     pGetSystemPowerStatus = NULL;
  172. }
  173.  
  174.