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