1 | /* $Id: oslibmisc.cpp,v 1.11 2000-09-12 04:27:45 bird 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 | //******************************************************************************
|
---|
32 | char *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 | //******************************************************************************
|
---|
43 | BOOL 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 |
|
---|
60 | ULONG 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 |
|
---|
114 | ULONG 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 |
|
---|
127 | void OSLibWait(ULONG msec)
|
---|
128 | {
|
---|
129 | DosSleep(msec);
|
---|
130 | }
|
---|
131 |
|
---|
132 | //******************************************************************************
|
---|
133 | //Wrapper for Dos16AllocSeg
|
---|
134 | //******************************************************************************
|
---|
135 | ULONG OSLibAllocSel(ULONG size, USHORT *selector)
|
---|
136 | {
|
---|
137 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
|
---|
138 | }
|
---|
139 | //******************************************************************************
|
---|
140 | //Wrapper for Dos16FreeSeg
|
---|
141 | //******************************************************************************
|
---|
142 | ULONG OSLibFreeSel(USHORT selector)
|
---|
143 | {
|
---|
144 | return (Dos16FreeSeg(selector) == 0);
|
---|
145 | }
|
---|
146 | //******************************************************************************
|
---|
147 | //Wrapper for Dos32SelToFlat
|
---|
148 | //******************************************************************************
|
---|
149 | PVOID OSLibSelToFlat(USHORT selector)
|
---|
150 | {
|
---|
151 | return (PVOID)DosSelToFlat(selector << 16);
|
---|
152 | }
|
---|
153 | //******************************************************************************
|
---|
154 | //Get TIB data
|
---|
155 | //******************************************************************************
|
---|
156 | ULONG 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 | /**
|
---|
178 | * Gets a PIB data.
|
---|
179 | * @returns Requested PIB data.
|
---|
180 | * 0 may indicate error or that the PIB data you requested actually is 0.
|
---|
181 | * @param iPIB PIB data index. (one of the PIB_* defines in oslibmisc.h)
|
---|
182 | * @author
|
---|
183 | * @remark Spooky error handling.
|
---|
184 | */
|
---|
185 | ULONG OSLibGetPIB(int iPIB)
|
---|
186 | {
|
---|
187 | PTIB ptib;
|
---|
188 | PPIB ppib;
|
---|
189 | APIRET rc;
|
---|
190 |
|
---|
191 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
192 | if (rc)
|
---|
193 | {
|
---|
194 | dprintf(("KERNEL32: OSLibGetPIB(%d): DosGetInfoBlocks failed with rc=%d\n", iPIB, rc));
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | switch(iPIB)
|
---|
199 | {
|
---|
200 | case PIB_TASKHNDL:
|
---|
201 | return ppib->pib_hmte;
|
---|
202 |
|
---|
203 | case PIB_TASKTYPE:
|
---|
204 | return (ppib->pib_ultype == 3) ? TASKTYPE_PM : TASKTYPE_VIO;
|
---|
205 |
|
---|
206 | case PIB_PCHCMD:
|
---|
207 | return (ULONG)ppib->pib_pchcmd;
|
---|
208 |
|
---|
209 | default:
|
---|
210 | dprintf(("KERNEL32: OSLibGetPIB(%d): Invalid PIB data index\n.", iPIB));
|
---|
211 | DebugInt3();
|
---|
212 | return 0;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | //******************************************************************************
|
---|
216 | //Allocate local thread memory
|
---|
217 | //******************************************************************************
|
---|
218 | ULONG OSLibAllocThreadLocalMemory(int nrdwords)
|
---|
219 | {
|
---|
220 | APIRET rc;
|
---|
221 | PULONG thrdaddr;
|
---|
222 |
|
---|
223 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
|
---|
224 | if(rc) {
|
---|
225 | dprintf(("DosAllocThreadLocalMemory failed %d", rc));
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 | return (ULONG)thrdaddr;
|
---|
229 | }
|
---|
230 | //******************************************************************************
|
---|
231 | //******************************************************************************
|
---|
232 | char *OSLibStripPath(char *path)
|
---|
233 | {
|
---|
234 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
|
---|
235 | char *pszFilename;
|
---|
236 |
|
---|
237 | pszFilename = strrchr(path, '\\'); /* find rightmost slash */
|
---|
238 | if (pszFilename != NULL)
|
---|
239 | return (++pszFilename); /* return pointer to next character */
|
---|
240 |
|
---|
241 | pszFilename = strrchr(path, '/'); /* find rightmost slash */
|
---|
242 | if (pszFilename != NULL)
|
---|
243 | return (++pszFilename); /* return pointer to next character */
|
---|
244 |
|
---|
245 | return (path); /* default return value */
|
---|
246 | }
|
---|
247 | //******************************************************************************
|
---|
248 | //******************************************************************************
|
---|
249 | ULONG OSLibWinInitialize()
|
---|
250 | {
|
---|
251 | return (ULONG)WinInitialize(0);
|
---|
252 | }
|
---|
253 | //******************************************************************************
|
---|
254 | //******************************************************************************
|
---|
255 | ULONG OSLibWinQueryMsgQueue(ULONG hab)
|
---|
256 | {
|
---|
257 | // ULONG hmq;
|
---|
258 |
|
---|
259 | // hmq = WinQueryWindowULong(HWND_DESKTOP, QWL_HMQ);
|
---|
260 | return (ULONG)WinCreateMsgQueue((HAB)hab, 0);
|
---|
261 | }
|
---|
262 | //******************************************************************************
|
---|
263 | //******************************************************************************
|
---|
264 | ULONG OSLibQueryCountry()
|
---|
265 | {
|
---|
266 | COUNTRYCODE Country = {0}; /* Country code info (0 = current country) */
|
---|
267 | COUNTRYINFO CtryInfo = {0}; /* Buffer for country-specific information */
|
---|
268 | ULONG ulInfoLen = 0;
|
---|
269 | APIRET rc = NO_ERROR; /* Return code */
|
---|
270 |
|
---|
271 | rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country,
|
---|
272 | &CtryInfo, &ulInfoLen);
|
---|
273 |
|
---|
274 | if (rc != NO_ERROR) {
|
---|
275 | return -1;
|
---|
276 | }
|
---|
277 | return CtryInfo.country;
|
---|
278 | }
|
---|
279 | //******************************************************************************
|
---|
280 | //******************************************************************************
|
---|