source: trunk/src/kernel32/oslibmisc.cpp@ 10606

Last change on this file since 10606 was 10606, checked in by sandervl, 21 years ago

KOM: Updates

File size: 11.0 KB
Line 
1/* $Id: oslibmisc.cpp,v 1.19 2004-05-24 08:56:07 sandervl Exp $ */
2/*
3 * Misc OS/2 util. procedures
4 *
5 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
6 * Copyright 1998 Peter FitzSimmons
7 * Copyright 1998 Patrick Haller
8 *
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13#define INCL_WIN
14#define INCL_BASE
15#define INCL_DOSPROCESS
16#define INCL_DOSERRORS
17#define INCL_DOSSEL
18#define INCL_DOSNLS /* National Language Support values */
19#include <os2wrap.h> //Odin32 OS/2 api wrappers
20#include <string.h>
21#include <stdlib.h>
22#include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
23#include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
24#include "oslibmisc.h"
25#include <misc.h>
26#include <heapstring.h>
27
28#define DBG_LOCALLOG DBG_oslibmisc
29#include "dbglocal.h"
30
31typedef APIRET ( APIENTRY *PFN_IMSETMSGQUEUEPROPERTY )( HMQ, ULONG );
32
33PFN_IMSETMSGQUEUEPROPERTY pfnImSetMsgQueueProperty = NULL;
34
35//******************************************************************************
36//TODO: not reentrant!
37//******************************************************************************
38char *OSLibGetDllName(ULONG hModule)
39{
40 static char modname[CCHMAXPATH] = {0};
41
42 if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
43 return NULL;
44 }
45 return(modname);
46}
47//******************************************************************************
48//******************************************************************************
49BOOL OSLibGetDllName(ULONG hModule, char *name, int length)
50{
51 return DosQueryModuleName(hModule, length, name) == 0;
52}
53//******************************************************************************
54/******************************************************************************
55 * Name : ULONG OSLibiGetModuleHandleA
56 * Purpose : replacement for IBM Open32's GetModuleHandle
57 * Parameters: LPCTSTR lpszModule
58 * Variables :
59 * Result : HMODULE hModule or NULLHANDLE in case of error
60 * Remark :
61 * Status : REWRITTEN UNTESTED
62 *
63 * Author : Patrick Haller [Sun, 1998/04/04 01:55]
64 *****************************************************************************/
65
66ULONG OSLibiGetModuleHandleA(char * pszModule)
67{
68 HMODULE hModule; /* module handle */
69 APIRET rc; /* API returncode */
70 static HMODULE hModuleExe = 0; /* "cached" hModuleExe */
71 PTIB pTIB; /* parameters for DosGetInfoBlocks */
72 PPIB pPIB;
73
74 dprintf(("KERNEL32:GetModuleHandle(%x)\n",
75 pszModule));
76
77 /* @@@PH 98/04/04
78
79 this Open32 function is broken for pszModule == NULL
80 return(GetModuleHandle(pszModule));
81
82 Open32 always returns -1 here, however it should return the handle
83 of the current process. MFC30 crashes.
84
85 SvL, me thinks for PELDR support, you'll have to rewrite
86 this code anyway :)
87
88 */
89
90 if (NULL == pszModule) /* obtain handle to current executable */
91 {
92 if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
93 return (hModuleExe);
94
95 rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
96 &pPIB);
97 if (rc != NO_ERROR) /* check for errors */
98 {
99 return (NULLHANDLE); /* signal failure */
100 }
101
102 hModuleExe = pPIB->pib_hmte; /* set cached module */
103 hModule = pPIB->pib_hmte; /* module table entry ID */
104 }
105 else
106 {
107 rc = DosQueryModuleHandle(pszModule, /* query module handle */
108 &hModule);
109
110 if (rc != NO_ERROR) /* check for errors */
111 {
112 return (NULLHANDLE); /* signal failure */
113 }
114 }
115
116 return (hModule); /* return determined handle */
117}
118
119
120ULONG OSLibQueryModuleHandle(char *modname)
121{
122 HMODULE hModule;
123 APIRET rc;
124
125 rc = DosQueryModuleHandle(modname, /* query module handle */
126 &hModule);
127 if(rc)
128 return(-1);
129
130 return(hModule);
131}
132
133void OSLibWait(ULONG msec)
134{
135 DosSleep(msec);
136}
137
138//******************************************************************************
139//Wrapper for Dos16AllocSeg
140//******************************************************************************
141ULONG OSLibAllocSel(ULONG size, USHORT *selector)
142{
143#if 1
144 PVOID pSelMem;
145 ULONG sel;
146 APIRET rc;
147
148 rc = DosAllocMem(&pSelMem, size, PAG_COMMIT|PAG_READ|PAG_WRITE|OBJ_TILE);
149 if(rc != NO_ERROR) {
150 dprintf(("OSLibAllocSel: DosAllocMem failed with %d", rc));
151 DebugInt3();
152 return FALSE;
153 }
154 *selector = (DosFlatToSel((ULONG)pSelMem) >> 16);
155 return *selector != 0;
156#else
157 return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
158#endif
159}
160//******************************************************************************
161//Wrapper for Dos16FreeSeg
162//******************************************************************************
163ULONG OSLibFreeSel(USHORT selector)
164{
165#if 1
166 PVOID pSelMem;
167 APIRET rc;
168
169 pSelMem = (PVOID)DosSelToFlat(selector << 16);
170 rc = DosFreeMem(pSelMem);
171 return rc == NO_ERROR;
172#else
173 return (Dos16FreeSeg(selector) == 0);
174#endif
175}
176//******************************************************************************
177//Wrapper for Dos32SelToFlat
178//******************************************************************************
179PVOID OSLibSelToFlat(USHORT selector)
180{
181 return (PVOID)DosSelToFlat(selector << 16);
182}
183//******************************************************************************
184//Get TIB data
185//******************************************************************************
186ULONG OSLibGetTIB(int tiboff)
187{
188 PTIB ptib;
189 PPIB ppib;
190 APIRET rc;
191
192 rc = DosGetInfoBlocks(&ptib, &ppib);
193 if(rc) {
194 return 0;
195 }
196 switch(tiboff)
197 {
198 case TIB_STACKTOP:
199 return (ULONG)ptib->tib_pstacklimit;
200 case TIB_STACKLOW:
201 return (ULONG)ptib->tib_pstack;
202 default:
203 return 0;
204 }
205}
206
207/**
208 * Gets a PIB data.
209 * @returns Requested PIB data.
210 * 0 may indicate error or that the PIB data you requested actually is 0.
211 * @param iPIB PIB data index. (one of the PIB_* defines in oslibmisc.h)
212 * @author
213 * @remark Spooky error handling.
214 */
215ULONG OSLibGetPIB(int iPIB)
216{
217 PTIB ptib;
218 PPIB ppib;
219 APIRET rc;
220
221 rc = DosGetInfoBlocks(&ptib, &ppib);
222 if (rc)
223 {
224 dprintf(("KERNEL32: OSLibGetPIB(%d): DosGetInfoBlocks failed with rc=%d\n", iPIB, rc));
225 return 0;
226 }
227
228 switch(iPIB)
229 {
230 case PIB_TASKHNDL:
231 return ppib->pib_hmte;
232
233 case PIB_TASKTYPE:
234 return (ppib->pib_ultype == 3) ? TASKTYPE_PM : TASKTYPE_VIO;
235
236 case PIB_PCHCMD:
237 return (ULONG)ppib->pib_pchcmd;
238
239 default:
240 dprintf(("KERNEL32: OSLibGetPIB(%d): Invalid PIB data index\n.", iPIB));
241 DebugInt3();
242 return 0;
243 }
244}
245//******************************************************************************
246//Allocate local thread memory
247//******************************************************************************
248ULONG OSLibAllocThreadLocalMemory(int nrdwords)
249{
250 APIRET rc;
251 PULONG thrdaddr;
252
253 rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
254 if(rc) {
255 dprintf(("DosAllocThreadLocalMemory failed %d", rc));
256 return 0;
257 }
258 return (ULONG)thrdaddr;
259}
260//******************************************************************************
261//******************************************************************************
262char *OSLibStripPath(char *path)
263{
264 /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
265 char *pszFilename;
266 char *pszFilename1;
267
268 pszFilename = strrchr(path, '\\'); /* find rightmost backslash */
269 pszFilename1 = strrchr(path, '/'); /* find rightmost slash */
270 if(pszFilename > pszFilename1 && pszFilename != NULL)
271 return (++pszFilename); /* return pointer to next character */
272
273 if (pszFilename1 != NULL)
274 return (++pszFilename1); /* return pointer to next character */
275
276 return (path); /* default return value */
277}
278//******************************************************************************
279//******************************************************************************
280ULONG OSLibWinInitialize()
281{
282 return (ULONG)WinInitialize(0);
283}
284//******************************************************************************
285//******************************************************************************
286ULONG OSLibWinQueryMsgQueue(ULONG hab)
287{
288 ULONG hmq;
289 APIRET rc;
290 PTIB ptib;
291 PPIB ppib;
292
293 rc = DosGetInfoBlocks(&ptib, &ppib);
294 if(rc != NO_ERROR) {
295 dprintf(("DosGetInfoBlocks failed with rc %d", rc));
296 DebugInt3();
297 return 0;
298 }
299 if(ppib->pib_ultype == 2) {
300 dprintf(("Warning: app type changed back to VIO!!"));
301 ppib->pib_ultype = 3;
302 }
303 hmq = WinQueueFromID(hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid);
304
305 if(!hmq) {
306 dprintf(("WinQueueFromID %x %x %x proc type %x failed with error %x", hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid, ppib->pib_ultype, WinGetLastError(hab)));
307
308 hmq = (ULONG)WinCreateMsgQueue((HAB)hab, 0);
309 if(!hmq) {
310 dprintf(("WinCreateMsgQueue failed with error %x", WinGetLastError(hab)));
311 }
312 }
313 return hmq;
314}
315//******************************************************************************
316//******************************************************************************
317ULONG OSLibWinSetCp(ULONG hmq, ULONG codepage)
318{
319 return WinSetCp(hmq, codepage);
320}
321//******************************************************************************
322//******************************************************************************
323ULONG OSLibQueryCountry()
324{
325 COUNTRYCODE Country = {0}; /* Country code info (0 = current country) */
326 COUNTRYINFO CtryInfo = {0}; /* Buffer for country-specific information */
327 ULONG ulInfoLen = 0;
328 APIRET rc = NO_ERROR; /* Return code */
329
330 rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country,
331 &CtryInfo, &ulInfoLen);
332
333 if (rc != NO_ERROR) {
334 return -1;
335 }
336 return CtryInfo.country;
337}
338//******************************************************************************
339//******************************************************************************
340BOOL OSLibDisablePopups()
341{
342 return DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR) == NO_ERROR;
343}
344//******************************************************************************
345//******************************************************************************
346ULONG OSLibImSetMsgQueueProperty( ULONG hmq, ULONG ulFlag )
347{
348 USHORT sel;
349 APIRET rc;
350
351 if( !pfnImSetMsgQueueProperty )
352 return 1;
353
354 sel = RestoreOS2FS();
355 rc = pfnImSetMsgQueueProperty( hmq, ulFlag );
356 SetFS( sel );
357
358 return rc;
359}
360//******************************************************************************
361//******************************************************************************
362
Note: See TracBrowser for help on using the repository browser.