source: trunk/src/kernel32/KERNEL32.CPP@ 7360

Last change on this file since 7360 was 7080, checked in by sandervl, 24 years ago

GlobalMemoryStatus change

File size: 15.5 KB
Line 
1/* $Id: KERNEL32.CPP,v 1.70 2001-10-16 17:50: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
48ODINDEBUGCHANNEL(KERNEL32-KERNEL32)
49
50 /* this define enables certain less important debug messages */
51//#define DEBUG_LOCAL 1
52
53
54
55/*****************************************************************************
56 * Name : BOOL WIN32API CloseHandle
57 * Purpose : forward call to Open32
58 * Parameters:
59 * Variables :
60 * Result :
61 * Remark :
62 * Status :
63 *
64 * Author : Patrick Haller [Fri, 1998/06/12 03:44]
65 *****************************************************************************/
66BOOL WIN32API CloseHandle(HANDLE hHandle)
67{
68 dprintf(("KERNEL32: CloseHandle(%08xh)\n",
69 hHandle));
70
71 return HMCloseHandle(hHandle);
72}
73
74
75//******************************************************************************
76HANDLE WIN32API GetStdHandle(DWORD fdwDevice)
77{
78 HANDLE handle;
79
80 /* @@@PH 1998/02/12 Handle Manager Support */
81 handle = HMGetStdHandle(fdwDevice);
82
83 //@@@PH translate handle
84
85 /* handle = GetStdHandle(fdwDevice); */
86 dprintf(("KERNEL32: GetStdHandle for device %X returned %X\n", fdwDevice, handle));
87 return(handle);
88}
89//******************************************************************************
90//******************************************************************************
91BOOL WIN32API SetStdHandle(DWORD IDStdHandle,
92 HANDLE hHandle)
93{
94 dprintf(("KERNEL32: SetStdHandle\n"));
95
96 ///@@@PH translate handle
97
98 return (HMSetStdHandle(IDStdHandle,
99 hHandle));
100}
101//******************************************************************************
102//******************************************************************************
103BOOL WIN32API IsBadWritePtr(LPVOID lpvPtr, UINT cbBytes)
104{
105#ifdef DEBUG
106 BOOL rc;
107
108 rc = O32_IsBadWritePtr(lpvPtr, cbBytes);
109 dprintf(("KERNEL32: IsBadWritePtr: 0x%X size %d rc = %d\n", (int)lpvPtr, cbBytes, rc));
110 return(rc);
111#else
112 return(O32_IsBadWritePtr(lpvPtr, cbBytes));
113#endif
114}
115//******************************************************************************
116//******************************************************************************
117BOOL WIN32API IsBadReadPtr(CONST VOID *lpvPtr, UINT cbBytes)
118{
119#ifdef DEBUG
120 BOOL rc;
121
122 rc = O32_IsBadReadPtr(lpvPtr, cbBytes);
123 dprintf(("KERNEL32: IsBadReadPtr: 0x%X size %d rc = %d\n", (int)lpvPtr, cbBytes, rc));
124 return(rc);
125#else
126 return(O32_IsBadReadPtr(lpvPtr, cbBytes));
127#endif
128}
129//******************************************************************************
130//******************************************************************************
131BOOL WIN32API IsBadCodePtr(FARPROC pCode)
132{
133 dprintf(("KERNEL32: IsBadCodePtr %x", pCode));
134 return O32_IsBadCodePtr(pCode);
135}
136//******************************************************************************
137//******************************************************************************
138BOOL WIN32API IsBadStringPtrA( LPCSTR arg1, UINT arg2)
139{
140 dprintf(("KERNEL32: IsBadStringPtr"));
141 return O32_IsBadStringPtr(arg1, arg2);
142}
143//******************************************************************************
144//******************************************************************************
145BOOL WIN32API IsBadStringPtrW(LPCWSTR arg1, UINT arg2)
146{
147 dprintf(("KERNEL32: OS2IsBadStringPtrW"));
148 return O32_IsBadReadPtr((CONST VOID *)arg1, arg2*2+2);
149}
150//******************************************************************************
151//******************************************************************************
152DWORD WIN32API GetLastError()
153{
154 DWORD rc;
155
156 rc = O32_GetLastError();
157 if(rc) {
158 dprintf(("KERNEL32: GetLastError returned %d\n", rc));
159 }
160 else dprintf(("KERNEL32: GetLastError returned no error\n", rc));
161 return(rc);
162}
163//******************************************************************************
164//******************************************************************************
165VOID WIN32API SetLastError( DWORD dwError)
166{
167 if(dwError != 0) {
168 dprintf2(("KERNEL32: SetLastError to %d\n", dwError));
169 }
170 O32_SetLastError(dwError);
171}
172//******************************************************************************
173//******************************************************************************
174/*
175 * PH 2000/09/25 This is an experiment to overcome some problems
176 * with Open32's GMS variant.
177 */
178void _GlobalMemoryStatus(MEMORYSTATUS *lpMemStat)
179{
180 ULONG sys[5];
181 // Note: QSV_TOTPHYSMEM = 17, QSV_MAXSHMEM = 21
182 lpMemStat->dwLength = sizeof(MEMORYSTATUS);
183
184 if(!OSLibDosQuerySysInfo( 17, 21, (PVOID)sys, sizeof(sys)))
185 {
186 // Specified a number between 0 and 100 that gives a general idea of
187 // current memory utilization, in which 0 indicates no memory use and
188 // 100 indicates full memory use
189
190 //#define MB512 0x1c000000
191 //lpMemStat->dwMemoryLoad = (MB512-sys[20]) * 100 / MB512;
192 lpMemStat->dwMemoryLoad = (sys[1] * 100) / sys[0];
193
194 // bytes of physical memory
195 lpMemStat->dwTotalPhys = sys[0];
196
197 // free physical memory bytes
198 lpMemStat->dwAvailPhys = sys[0] - sys[1];
199
200 // bytes of paging file
201 // @@@PH add swapper.dat size?
202 // SvL: Some stupid apps interpret this as a signed long
203 lpMemStat->dwTotalPageFile = (sys[2] > 0x80000000) ? 0x7fffffff : sys[2];
204
205 // free bytes of paging file
206 lpMemStat->dwAvailPageFile = (sys[2] > 0x80000000) ? 0x7fffffff : sys[2];
207
208 // user bytes of address space
209 lpMemStat->dwTotalVirtual = max(lpMemStat->dwTotalPageFile, sys[3]);
210 lpMemStat->dwAvailVirtual = min(lpMemStat->dwAvailPageFile, sys[3]);
211 }
212}
213
214
215
216VOID WIN32API GlobalMemoryStatus(MEMORYSTATUS *arg1)
217{
218 dprintf(("KERNEL32: GlobalMemoryStatus\n"));
219 //O32_GlobalMemoryStatus(arg1);
220 _GlobalMemoryStatus(arg1);
221 dprintf(("dwMemoryLoad %X\n", arg1->dwMemoryLoad));
222 dprintf(("dwTotalPhys %X\n", arg1->dwTotalPhys));
223 dprintf(("dwAvailPhys %X\n", arg1->dwAvailPhys));
224 dprintf(("dwTotalPageFile %X\n", arg1->dwTotalPageFile));
225 dprintf(("dwAvailPageFile %X\n", arg1->dwAvailPageFile));
226 dprintf(("dwTotalVirtual %X\n", arg1->dwTotalVirtual));
227 dprintf(("dwAvailVirtual %X\n", arg1->dwAvailVirtual));
228}
229//******************************************************************************
230//******************************************************************************
231BOOL WIN32API Beep( DWORD arg1, DWORD arg2)
232{
233 dprintf(("KERNEL32: Beep %d %d", arg1, arg2));
234 return OSLibDosBeep(arg1, arg2);
235}
236//******************************************************************************
237//******************************************************************************
238
239typedef INT (WINAPI *MessageBoxA_funcptr)(HWND,LPCSTR,LPCSTR,UINT);
240typedef INT (WINAPI *MessageBoxW_funcptr)(HWND,LPCWSTR,LPCWSTR,UINT);
241
242//******************************************************************************
243//******************************************************************************
244VOID WIN32API FatalAppExitA( UINT exitCode, LPCSTR str)
245{
246 HMODULE mod = GetModuleHandleA( "user32.dll" );
247 MessageBoxA_funcptr pMessageBoxA = NULL;
248
249 dprintf(("KERNEL32: FatalAppExitA %d %s", exitCode, str));
250
251 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
252 if(pMessageBoxA) pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
253
254 ExitProcess(exitCode);
255}
256//******************************************************************************
257//******************************************************************************
258VOID WIN32API FatalAppExitW(UINT exitCode, LPCWSTR str)
259{
260 HMODULE mod = GetModuleHandleA( "user32.dll" );
261 MessageBoxW_funcptr pMessageBoxW = NULL;
262
263 dprintf(("KERNEL32: FatalAppExitW %d %ls", exitCode, str));
264
265 if (mod) pMessageBoxW = (MessageBoxW_funcptr)GetProcAddress( mod, "MessageBoxW" );
266 if(pMessageBoxW) pMessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
267
268 ExitProcess(exitCode);
269}
270//******************************************************************************
271//******************************************************************************
272VOID WIN32API FatalExit( UINT exitCode)
273{
274 dprintf(("KERNEL32: FatalExit %x", exitCode));
275 ExitProcess(exitCode);
276}
277//******************************************************************************
278//******************************************************************************
279BOOL WIN32API IsBadHugeReadPtr( const void * arg1, UINT arg2)
280{
281 return IsBadReadPtr(arg1, arg2);
282}
283//******************************************************************************
284//******************************************************************************
285BOOL WIN32API IsBadHugeWritePtr( PVOID arg1, UINT arg2)
286{
287 return IsBadWritePtr(arg1, arg2);
288}
289//******************************************************************************
290//******************************************************************************
291int WIN32API MulDiv(int arg1, int arg2, int arg3)
292{
293 dprintf2(("KERNEL32: MulDiv %d*%d/%d\n", arg1, arg2, arg3));
294 if(arg3 == 0)
295 return 0;
296
297 return O32_MulDiv(arg1, arg2, arg3);
298}
299//******************************************************************************
300//Borrowed from Wine
301//******************************************************************************
302ODINPROCEDURE1(GetStartupInfoA,LPSTARTUPINFOA,lpStartupInfo)
303{
304 lpStartupInfo->cb = sizeof(STARTUPINFOA);
305 lpStartupInfo->lpReserved = "<Reserved>";
306 lpStartupInfo->lpDesktop = "Desktop";
307 lpStartupInfo->lpTitle = "Title";
308 lpStartupInfo->dwX = 0;
309 lpStartupInfo->dwY = 0;
310 lpStartupInfo->dwXSize = 640;
311 lpStartupInfo->dwYSize = 480;
312 lpStartupInfo->dwXCountChars = 80; // for console
313 lpStartupInfo->dwYCountChars = 25;
314 lpStartupInfo->dwFillAttribute = 0x0720;
315 lpStartupInfo->dwFlags = STARTF_USESHOWWINDOW |
316 STARTF_USEPOSITION |
317 STARTF_USESIZE |
318 STARTF_USECOUNTCHARS |
319 STARTF_USEFILLATTRIBUTE|
320 STARTF_USESTDHANDLES;
321 lpStartupInfo->wShowWindow = SW_SHOWDEFAULT;
322 lpStartupInfo->cbReserved2 = 0;
323 lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
324 /* @@@PH 98/07/13 Handlemanager support */
325 lpStartupInfo->hStdInput = GetStdHandle(STD_INPUT_HANDLE);
326 lpStartupInfo->hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
327 lpStartupInfo->hStdError = GetStdHandle(STD_ERROR_HANDLE);
328 return;
329}
330//******************************************************************************
331//Borrowed from Wine
332//******************************************************************************
333ODINPROCEDURE1(GetStartupInfoW,LPSTARTUPINFOW,lpStartupInfo)
334{
335 static WCHAR lpReserved[] = {'<', 'R','e','s','e','r','v','e','d','>', 0};
336 static WCHAR lpDesktop[] = {'D', 'e','s','k','t','o','p', 0};
337 static WCHAR lpTitle[] = {'T', 'i','t','l','e', 0};
338
339 // forward call to ascii variant
340 GetStartupInfoA((LPSTARTUPINFOA)lpStartupInfo);
341 lpStartupInfo->cb = sizeof(STARTUPINFOW);
342 lpStartupInfo->lpReserved = lpReserved;
343 lpStartupInfo->lpDesktop = lpDesktop;
344 lpStartupInfo->lpTitle = lpTitle;
345 return;
346}
347//******************************************************************************
348//******************************************************************************
349BOOL WIN32API FlushInstructionCache( /*PLF Mon 98-02-09 23:56:49 : STUB STUB STUB STUB STUB */
350 HANDLE hProcess, /* process with cache to flush */
351 LPCVOID lpvBase, /* address of region to flush */
352 DWORD cbFlush) /* length of region to flush */
353
354{
355 dprintf(("FlushInstructionCache() - NIY\n"));
356 return TRUE;
357}
358
359
360//******************************************************************************
361
362/***********************************************************************
363* RtlFillMemory (KERNEL32.441)
364*/
365VOID WIN32API RtlFillMemory(LPVOID ptr,
366 UINT len,
367 UINT fill )
368{
369#ifdef DEBUG_LOCAL
370 dprintf(("KERNEL32: RtlFillMemory(%08x,%08x,%08x)\n",
371 ptr,
372 len,
373 fill));
374#endif
375
376 memset(ptr,
377 fill,
378 len );
379}
380
381
382/***********************************************************************
383* RtlMoveMemory (KERNEL32.442)
384*/
385VOID WIN32API RtlMoveMemory(LPVOID dst,
386 LPCVOID src,
387 UINT len )
388{
389 dprintf2(("KERNEL32: RtlMoveMemory(%08x,%08x,%08x)\n",
390 dst,
391 src,
392 len));
393
394 memmove(dst,
395 src,
396 len );
397}
398
399
400/***********************************************************************
401* RtlZeroMemory (KERNEL32.444)
402*/
403VOID WIN32API RtlZeroMemory(LPVOID ptr,
404 UINT len)
405{
406 dprintf2(("KERNEL32: RtlZeroMemory(%08x,%08x)\n",
407 ptr,
408 len));
409
410 memset(ptr,
411 0,
412 len);
413}
414
415//******************************************************************************
416
417
418
419
420
421
422
423
424/*****************************************************************************
425 * Name : BOOL GetSystemPowerStatus
426 * Purpose : The GetSystemPowerStatus function retrieves the power status of
427 * the system. The status indicates whether the system is running
428 * on AC or DC power, whether the battery is currently charging,
429 * and how much battery life currently remains.
430 * Parameters: LPSYSTEM_POWER_STATUS lpSystemPowerStatus
431 * Variables :
432 * Result : TRUE / FALSE
433 * Remark :
434 * Status : UNTESTED STUB
435 *
436 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
437 *****************************************************************************/
438
439BOOL WIN32API GetSystemPowerStatus(LPSYSTEM_POWER_STATUS lpSystemPowerStatus)
440{
441 dprintf(("Kernel32: GetSystemPowerStatus(%08xh) not properly implemented.\n",
442 lpSystemPowerStatus));
443
444 if(lpSystemPowerStatus == NULL) {
445 SetLastError(ERROR_INVALID_PARAMETER);
446 return FALSE;
447 }
448 lpSystemPowerStatus->ACLineStatus = AC_LINE_ONLINE;
449 lpSystemPowerStatus->BatteryFlag = BATTERY_FLAG_HIGH;
450 lpSystemPowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
451 lpSystemPowerStatus->Reserved1 = 0;
452 lpSystemPowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
453 lpSystemPowerStatus->BatteryFullLifeTime= BATTERY_LIFE_UNKNOWN;
454
455 return TRUE;
456}
Note: See TracBrowser for help on using the repository browser.