| 1 | /* $Id: KERNEL32.CPP,v 1.75 2003-04-29 10:27:18 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 compatibility file functions for OS/2 | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen | 
|---|
| 7 | * Copyright 1998 Patrick Haller | 
|---|
| 8 | * Copyright 1998 Peter Fitzsimmons | 
|---|
| 9 | * Copyright 1998 Knut St. Osmundsen | 
|---|
| 10 | * | 
|---|
| 11 | * @(#) KERNEL32.CPP            1.0.1   1998/06/12 PH added HandleManager support | 
|---|
| 12 | * | 
|---|
| 13 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 14 | * | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | /***************************************************************************** | 
|---|
| 19 | * Includes                                                                  * | 
|---|
| 20 | *****************************************************************************/ | 
|---|
| 21 |  | 
|---|
| 22 | #include <odin.h> | 
|---|
| 23 | #include <odinwrap.h> | 
|---|
| 24 | #include <os2sel.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include <os2win.h> | 
|---|
| 27 | #include <winnt.h> | 
|---|
| 28 | #include <winnls.h> | 
|---|
| 29 | #include <stdlib.h> | 
|---|
| 30 | #include <string.h> | 
|---|
| 31 | #include <ctype.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include <misc.h> | 
|---|
| 34 | #include <unicode.h> | 
|---|
| 35 | #include "heap.h" | 
|---|
| 36 | #include "handlemanager.h" | 
|---|
| 37 | #include "wprocess.h" | 
|---|
| 38 | #include "oslibdos.h" | 
|---|
| 39 | #include <versionos2.h> | 
|---|
| 40 |  | 
|---|
| 41 | #define DBG_LOCALLOG    DBG_kernel32 | 
|---|
| 42 | #include "dbglocal.h" | 
|---|
| 43 |  | 
|---|
| 44 | /***************************************************************************** | 
|---|
| 45 | * Defines                                                                   * | 
|---|
| 46 | *****************************************************************************/ | 
|---|
| 47 |  | 
|---|
| 48 | ODINDEBUGCHANNEL(KERNEL32-KERNEL32) | 
|---|
| 49 |  | 
|---|
| 50 | /* this define enables certain less important debug messages */ | 
|---|
| 51 | //#define DEBUG_LOCAL 1 | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | /***************************************************************************** | 
|---|
| 56 | * Name      : DWORD GetHandleInformation | 
|---|
| 57 | * Purpose   : The GetHandleInformation function obtains information about certain | 
|---|
| 58 | *             properties of an object handle. The information is obtained as a set of bit flags. | 
|---|
| 59 | * Parameters: HANDLE  hObject | 
|---|
| 60 | *             LPDWORD lpdwFlags | 
|---|
| 61 | * Variables : | 
|---|
| 62 | * Result    : TRUE / FALSE | 
|---|
| 63 | * Remark    : | 
|---|
| 64 | * Status    : | 
|---|
| 65 | * | 
|---|
| 66 | * Author    : SvL | 
|---|
| 67 | *****************************************************************************/ | 
|---|
| 68 |  | 
|---|
| 69 | BOOL WIN32API GetHandleInformation(HANDLE  hObject, | 
|---|
| 70 | LPDWORD lpdwFlags) | 
|---|
| 71 | { | 
|---|
| 72 | return HMGetHandleInformation(hObject, lpdwFlags); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /***************************************************************************** | 
|---|
| 76 | * Name      : BOOL SetHandleInformation | 
|---|
| 77 | * Purpose   : The SetHandleInformation function sets certain properties of an | 
|---|
| 78 | *             object handle. The information is specified as a set of bit flags. | 
|---|
| 79 | * Parameters: HANDLE hObject  handle to an object | 
|---|
| 80 | *             DWORD  dwMask   specifies flags to change | 
|---|
| 81 | *             DWORD  dwFlags  specifies new values for flags | 
|---|
| 82 | * Variables : | 
|---|
| 83 | * Result    : TRUE / FALSE | 
|---|
| 84 | * Remark    : | 
|---|
| 85 | * Status    : | 
|---|
| 86 | * | 
|---|
| 87 | * Author    : SvL | 
|---|
| 88 | *****************************************************************************/ | 
|---|
| 89 |  | 
|---|
| 90 | BOOL WIN32API SetHandleInformation(HANDLE hObject, | 
|---|
| 91 | DWORD  dwMask, | 
|---|
| 92 | DWORD  dwFlags) | 
|---|
| 93 | { | 
|---|
| 94 | return HMSetHandleInformation(hObject, dwMask, dwFlags); | 
|---|
| 95 | } | 
|---|
| 96 | /***************************************************************************** | 
|---|
| 97 | * Name      : BOOL WIN32API CloseHandle | 
|---|
| 98 | * Purpose   : forward call to Open32 | 
|---|
| 99 | * Parameters: | 
|---|
| 100 | * Variables : | 
|---|
| 101 | * Result    : | 
|---|
| 102 | * Remark    : | 
|---|
| 103 | * Status    : | 
|---|
| 104 | * | 
|---|
| 105 | * Author    : Patrick Haller [Fri, 1998/06/12 03:44] | 
|---|
| 106 | *****************************************************************************/ | 
|---|
| 107 | BOOL WIN32API CloseHandle(HANDLE hHandle) | 
|---|
| 108 | { | 
|---|
| 109 | dprintf(("KERNEL32: CloseHandle(%08xh)\n", | 
|---|
| 110 | hHandle)); | 
|---|
| 111 |  | 
|---|
| 112 | return HMCloseHandle(hHandle); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | //****************************************************************************** | 
|---|
| 117 | HANDLE WIN32API GetStdHandle(DWORD fdwDevice) | 
|---|
| 118 | { | 
|---|
| 119 | HANDLE handle; | 
|---|
| 120 |  | 
|---|
| 121 | /* @@@PH 1998/02/12 Handle Manager Support */ | 
|---|
| 122 | handle = HMGetStdHandle(fdwDevice); | 
|---|
| 123 |  | 
|---|
| 124 | //@@@PH translate handle | 
|---|
| 125 |  | 
|---|
| 126 | /* handle = GetStdHandle(fdwDevice); */ | 
|---|
| 127 | dprintf(("KERNEL32:  GetStdHandle for device %X returned %X\n", fdwDevice, handle)); | 
|---|
| 128 | return(handle); | 
|---|
| 129 | } | 
|---|
| 130 | //****************************************************************************** | 
|---|
| 131 | //****************************************************************************** | 
|---|
| 132 | BOOL WIN32API SetStdHandle(DWORD  IDStdHandle, | 
|---|
| 133 | HANDLE hHandle) | 
|---|
| 134 | { | 
|---|
| 135 | dprintf(("KERNEL32:  SetStdHandle\n")); | 
|---|
| 136 |  | 
|---|
| 137 | ///@@@PH translate handle | 
|---|
| 138 |  | 
|---|
| 139 | return (HMSetStdHandle(IDStdHandle, | 
|---|
| 140 | hHandle)); | 
|---|
| 141 | } | 
|---|
| 142 | //****************************************************************************** | 
|---|
| 143 | //****************************************************************************** | 
|---|
| 144 | BOOL WIN32API IsBadWritePtr(LPVOID lpvPtr, UINT cbBytes) | 
|---|
| 145 | { | 
|---|
| 146 | #ifdef DEBUG | 
|---|
| 147 | BOOL rc; | 
|---|
| 148 |  | 
|---|
| 149 | rc = O32_IsBadWritePtr(lpvPtr, cbBytes); | 
|---|
| 150 | dprintf(("KERNEL32:  IsBadWritePtr: 0x%X size %d rc = %d\n", (int)lpvPtr, cbBytes, rc)); | 
|---|
| 151 | return(rc); | 
|---|
| 152 | #else | 
|---|
| 153 | return(O32_IsBadWritePtr(lpvPtr, cbBytes)); | 
|---|
| 154 | #endif | 
|---|
| 155 | } | 
|---|
| 156 | //****************************************************************************** | 
|---|
| 157 | //****************************************************************************** | 
|---|
| 158 | BOOL WIN32API IsBadReadPtr(CONST VOID *lpvPtr, UINT cbBytes) | 
|---|
| 159 | { | 
|---|
| 160 | #ifdef DEBUG | 
|---|
| 161 | BOOL rc; | 
|---|
| 162 |  | 
|---|
| 163 | rc = O32_IsBadReadPtr(lpvPtr, cbBytes); | 
|---|
| 164 | dprintf(("KERNEL32:  IsBadReadPtr: 0x%X size %d rc = %d\n", (int)lpvPtr, cbBytes, rc)); | 
|---|
| 165 | return(rc); | 
|---|
| 166 | #else | 
|---|
| 167 | return(O32_IsBadReadPtr(lpvPtr, cbBytes)); | 
|---|
| 168 | #endif | 
|---|
| 169 | } | 
|---|
| 170 | //****************************************************************************** | 
|---|
| 171 | //****************************************************************************** | 
|---|
| 172 | BOOL WIN32API IsBadCodePtr(FARPROC pCode) | 
|---|
| 173 | { | 
|---|
| 174 | dprintf(("KERNEL32: IsBadCodePtr %x", pCode)); | 
|---|
| 175 | return O32_IsBadCodePtr(pCode); | 
|---|
| 176 | } | 
|---|
| 177 | //****************************************************************************** | 
|---|
| 178 | //****************************************************************************** | 
|---|
| 179 | BOOL WIN32API IsBadStringPtrA( LPCSTR arg1, UINT  arg2) | 
|---|
| 180 | { | 
|---|
| 181 | dprintf(("KERNEL32:  IsBadStringPtr")); | 
|---|
| 182 | return O32_IsBadStringPtr(arg1, arg2); | 
|---|
| 183 | } | 
|---|
| 184 | //****************************************************************************** | 
|---|
| 185 | //****************************************************************************** | 
|---|
| 186 | BOOL WIN32API IsBadStringPtrW(LPCWSTR arg1, UINT arg2) | 
|---|
| 187 | { | 
|---|
| 188 | dprintf(("KERNEL32:  OS2IsBadStringPtrW")); | 
|---|
| 189 | return O32_IsBadReadPtr((CONST VOID *)arg1, arg2*2+2); | 
|---|
| 190 | } | 
|---|
| 191 | //****************************************************************************** | 
|---|
| 192 | //****************************************************************************** | 
|---|
| 193 | DWORD WIN32API GetLastError() | 
|---|
| 194 | { | 
|---|
| 195 | DWORD rc; | 
|---|
| 196 |  | 
|---|
| 197 | rc = O32_GetLastError(); | 
|---|
| 198 | if(rc) { | 
|---|
| 199 | dprintf(("KERNEL32: GetLastError returned %d\n", rc)); | 
|---|
| 200 | } | 
|---|
| 201 | else dprintf2(("KERNEL32: GetLastError returned no error\n", rc)); | 
|---|
| 202 | return(rc); | 
|---|
| 203 | } | 
|---|
| 204 | //****************************************************************************** | 
|---|
| 205 | //****************************************************************************** | 
|---|
| 206 | VOID WIN32API SetLastError( DWORD dwError) | 
|---|
| 207 | { | 
|---|
| 208 | if(dwError != 0) { | 
|---|
| 209 | dprintf(("KERNEL32:  SetLastError to %d\n", dwError)); | 
|---|
| 210 | } | 
|---|
| 211 | O32_SetLastError(dwError); | 
|---|
| 212 | } | 
|---|
| 213 | //****************************************************************************** | 
|---|
| 214 | //****************************************************************************** | 
|---|
| 215 | /* | 
|---|
| 216 | * PH 2000/09/25 This is an experiment to overcome some problems | 
|---|
| 217 | * with Open32's GMS variant. | 
|---|
| 218 | */ | 
|---|
| 219 | void _GlobalMemoryStatus(MEMORYSTATUS *lpMemStat) | 
|---|
| 220 | { | 
|---|
| 221 | ULONG  sys[5]; | 
|---|
| 222 | // Note: QSV_TOTPHYSMEM = 17, QSV_MAXSHMEM = 21 | 
|---|
| 223 | lpMemStat->dwLength = sizeof(MEMORYSTATUS); | 
|---|
| 224 |  | 
|---|
| 225 | if(!OSLibDosQuerySysInfo( 17, 21, (PVOID)sys, sizeof(sys))) | 
|---|
| 226 | { | 
|---|
| 227 | // Specified a number between 0 and 100 that gives a general idea of | 
|---|
| 228 | // current memory utilization, in which 0 indicates no memory use and | 
|---|
| 229 | // 100 indicates full memory use | 
|---|
| 230 |  | 
|---|
| 231 | //#define MB512 0x1c000000 | 
|---|
| 232 | //lpMemStat->dwMemoryLoad = (MB512-sys[20]) * 100 / MB512; | 
|---|
| 233 | lpMemStat->dwMemoryLoad = (sys[1] * 100) / sys[0]; | 
|---|
| 234 |  | 
|---|
| 235 | // bytes of physical memory | 
|---|
| 236 | lpMemStat->dwTotalPhys = sys[0]; | 
|---|
| 237 |  | 
|---|
| 238 | // free physical memory bytes | 
|---|
| 239 | lpMemStat->dwAvailPhys = sys[0] - sys[1]; | 
|---|
| 240 |  | 
|---|
| 241 | // bytes of paging file | 
|---|
| 242 | // @@@PH add swapper.dat size? | 
|---|
| 243 | // SvL: Some stupid apps interpret this as a signed long | 
|---|
| 244 | lpMemStat->dwTotalPageFile = (sys[2] > 0x80000000) ? 0x7fffffff : sys[2]; | 
|---|
| 245 |  | 
|---|
| 246 | // free bytes of paging file | 
|---|
| 247 | lpMemStat->dwAvailPageFile = (sys[2] > 0x80000000) ? 0x7fffffff : sys[2]; | 
|---|
| 248 |  | 
|---|
| 249 | // user bytes of address space | 
|---|
| 250 | lpMemStat->dwTotalVirtual = max(lpMemStat->dwTotalPageFile, sys[3]); | 
|---|
| 251 | lpMemStat->dwAvailVirtual = min(lpMemStat->dwAvailPageFile, sys[3]); | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 |  | 
|---|
| 257 | VOID WIN32API GlobalMemoryStatus(MEMORYSTATUS *arg1) | 
|---|
| 258 | { | 
|---|
| 259 | dprintf(("KERNEL32:  GlobalMemoryStatus\n")); | 
|---|
| 260 | //O32_GlobalMemoryStatus(arg1); | 
|---|
| 261 | _GlobalMemoryStatus(arg1); | 
|---|
| 262 | dprintf(("dwMemoryLoad    %X\n", arg1->dwMemoryLoad)); | 
|---|
| 263 | dprintf(("dwTotalPhys     %X\n", arg1->dwTotalPhys)); | 
|---|
| 264 | dprintf(("dwAvailPhys     %X\n", arg1->dwAvailPhys)); | 
|---|
| 265 | dprintf(("dwTotalPageFile %X\n", arg1->dwTotalPageFile)); | 
|---|
| 266 | dprintf(("dwAvailPageFile %X\n", arg1->dwAvailPageFile)); | 
|---|
| 267 | dprintf(("dwTotalVirtual  %X\n", arg1->dwTotalVirtual)); | 
|---|
| 268 | dprintf(("dwAvailVirtual  %X\n", arg1->dwAvailVirtual)); | 
|---|
| 269 | } | 
|---|
| 270 | //****************************************************************************** | 
|---|
| 271 | //****************************************************************************** | 
|---|
| 272 | BOOL WIN32API Beep( DWORD arg1, DWORD  arg2) | 
|---|
| 273 | { | 
|---|
| 274 | dprintf(("KERNEL32: Beep %d %d", arg1, arg2)); | 
|---|
| 275 | return OSLibDosBeep(arg1, arg2); | 
|---|
| 276 | } | 
|---|
| 277 | //****************************************************************************** | 
|---|
| 278 | //****************************************************************************** | 
|---|
| 279 |  | 
|---|
| 280 | typedef INT (WINAPI *MessageBoxA_funcptr)(HWND,LPCSTR,LPCSTR,UINT); | 
|---|
| 281 | typedef INT (WINAPI *MessageBoxW_funcptr)(HWND,LPCWSTR,LPCWSTR,UINT); | 
|---|
| 282 |  | 
|---|
| 283 | //****************************************************************************** | 
|---|
| 284 | //****************************************************************************** | 
|---|
| 285 | VOID WIN32API FatalAppExitA( UINT exitCode, LPCSTR  str) | 
|---|
| 286 | { | 
|---|
| 287 | HMODULE mod = GetModuleHandleA( "user32.dll" ); | 
|---|
| 288 | MessageBoxA_funcptr pMessageBoxA = NULL; | 
|---|
| 289 |  | 
|---|
| 290 | dprintf(("KERNEL32: FatalAppExitA %d %s", exitCode, str)); | 
|---|
| 291 |  | 
|---|
| 292 | if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" ); | 
|---|
| 293 | if(pMessageBoxA) pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK ); | 
|---|
| 294 |  | 
|---|
| 295 | ExitProcess(exitCode); | 
|---|
| 296 | } | 
|---|
| 297 | //****************************************************************************** | 
|---|
| 298 | //****************************************************************************** | 
|---|
| 299 | VOID WIN32API FatalAppExitW(UINT exitCode, LPCWSTR str) | 
|---|
| 300 | { | 
|---|
| 301 | HMODULE mod = GetModuleHandleA( "user32.dll" ); | 
|---|
| 302 | MessageBoxW_funcptr pMessageBoxW = NULL; | 
|---|
| 303 |  | 
|---|
| 304 | dprintf(("KERNEL32: FatalAppExitW %d %ls", exitCode, str)); | 
|---|
| 305 |  | 
|---|
| 306 | if (mod) pMessageBoxW = (MessageBoxW_funcptr)GetProcAddress( mod, "MessageBoxW" ); | 
|---|
| 307 | if(pMessageBoxW) pMessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK ); | 
|---|
| 308 |  | 
|---|
| 309 | ExitProcess(exitCode); | 
|---|
| 310 | } | 
|---|
| 311 | //****************************************************************************** | 
|---|
| 312 | //****************************************************************************** | 
|---|
| 313 | VOID WIN32API FatalExit( UINT exitCode) | 
|---|
| 314 | { | 
|---|
| 315 | dprintf(("KERNEL32: FatalExit %x", exitCode)); | 
|---|
| 316 | ExitProcess(exitCode); | 
|---|
| 317 | } | 
|---|
| 318 | //****************************************************************************** | 
|---|
| 319 | //****************************************************************************** | 
|---|
| 320 | BOOL WIN32API IsBadHugeReadPtr( const void * arg1, UINT  arg2) | 
|---|
| 321 | { | 
|---|
| 322 | return IsBadReadPtr(arg1, arg2); | 
|---|
| 323 | } | 
|---|
| 324 | //****************************************************************************** | 
|---|
| 325 | //****************************************************************************** | 
|---|
| 326 | BOOL WIN32API IsBadHugeWritePtr( PVOID arg1, UINT  arg2) | 
|---|
| 327 | { | 
|---|
| 328 | return IsBadWritePtr(arg1, arg2); | 
|---|
| 329 | } | 
|---|
| 330 | //****************************************************************************** | 
|---|
| 331 | //****************************************************************************** | 
|---|
| 332 | int WIN32API MulDiv(int arg1, int arg2, int  arg3) | 
|---|
| 333 | { | 
|---|
| 334 | dprintf2(("KERNEL32:  MulDiv %d*%d/%d\n", arg1, arg2, arg3)); | 
|---|
| 335 | if(arg3 == 0) | 
|---|
| 336 | return 0; | 
|---|
| 337 |  | 
|---|
| 338 | return O32_MulDiv(arg1, arg2, arg3); | 
|---|
| 339 | } | 
|---|
| 340 | //****************************************************************************** | 
|---|
| 341 | //****************************************************************************** | 
|---|
| 342 | BOOL WIN32API FlushInstructionCache(     /*PLF Mon  98-02-09 23:56:49 : STUB STUB STUB STUB STUB */ | 
|---|
| 343 | HANDLE hProcess,    /* process with cache to flush  */ | 
|---|
| 344 | LPCVOID lpvBase,    /* address of region to flush   */ | 
|---|
| 345 | DWORD cbFlush)      /* length of region to flush    */ | 
|---|
| 346 |  | 
|---|
| 347 | { | 
|---|
| 348 | dprintf(("FlushInstructionCache() - NIY\n")); | 
|---|
| 349 | return TRUE; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 |  | 
|---|
| 353 | /***************************************************************************** | 
|---|
| 354 | * Name      : BOOL GetSystemPowerStatus | 
|---|
| 355 | * Purpose   : The GetSystemPowerStatus function retrieves the power status of | 
|---|
| 356 | *             the system. The status indicates whether the system is running | 
|---|
| 357 | *             on AC or DC power, whether the battery is currently charging, | 
|---|
| 358 | *             and how much battery life currently remains. | 
|---|
| 359 | * Parameters: LPSYSTEM_POWER_STATUS lpSystemPowerStatus | 
|---|
| 360 | * Variables : | 
|---|
| 361 | * Result    : TRUE / FALSE | 
|---|
| 362 | * Remark    : | 
|---|
| 363 | * Status    : UNTESTED STUB | 
|---|
| 364 | * | 
|---|
| 365 | * Author    : Patrick Haller [Mon, 1998/06/15 08:00] | 
|---|
| 366 | *****************************************************************************/ | 
|---|
| 367 |  | 
|---|
| 368 | BOOL WIN32API GetSystemPowerStatus(LPSYSTEM_POWER_STATUS lpSystemPowerStatus) | 
|---|
| 369 | { | 
|---|
| 370 | dprintf(("Kernel32: GetSystemPowerStatus(%08xh) not properly implemented.\n", | 
|---|
| 371 | lpSystemPowerStatus)); | 
|---|
| 372 |  | 
|---|
| 373 | if(lpSystemPowerStatus == NULL) { | 
|---|
| 374 | SetLastError(ERROR_INVALID_PARAMETER); | 
|---|
| 375 | return FALSE; | 
|---|
| 376 | } | 
|---|
| 377 | lpSystemPowerStatus->ACLineStatus       = AC_LINE_ONLINE; | 
|---|
| 378 | lpSystemPowerStatus->BatteryFlag        = BATTERY_FLAG_HIGH; | 
|---|
| 379 | lpSystemPowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN; | 
|---|
| 380 | lpSystemPowerStatus->Reserved1          = 0; | 
|---|
| 381 | lpSystemPowerStatus->BatteryLifeTime    = BATTERY_LIFE_UNKNOWN; | 
|---|
| 382 | lpSystemPowerStatus->BatteryFullLifeTime= BATTERY_LIFE_UNKNOWN; | 
|---|
| 383 |  | 
|---|
| 384 | return TRUE; | 
|---|
| 385 | } | 
|---|