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

Last change on this file since 3993 was 3993, checked in by sandervl, 25 years ago

Updates for fake system dll headers

File size: 8.3 KB
Line 
1/* $Id: oslibmisc.cpp,v 1.10 2000-08-11 10:56:18 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_DOSSEL
17#define INCL_DOSNLS /* National Language Support values */
18#include <os2wrap.h> //Odin32 OS/2 api wrappers
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
22#include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
23#include "oslibmisc.h"
24#include <misc.h>
25
26#define DBG_LOCALLOG DBG_oslibmisc
27#include "dbglocal.h"
28
29//******************************************************************************
30//TODO: not reentrant!
31//******************************************************************************
32char *OSLibGetDllName(ULONG hModule)
33{
34 static char modname[CCHMAXPATH] = {0};
35
36 if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
37 return NULL;
38 }
39 return(modname);
40}
41//******************************************************************************
42//******************************************************************************
43BOOL OSLibGetDllName(ULONG hModule, char *name, int length)
44{
45 return DosQueryModuleName(hModule, length, name) == 0;
46}
47//******************************************************************************
48/******************************************************************************
49 * Name : ULONG OSLibiGetModuleHandleA
50 * Purpose : replacement for IBM Open32's GetModuleHandle
51 * Parameters: LPCTSTR lpszModule
52 * Variables :
53 * Result : HMODULE hModule or NULLHANDLE in case of error
54 * Remark :
55 * Status : REWRITTEN UNTESTED
56 *
57 * Author : Patrick Haller [Sun, 1998/04/04 01:55]
58 *****************************************************************************/
59
60ULONG OSLibiGetModuleHandleA(char * pszModule)
61{
62 HMODULE hModule; /* module handle */
63 APIRET rc; /* API returncode */
64 static HMODULE hModuleExe = 0; /* "cached" hModuleExe */
65 PTIB pTIB; /* parameters for DosGetInfoBlocks */
66 PPIB pPIB;
67
68 dprintf(("KERNEL32:GetModuleHandle(%x)\n",
69 pszModule));
70
71 /* @@@PH 98/04/04
72
73 this Open32 function is broken for pszModule == NULL
74 return(GetModuleHandle(pszModule));
75
76 Open32 always returns -1 here, however it should return the handle
77 of the current process. MFC30 crashes.
78
79 SvL, me thinks for PELDR support, you'll have to rewrite
80 this code anyway :)
81
82 */
83
84 if (NULL == pszModule) /* obtain handle to current executable */
85 {
86 if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
87 return (hModuleExe);
88
89 rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
90 &pPIB);
91 if (rc != NO_ERROR) /* check for errors */
92 {
93 return (NULLHANDLE); /* signal failure */
94 }
95
96 hModuleExe = pPIB->pib_hmte; /* set cached module */
97 hModule = pPIB->pib_hmte; /* module table entry ID */
98 }
99 else
100 {
101 rc = DosQueryModuleHandle(pszModule, /* query module handle */
102 &hModule);
103
104 if (rc != NO_ERROR) /* check for errors */
105 {
106 return (NULLHANDLE); /* signal failure */
107 }
108 }
109
110 return (hModule); /* return determined handle */
111}
112
113
114ULONG OSLibQueryModuleHandle(char *modname)
115{
116 HMODULE hModule;
117 APIRET rc;
118
119 rc = DosQueryModuleHandle(modname, /* query module handle */
120 &hModule);
121 if(rc)
122 return(-1);
123
124 return(hModule);
125}
126
127void OSLibWait(ULONG msec)
128{
129 DosSleep(msec);
130}
131
132//******************************************************************************
133//Wrapper for Dos16AllocSeg
134//******************************************************************************
135ULONG OSLibAllocSel(ULONG size, USHORT *selector)
136{
137 return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
138}
139//******************************************************************************
140//Wrapper for Dos16FreeSeg
141//******************************************************************************
142ULONG OSLibFreeSel(USHORT selector)
143{
144 return (Dos16FreeSeg(selector) == 0);
145}
146//******************************************************************************
147//Wrapper for Dos32SelToFlat
148//******************************************************************************
149PVOID OSLibSelToFlat(USHORT selector)
150{
151 return (PVOID)DosSelToFlat(selector << 16);
152}
153//******************************************************************************
154//Get TIB data
155//******************************************************************************
156ULONG OSLibGetTIB(int tiboff)
157{
158 PTIB ptib;
159 PPIB ppib;
160 APIRET rc;
161
162 rc = DosGetInfoBlocks(&ptib, &ppib);
163 if(rc) {
164 return 0;
165 }
166 switch(tiboff)
167 {
168 case TIB_STACKTOP:
169 return (ULONG)ptib->tib_pstacklimit;
170 case TIB_STACKLOW:
171 return (ULONG)ptib->tib_pstack;
172 default:
173 return 0;
174 }
175}
176//******************************************************************************
177//Get PIB data
178//******************************************************************************
179ULONG OSLibGetPIB(int piboff)
180{
181 PTIB ptib;
182 PPIB ppib;
183 APIRET rc;
184
185 rc = DosGetInfoBlocks(&ptib, &ppib);
186 if(rc) {
187 return 0;
188 }
189 switch(piboff)
190 {
191 case PIB_TASKHNDL:
192 return ppib->pib_hmte;
193 case PIB_TASKTYPE:
194 if(ppib->pib_ultype == 3) {
195 return TASKTYPE_PM;
196 }
197 else return TASKTYPE_VIO;
198 default:
199 return 0;
200 }
201}
202//******************************************************************************
203//Allocate local thread memory
204//******************************************************************************
205ULONG OSLibAllocThreadLocalMemory(int nrdwords)
206{
207 APIRET rc;
208 PULONG thrdaddr;
209
210 rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
211 if(rc) {
212 dprintf(("DosAllocThreadLocalMemory failed %d", rc));
213 return 0;
214 }
215 return (ULONG)thrdaddr;
216}
217//******************************************************************************
218//******************************************************************************
219char *OSLibStripPath(char *path)
220{
221 /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
222 char *pszFilename;
223
224 pszFilename = strrchr(path, '\\'); /* find rightmost slash */
225 if (pszFilename != NULL)
226 return (++pszFilename); /* return pointer to next character */
227
228 pszFilename = strrchr(path, '/'); /* find rightmost slash */
229 if (pszFilename != NULL)
230 return (++pszFilename); /* return pointer to next character */
231
232 return (path); /* default return value */
233}
234//******************************************************************************
235//******************************************************************************
236ULONG OSLibWinInitialize()
237{
238 return (ULONG)WinInitialize(0);
239}
240//******************************************************************************
241//******************************************************************************
242ULONG OSLibWinQueryMsgQueue(ULONG hab)
243{
244// ULONG hmq;
245
246// hmq = WinQueryWindowULong(HWND_DESKTOP, QWL_HMQ);
247 return (ULONG)WinCreateMsgQueue((HAB)hab, 0);
248}
249//******************************************************************************
250//******************************************************************************
251ULONG OSLibQueryCountry()
252{
253 COUNTRYCODE Country = {0}; /* Country code info (0 = current country) */
254 COUNTRYINFO CtryInfo = {0}; /* Buffer for country-specific information */
255 ULONG ulInfoLen = 0;
256 APIRET rc = NO_ERROR; /* Return code */
257
258 rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country,
259 &CtryInfo, &ulInfoLen);
260
261 if (rc != NO_ERROR) {
262 return -1;
263 }
264 return CtryInfo.country;
265}
266//******************************************************************************
267//******************************************************************************
Note: See TracBrowser for help on using the repository browser.