1 | /* $Id: os2util.cpp,v 1.5 1999-06-19 17:58:32 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Misc 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_BASE
|
---|
15 | #define INCL_DOSPROCESS
|
---|
16 | #define INCL_DOSSEL
|
---|
17 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
18 | #include <string.h>
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
|
---|
21 | #include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
|
---|
22 | #include "os2util.h"
|
---|
23 | #include "misc.h"
|
---|
24 | #include "version.h"
|
---|
25 |
|
---|
26 | /***********************************
|
---|
27 | * PH: fixups for missing os2win.h *
|
---|
28 | ***********************************/
|
---|
29 |
|
---|
30 | void _System SetLastError(ULONG ulError);
|
---|
31 |
|
---|
32 | //******************************************************************************
|
---|
33 | //******************************************************************************
|
---|
34 | void OS2SetExitList(unsigned long handler)
|
---|
35 | {
|
---|
36 | APIRET rc;
|
---|
37 |
|
---|
38 | rc = DosExitList(EXLST_ADD | 0x00002A00, (PFNEXITLIST)handler);
|
---|
39 | if(rc) {
|
---|
40 | dprintf(("DosExitList returned %d\n", rc));
|
---|
41 | }
|
---|
42 | }
|
---|
43 | //******************************************************************************
|
---|
44 | //******************************************************************************
|
---|
45 | void OS2ClearExitList()
|
---|
46 | {
|
---|
47 | DosExitList(EXLST_EXIT, NULL);
|
---|
48 | }
|
---|
49 | //******************************************************************************
|
---|
50 | //******************************************************************************
|
---|
51 | void OS2RemoveExitList(unsigned long handler)
|
---|
52 | {
|
---|
53 | DosExitList(EXLST_REMOVE, (PFNEXITLIST)handler);
|
---|
54 | }
|
---|
55 | //******************************************************************************
|
---|
56 | //TODO: not reentrant!
|
---|
57 | //******************************************************************************
|
---|
58 | char *OS2GetDllName(ULONG hModule)
|
---|
59 | {
|
---|
60 | static char modname[CCHMAXPATH] = {0};
|
---|
61 |
|
---|
62 | DosQueryModuleName(hModule, CCHMAXPATH, modname);
|
---|
63 | return(modname);
|
---|
64 | }
|
---|
65 | //******************************************************************************
|
---|
66 | void SYSTEM CheckVersion(ULONG version, char *modname)
|
---|
67 | {
|
---|
68 | dprintf(("CheckVersion of %s, %d\n", modname, version));
|
---|
69 | if(version != PE2LX_VERSION){
|
---|
70 | static char msg[300];
|
---|
71 | int r;
|
---|
72 | dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
|
---|
73 | sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
|
---|
74 | do{
|
---|
75 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
---|
76 | }while(r == MBID_RETRY); // giggle
|
---|
77 | if( r != MBID_IGNORE )
|
---|
78 | exit(987);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
|
---|
83 | {
|
---|
84 | char name[_MAX_PATH];
|
---|
85 |
|
---|
86 | // query name of dll.
|
---|
87 | if(!DosQueryModuleName(hModule, sizeof(name), name))
|
---|
88 | CheckVersion(version, name);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*****************************************************************************
|
---|
92 | * Name : HMODULE OS2iGetModuleHandleA
|
---|
93 | * Purpose : replacement for IBM Open32's GetModuleHandle
|
---|
94 | * Parameters: LPCTSTR lpszModule
|
---|
95 | * Variables :
|
---|
96 | * Result : HMODULE hModule or NULLHANDLE in case of error
|
---|
97 | * Remark :
|
---|
98 | * Status : REWRITTEN UNTESTED
|
---|
99 | *
|
---|
100 | * Author : Patrick Haller [Sun, 1998/04/04 01:55]
|
---|
101 | *****************************************************************************/
|
---|
102 |
|
---|
103 | HMODULE OS2iGetModuleHandleA(PSZ pszModule)
|
---|
104 | {
|
---|
105 | HMODULE hModule; /* module handle */
|
---|
106 | APIRET rc; /* API returncode */
|
---|
107 | static HMODULE hModuleExe; /* "cached" hModuleExe */
|
---|
108 | PTIB pTIB; /* parameters for DosGetInfoBlocks */
|
---|
109 | PPIB pPIB;
|
---|
110 |
|
---|
111 | dprintf(("KERNEL32:GetModuleHandle(%s)\n",
|
---|
112 | pszModule));
|
---|
113 |
|
---|
114 | /* @@@PH 98/04/04
|
---|
115 |
|
---|
116 | this Open32 function is broken for pszModule == NULL
|
---|
117 | return(GetModuleHandle(pszModule));
|
---|
118 |
|
---|
119 | Open32 always returns -1 here, however it should return the handle
|
---|
120 | of the current process. MFC30 crashes.
|
---|
121 |
|
---|
122 | SvL, me thinks for PELDR support, you'll have to rewrite
|
---|
123 | this code anyway :)
|
---|
124 |
|
---|
125 | */
|
---|
126 |
|
---|
127 | if (NULL == pszModule) /* obtain handle to current executable */
|
---|
128 | {
|
---|
129 | if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
|
---|
130 | return (hModuleExe);
|
---|
131 |
|
---|
132 | rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
|
---|
133 | &pPIB);
|
---|
134 | if (rc != NO_ERROR) /* check for errors */
|
---|
135 | {
|
---|
136 | SetLastError(rc); /* set error code */
|
---|
137 | return (NULLHANDLE); /* signal failure */
|
---|
138 | }
|
---|
139 |
|
---|
140 | hModuleExe = pPIB->pib_hmte; /* set cached module */
|
---|
141 | hModule = pPIB->pib_hmte; /* module table entry ID */
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | rc = DosQueryModuleHandle(pszModule, /* query module handle */
|
---|
146 | &hModule);
|
---|
147 |
|
---|
148 | if (rc != NO_ERROR) /* check for errors */
|
---|
149 | {
|
---|
150 | SetLastError(rc); /* set error code */
|
---|
151 | return (NULLHANDLE); /* signal failure */
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | return (hModule); /* return determined handle */
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | HMODULE OS2QueryModuleHandle(char *modname)
|
---|
160 | {
|
---|
161 | HMODULE hModule;
|
---|
162 | APIRET rc;
|
---|
163 |
|
---|
164 | rc = DosQueryModuleHandle(modname, /* query module handle */
|
---|
165 | &hModule);
|
---|
166 | if(rc)
|
---|
167 | return(-1);
|
---|
168 |
|
---|
169 | return(hModule);
|
---|
170 | }
|
---|
171 |
|
---|
172 | //SvL: only for RT_RCDATA!
|
---|
173 | ULONG OS2GetResourceSize(HMODULE hinstance, int id)
|
---|
174 | {
|
---|
175 | APIRET rc;
|
---|
176 | ULONG size;
|
---|
177 |
|
---|
178 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
---|
179 | if(rc) {
|
---|
180 | dprintf(("DosQueryResourceSize returned %d, %X id = %d\n", rc, hinstance, id));
|
---|
181 | return(0);
|
---|
182 | }
|
---|
183 | return(size);
|
---|
184 | }
|
---|
185 |
|
---|
186 | BOOL OS2GetResource(HMODULE hinstance, int id, char *destbuf, int bufLength)
|
---|
187 | {
|
---|
188 | APIRET rc;
|
---|
189 | char *resdata;
|
---|
190 | ULONG size;
|
---|
191 |
|
---|
192 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
---|
193 | if(rc) {
|
---|
194 | dprintf(("OS2GetResource: Can't get resource size of %d!!!\n", id));
|
---|
195 | return(0);
|
---|
196 | }
|
---|
197 | rc = DosGetResource(hinstance, RT_RCDATA, id, (PPVOID)&resdata);
|
---|
198 | if(rc) {
|
---|
199 | dprintf(("OS2GetResource: Can't find resource %d!!!\n", id));
|
---|
200 | return(0);
|
---|
201 | }
|
---|
202 | dprintf(("OS2GetResoure: bufLength %d, size %d, id %d", bufLength, size, id));
|
---|
203 | size = min(size, bufLength);
|
---|
204 | memcpy(destbuf, resdata, size);
|
---|
205 | DosFreeResource(resdata);
|
---|
206 |
|
---|
207 | return(FALSE);
|
---|
208 | }
|
---|
209 |
|
---|
210 | void OS2Wait(ULONG msec)
|
---|
211 | {
|
---|
212 | DosSleep(msec);
|
---|
213 | }
|
---|
214 |
|
---|
215 | //******************************************************************************
|
---|
216 | //Wrapper for Dos16AllocSeg
|
---|
217 | //******************************************************************************
|
---|
218 | BOOL OS2AllocSel(ULONG size, USHORT *selector)
|
---|
219 | {
|
---|
220 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
|
---|
221 | }
|
---|
222 | //******************************************************************************
|
---|
223 | //Wrapper for Dos16FreeSeg
|
---|
224 | //******************************************************************************
|
---|
225 | BOOL OS2FreeSel(USHORT selector)
|
---|
226 | {
|
---|
227 | return (Dos16FreeSeg(selector) == 0);
|
---|
228 | }
|
---|
229 | //******************************************************************************
|
---|
230 | //Wrapper for Dos32SelToFlat
|
---|
231 | //******************************************************************************
|
---|
232 | PVOID OS2SelToFlat(USHORT selector)
|
---|
233 | {
|
---|
234 | return (PVOID)DosSelToFlat(selector << 16);
|
---|
235 | }
|
---|
236 | //******************************************************************************
|
---|
237 | //Get TIB data
|
---|
238 | //******************************************************************************
|
---|
239 | ULONG OS2GetTIB(int tiboff)
|
---|
240 | {
|
---|
241 | PTIB ptib;
|
---|
242 | PPIB ppib;
|
---|
243 | APIRET rc;
|
---|
244 |
|
---|
245 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
246 | if(rc) {
|
---|
247 | return 0;
|
---|
248 | }
|
---|
249 | switch(tiboff)
|
---|
250 | {
|
---|
251 | case TIB_STACKTOP:
|
---|
252 | return (ULONG)ptib->tib_pstack;
|
---|
253 | case TIB_STACKLOW:
|
---|
254 | return (ULONG)ptib->tib_pstacklimit;
|
---|
255 | default:
|
---|
256 | return 0;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | //******************************************************************************
|
---|
260 | //Get PIB data
|
---|
261 | //******************************************************************************
|
---|
262 | ULONG OS2GetPIB(int piboff)
|
---|
263 | {
|
---|
264 | PTIB ptib;
|
---|
265 | PPIB ppib;
|
---|
266 | APIRET rc;
|
---|
267 |
|
---|
268 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
269 | if(rc) {
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 | switch(piboff)
|
---|
273 | {
|
---|
274 | case PIB_TASKHNDL:
|
---|
275 | return ppib->pib_hmte;
|
---|
276 | case PIB_TASKTYPE:
|
---|
277 | if(ppib->pib_ultype == 3) {
|
---|
278 | return TASKTYPE_PM;
|
---|
279 | }
|
---|
280 | else return TASKTYPE_VIO;
|
---|
281 | default:
|
---|
282 | return 0;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | //******************************************************************************
|
---|
286 | //Allocate local thread memory
|
---|
287 | //******************************************************************************
|
---|
288 | ULONG OS2AllocThreadLocalMemory(int nrdwords)
|
---|
289 | {
|
---|
290 | APIRET rc;
|
---|
291 | PULONG thrdaddr;
|
---|
292 |
|
---|
293 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
|
---|
294 | if(rc) {
|
---|
295 | dprintf(("DosAllocThreadLocalMemory failed %d", rc));
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 | return (ULONG)thrdaddr;
|
---|
299 | }
|
---|
300 | //******************************************************************************
|
---|
301 | //******************************************************************************
|
---|
302 |
|
---|