Subversion Repositories filter_foundry

Rev

Rev 480 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of a common library
  3.     Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <windows.h>
  21. #include <stdio.h>
  22.  
  23. #include "compat_win.h"
  24. #include "compat_win_resource.h"
  25.  
  26. typedef BOOL(__stdcall* f_GetVersionEx)(LPOSVERSIONINFOA lpVersionInformation);
  27. Boolean Implements3264ResourceAPI(void) {
  28. #ifdef _WIN64
  29.         // 64 bit OS is never Win9x, so it must be WinNT
  30.         return true;
  31. #else
  32.         HMODULE hLib;
  33.         f_GetVersionEx fGetVersionEx;
  34.         BOOL res;
  35.  
  36.         hLib = LoadLibrary(TEXT("KERNEL32.DLL"));
  37.         if (!hLib) return 0;
  38.         fGetVersionEx = (f_GetVersionEx)(void*)GetProcAddress(hLib, "GetVersionExA");
  39.         if (fGetVersionEx != 0) {
  40.                 OSVERSIONINFOA osv;
  41.                 osv.dwOSVersionInfoSize = sizeof(osv);
  42.                 res = fGetVersionEx(&osv);
  43.                 FreeLibrary(hLib);
  44.                 // Windows NT 3.51 does implement GetVersionExA() and UpdateResourceA(), but it doesn't know about 64 bit images.
  45.                 // Windows NT 4.0 does implement UpdateResourceA(), and it can handle 64 bit images
  46.                 return res && osv.dwMajorVersion >= 4 && osv.dwPlatformId == VER_PLATFORM_WIN32_NT;
  47.         }
  48.         else {
  49.                 // Windows NT 3.1 doesn't have GetVersionExA(), doesn't implement UpdateResourceA(), and doesn't know about 64 bit images
  50.                 // Therefore, we conclude that if GetVersionExA() is missing, then we are on a system that requires manual resource processing
  51.                 FreeLibrary(hLib);
  52.                 return false;
  53.         }
  54. #endif
  55. }
  56.  
  57. // ---------------------------------
  58.  
  59. typedef ULONGLONG(__stdcall* f_GetTickCount64)();
  60. ULONGLONG _GetTickCount64(void) {
  61.         HMODULE hLib;
  62.         f_GetTickCount64 fGetTickCount64;
  63.         ULONGLONG res;
  64.  
  65.         hLib = LoadLibrary(TEXT("KERNEL32.DLL"));
  66.         if (!hLib) return 0;
  67.         fGetTickCount64 = (f_GetTickCount64)(void*)GetProcAddress(hLib, "GetTickCount64");
  68.         if (fGetTickCount64 != 0) {
  69.                 res = fGetTickCount64();
  70.                 FreeLibrary(hLib);
  71.         } else {
  72.                 #pragma warning(suppress : 28159)
  73.                 res = (ULONGLONG)GetTickCount();
  74.         }
  75.  
  76.         return res;
  77. }
  78.  
  79. // ---------------------------------
  80.  
  81. HANDLE _BeginUpdateResource(
  82.         LPCTSTR pFileName,
  83.         BOOL    bDeleteExistingResources
  84. ) {
  85.         #ifdef UNICODE
  86.         return BeginUpdateResource(pFileName, bDeleteExistingResources);
  87.         #else
  88.         if (Implements3264ResourceAPI()) {
  89.                 return BeginUpdateResourceA(pFileName, bDeleteExistingResources);
  90.         } else {
  91.                 return WineBeginUpdateResourceA(pFileName, bDeleteExistingResources);
  92.         }
  93.         #endif
  94. }
  95.  
  96. // ---------------------------------
  97.  
  98. BOOL _EndUpdateResource(
  99.         HANDLE hUpdate,
  100.         BOOL   fDiscard
  101. ) {
  102.         #ifdef UNICODE
  103.         return EndUpdateResource(hUpdate, fDiscard);
  104.         #else
  105.         if (Implements3264ResourceAPI()) {
  106.                 return EndUpdateResourceA(hUpdate, fDiscard);
  107.         } else {
  108.                 return WineEndUpdateResourceA(hUpdate, fDiscard);
  109.         }
  110.         #endif
  111. }
  112.  
  113. // ---------------------------------
  114.  
  115. BOOL _UpdateResource(
  116.         HANDLE  hUpdate,
  117.         LPCTSTR lpType,
  118.         LPCTSTR lpName,
  119.         WORD    wLanguage,
  120.         LPVOID  lpData,
  121.         DWORD   cb
  122. ) {
  123.         #ifdef UNICODE
  124.         return UpdateResource(hUpdate, lpType, lpName, wLanguage, lpData, cb);
  125.         #else
  126.         if (Implements3264ResourceAPI()) {
  127.                 return UpdateResourceA(hUpdate, lpType, lpName, wLanguage, lpData, cb);
  128.         } else {
  129.                 return WineUpdateResourceA(hUpdate, lpType, lpName, wLanguage, lpData, cb);
  130.         }
  131.         #endif
  132. }
  133.  
  134. typedef void(__stdcall* f_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
  135. void _GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
  136.         HMODULE hLib;
  137.         f_GetNativeSystemInfo fGetNativeSystemInfo;
  138.  
  139.         hLib = LoadLibrary(TEXT("KERNEL32.DLL"));
  140.         if (!hLib) return;
  141.         fGetNativeSystemInfo = (f_GetNativeSystemInfo)(void*)GetProcAddress(hLib, "GetNativeSystemInfo");
  142.         if (fGetNativeSystemInfo != 0) {
  143.                 fGetNativeSystemInfo(lpSystemInfo);
  144.                 FreeLibrary(hLib);
  145.         }
  146.         else {
  147.                 GetSystemInfo(lpSystemInfo);
  148.         }
  149. }
  150.  
  151. typedef BOOL(__stdcall* f_ImageRemoveCertificate)(HANDLE FileHandle, DWORD Index);
  152. BOOL _ImageRemoveCertificate(HANDLE FileHandle, DWORD Index) {
  153.         HMODULE hLib;
  154.         f_ImageRemoveCertificate fImageRemoveCertificate;
  155.         BOOL res = FALSE;
  156.  
  157.         #ifndef _WIN64
  158.         // Win32s (Windows 3.11) compatibility: LoadLibrary() will output a visual message if IMAGEHLP.DLL is not existing!
  159.         char* sys_path;
  160.         sys_path = (char*)malloc(MAX_PATH);
  161.         if (GetSystemDirectoryA(sys_path, MAX_PATH)) {
  162.                 char* dllfile;
  163.                 dllfile = (char*)malloc(MAX_PATH);
  164.                 if (dllfile) {
  165.                         sprintf(dllfile, "%s\\IMAGEHLP.DLL", sys_path);
  166.                         if (GetFileAttributesA(dllfile) == INVALID_FILE_ATTRIBUTES) {
  167.                                 // File does not exist
  168.                                 free(dllfile);
  169.                                 return true;
  170.                         }
  171.                         else {
  172.                                 free(dllfile);
  173.                         }
  174.                 }
  175.         }
  176.         free(sys_path);
  177.         #endif
  178.  
  179.         hLib = LoadLibrary(TEXT("IMAGEHLP.DLL"));
  180.         if (!hLib) return FALSE;
  181.         fImageRemoveCertificate = (f_ImageRemoveCertificate)(void*)GetProcAddress(hLib, "ImageRemoveCertificate");
  182.         if (fImageRemoveCertificate != 0) {
  183.                 res = fImageRemoveCertificate(FileHandle, Index);
  184.                 FreeLibrary(hLib);
  185.         }
  186.  
  187.         return res;
  188. }
  189.  
  190. Ptr NewPtr(size_t size) {
  191.         HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
  192.         if (!hMem)
  193.                 return NULL;
  194.         return (Ptr)GlobalLock(hMem);
  195. }
  196.  
  197. Ptr NewPtrClear(size_t size) {
  198.         HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
  199.         if (!hMem)
  200.                 return NULL;
  201.         return (Ptr)GlobalLock(hMem);
  202. }
  203.  
  204. void DisposePtr(Ptr ptr) {
  205.         HGLOBAL hMem = GlobalHandle((LPCVOID)ptr);
  206.         if (!hMem)
  207.                 return;
  208.         if (GlobalUnlock(hMem)) GlobalFree(hMem);
  209. }