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

Last change on this file since 21308 was 21308, checked in by ydario, 16 years ago

Minor updates, backout imm changes.

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