1 | /* $Id: oslibmisc.cpp,v 1.6 2000-02-16 14:25:45 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Misc OS/2 util. procedures
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1998 Peter FitzSimmons
|
---|
8 | * Copyright 1998 Patrick Haller
|
---|
9 | *
|
---|
10 | *
|
---|
11 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
12 | *
|
---|
13 | */
|
---|
14 | #define INCL_WIN
|
---|
15 | #define INCL_BASE
|
---|
16 | #define INCL_DOSPROCESS
|
---|
17 | #define INCL_DOSSEL
|
---|
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 | * PH: fixups for missing os2win.h *
|
---|
31 | ***********************************/
|
---|
32 |
|
---|
33 | void _System SetLastError(ULONG ulError);
|
---|
34 |
|
---|
35 | //******************************************************************************
|
---|
36 | //******************************************************************************
|
---|
37 | void OSLibSetExitList(unsigned long handler)
|
---|
38 | {
|
---|
39 | APIRET rc;
|
---|
40 |
|
---|
41 | rc = DosExitList(EXLST_ADD | 0x00002A00, (PFNEXITLIST)handler);
|
---|
42 | if(rc) {
|
---|
43 | dprintf(("DosExitList returned %d\n", rc));
|
---|
44 | }
|
---|
45 | }
|
---|
46 | //******************************************************************************
|
---|
47 | //******************************************************************************
|
---|
48 | void OSLibClearExitList()
|
---|
49 | {
|
---|
50 | DosExitList(EXLST_EXIT, NULL);
|
---|
51 | }
|
---|
52 | //******************************************************************************
|
---|
53 | //******************************************************************************
|
---|
54 | void OSLibRemoveExitList(unsigned long handler)
|
---|
55 | {
|
---|
56 | DosExitList(EXLST_REMOVE, (PFNEXITLIST)handler);
|
---|
57 | }
|
---|
58 | //******************************************************************************
|
---|
59 | //TODO: not reentrant!
|
---|
60 | //******************************************************************************
|
---|
61 | char *OSLibGetDllName(ULONG hModule)
|
---|
62 | {
|
---|
63 | static char modname[CCHMAXPATH] = {0};
|
---|
64 |
|
---|
65 | if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
|
---|
66 | return NULL;
|
---|
67 | }
|
---|
68 | return(modname);
|
---|
69 | }
|
---|
70 | //******************************************************************************
|
---|
71 | /*****************************************************************************
|
---|
72 | * Name : ULONG OSLibiGetModuleHandleA
|
---|
73 | * Purpose : replacement for IBM Open32's GetModuleHandle
|
---|
74 | * Parameters: LPCTSTR lpszModule
|
---|
75 | * Variables :
|
---|
76 | * Result : HMODULE hModule or NULLHANDLE in case of error
|
---|
77 | * Remark :
|
---|
78 | * Status : REWRITTEN UNTESTED
|
---|
79 | *
|
---|
80 | * Author : Patrick Haller [Sun, 1998/04/04 01:55]
|
---|
81 | *****************************************************************************/
|
---|
82 |
|
---|
83 | ULONG OSLibiGetModuleHandleA(char * pszModule)
|
---|
84 | {
|
---|
85 | HMODULE hModule; /* module handle */
|
---|
86 | APIRET rc; /* API returncode */
|
---|
87 | static HMODULE hModuleExe; /* "cached" hModuleExe */
|
---|
88 | PTIB pTIB; /* parameters for DosGetInfoBlocks */
|
---|
89 | PPIB pPIB;
|
---|
90 |
|
---|
91 | dprintf(("KERNEL32:GetModuleHandle(%s)\n",
|
---|
92 | pszModule));
|
---|
93 |
|
---|
94 | /* @@@PH 98/04/04
|
---|
95 |
|
---|
96 | this Open32 function is broken for pszModule == NULL
|
---|
97 | return(GetModuleHandle(pszModule));
|
---|
98 |
|
---|
99 | Open32 always returns -1 here, however it should return the handle
|
---|
100 | of the current process. MFC30 crashes.
|
---|
101 |
|
---|
102 | SvL, me thinks for PELDR support, you'll have to rewrite
|
---|
103 | this code anyway :)
|
---|
104 |
|
---|
105 | */
|
---|
106 |
|
---|
107 | if (NULL == pszModule) /* obtain handle to current executable */
|
---|
108 | {
|
---|
109 | if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
|
---|
110 | return (hModuleExe);
|
---|
111 |
|
---|
112 | rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
|
---|
113 | &pPIB);
|
---|
114 | if (rc != NO_ERROR) /* check for errors */
|
---|
115 | {
|
---|
116 | SetLastError(rc); /* set error code */
|
---|
117 | return (NULLHANDLE); /* signal failure */
|
---|
118 | }
|
---|
119 |
|
---|
120 | hModuleExe = pPIB->pib_hmte; /* set cached module */
|
---|
121 | hModule = pPIB->pib_hmte; /* module table entry ID */
|
---|
122 | }
|
---|
123 | else
|
---|
124 | {
|
---|
125 | rc = DosQueryModuleHandle(pszModule, /* query module handle */
|
---|
126 | &hModule);
|
---|
127 |
|
---|
128 | if (rc != NO_ERROR) /* check for errors */
|
---|
129 | {
|
---|
130 | SetLastError(rc); /* set error code */
|
---|
131 | return (NULLHANDLE); /* signal failure */
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return (hModule); /* return determined handle */
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | ULONG OSLibQueryModuleHandle(char *modname)
|
---|
140 | {
|
---|
141 | HMODULE hModule;
|
---|
142 | APIRET rc;
|
---|
143 |
|
---|
144 | rc = DosQueryModuleHandle(modname, /* query module handle */
|
---|
145 | &hModule);
|
---|
146 | if(rc)
|
---|
147 | return(-1);
|
---|
148 |
|
---|
149 | return(hModule);
|
---|
150 | }
|
---|
151 |
|
---|
152 | //SvL: only for RT_RCDATA!
|
---|
153 | ULONG OSLibGetResourceSize(HMODULE hinstance, int id)
|
---|
154 | {
|
---|
155 | APIRET rc;
|
---|
156 | ULONG size;
|
---|
157 |
|
---|
158 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
---|
159 | if(rc) {
|
---|
160 | dprintf(("DosQueryResourceSize returned %d, %X id = %d\n", rc, hinstance, id));
|
---|
161 | return(0);
|
---|
162 | }
|
---|
163 | return(size);
|
---|
164 | }
|
---|
165 |
|
---|
166 | ULONG OSLibGetResource(HMODULE hinstance, int id, char *destbuf, int bufLength)
|
---|
167 | {
|
---|
168 | APIRET rc;
|
---|
169 | char *resdata;
|
---|
170 | ULONG size;
|
---|
171 |
|
---|
172 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
---|
173 | if(rc) {
|
---|
174 | dprintf(("OSLibGetResource: Can't get resource size of %d!!!\n", id));
|
---|
175 | return(FALSE);
|
---|
176 | }
|
---|
177 | rc = DosGetResource(hinstance, RT_RCDATA, id, (PPVOID)&resdata);
|
---|
178 | if(rc) {
|
---|
179 | dprintf(("OSLibGetResource: Can't find resource %d!!!\n", id));
|
---|
180 | return(FALSE);
|
---|
181 | }
|
---|
182 | dprintf(("OSLibGetResoure: bufLength %d, size %d, id %d", bufLength, size, id));
|
---|
183 | size = min(size, bufLength);
|
---|
184 | memcpy(destbuf, resdata, size);
|
---|
185 | DosFreeResource(resdata);
|
---|
186 |
|
---|
187 | return(TRUE);
|
---|
188 | }
|
---|
189 |
|
---|
190 | void OSLibWait(ULONG msec)
|
---|
191 | {
|
---|
192 | DosSleep(msec);
|
---|
193 | }
|
---|
194 |
|
---|
195 | //******************************************************************************
|
---|
196 | //Wrapper for Dos16AllocSeg
|
---|
197 | //******************************************************************************
|
---|
198 | ULONG OSLibAllocSel(ULONG size, USHORT *selector)
|
---|
199 | {
|
---|
200 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
|
---|
201 | }
|
---|
202 | //******************************************************************************
|
---|
203 | //Wrapper for Dos16FreeSeg
|
---|
204 | //******************************************************************************
|
---|
205 | ULONG OSLibFreeSel(USHORT selector)
|
---|
206 | {
|
---|
207 | return (Dos16FreeSeg(selector) == 0);
|
---|
208 | }
|
---|
209 | //******************************************************************************
|
---|
210 | //Wrapper for Dos32SelToFlat
|
---|
211 | //******************************************************************************
|
---|
212 | PVOID OSLibSelToFlat(USHORT selector)
|
---|
213 | {
|
---|
214 | return (PVOID)DosSelToFlat(selector << 16);
|
---|
215 | }
|
---|
216 | //******************************************************************************
|
---|
217 | //Get TIB data
|
---|
218 | //******************************************************************************
|
---|
219 | ULONG OSLibGetTIB(int tiboff)
|
---|
220 | {
|
---|
221 | PTIB ptib;
|
---|
222 | PPIB ppib;
|
---|
223 | APIRET rc;
|
---|
224 |
|
---|
225 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
226 | if(rc) {
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 | switch(tiboff)
|
---|
230 | {
|
---|
231 | case TIB_STACKTOP:
|
---|
232 | return (ULONG)ptib->tib_pstacklimit;
|
---|
233 | case TIB_STACKLOW:
|
---|
234 | return (ULONG)ptib->tib_pstack;
|
---|
235 | default:
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | //******************************************************************************
|
---|
240 | //Get PIB data
|
---|
241 | //******************************************************************************
|
---|
242 | ULONG OSLibGetPIB(int piboff)
|
---|
243 | {
|
---|
244 | PTIB ptib;
|
---|
245 | PPIB ppib;
|
---|
246 | APIRET rc;
|
---|
247 |
|
---|
248 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
249 | if(rc) {
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 | switch(piboff)
|
---|
253 | {
|
---|
254 | case PIB_TASKHNDL:
|
---|
255 | return ppib->pib_hmte;
|
---|
256 | case PIB_TASKTYPE:
|
---|
257 | if(ppib->pib_ultype == 3) {
|
---|
258 | return TASKTYPE_PM;
|
---|
259 | }
|
---|
260 | else return TASKTYPE_VIO;
|
---|
261 | default:
|
---|
262 | return 0;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | //******************************************************************************
|
---|
266 | //Allocate local thread memory
|
---|
267 | //******************************************************************************
|
---|
268 | ULONG OSLibAllocThreadLocalMemory(int nrdwords)
|
---|
269 | {
|
---|
270 | APIRET rc;
|
---|
271 | PULONG thrdaddr;
|
---|
272 |
|
---|
273 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
|
---|
274 | if(rc) {
|
---|
275 | dprintf(("DosAllocThreadLocalMemory failed %d", rc));
|
---|
276 | return 0;
|
---|
277 | }
|
---|
278 | return (ULONG)thrdaddr;
|
---|
279 | }
|
---|
280 | //******************************************************************************
|
---|
281 | //******************************************************************************
|
---|
282 | char *OSLibStripPath(char *path)
|
---|
283 | {
|
---|
284 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
|
---|
285 | char *pszFilename;
|
---|
286 |
|
---|
287 | pszFilename = strrchr(path, '\\'); /* find rightmost slash */
|
---|
288 | if (pszFilename != NULL)
|
---|
289 | return (++pszFilename); /* return pointer to next character */
|
---|
290 |
|
---|
291 | pszFilename = strrchr(path, '/'); /* find rightmost slash */
|
---|
292 | if (pszFilename != NULL)
|
---|
293 | return (++pszFilename); /* return pointer to next character */
|
---|
294 |
|
---|
295 | return (path); /* default return value */
|
---|
296 | }
|
---|
297 | //******************************************************************************
|
---|
298 | //******************************************************************************
|
---|
299 | ULONG OSLibWinInitialize()
|
---|
300 | {
|
---|
301 | return (ULONG)WinInitialize(0);
|
---|
302 | }
|
---|
303 | //******************************************************************************
|
---|
304 | //******************************************************************************
|
---|
305 | ULONG OSLibWinQueryMsgQueue(ULONG hab)
|
---|
306 | {
|
---|
307 | // ULONG hmq;
|
---|
308 |
|
---|
309 | // hmq = WinQueryWindowULong(HWND_DESKTOP, QWL_HMQ);
|
---|
310 | return (ULONG)WinCreateMsgQueue((HAB)hab, 0);
|
---|
311 | }
|
---|
312 | //******************************************************************************
|
---|
313 | //******************************************************************************
|
---|