| 1 | /* $Id: oslibmisc.cpp,v 1.18 2003-03-03 16:38:20 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_DOSERRORS
|
|---|
| 17 | #define INCL_DOSSEL
|
|---|
| 18 | #define INCL_DOSNLS /* National Language Support values */
|
|---|
| 19 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 20 | #include <string.h>
|
|---|
| 21 | #include <stdlib.h>
|
|---|
| 22 | #include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 23 | #include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 24 | #include "oslibmisc.h"
|
|---|
| 25 | #include <misc.h>
|
|---|
| 26 |
|
|---|
| 27 | #define DBG_LOCALLOG DBG_oslibmisc
|
|---|
| 28 | #include "dbglocal.h"
|
|---|
| 29 |
|
|---|
| 30 | //******************************************************************************
|
|---|
| 31 | //TODO: not reentrant!
|
|---|
| 32 | //******************************************************************************
|
|---|
| 33 | char *OSLibGetDllName(ULONG hModule)
|
|---|
| 34 | {
|
|---|
| 35 | static char modname[CCHMAXPATH] = {0};
|
|---|
| 36 |
|
|---|
| 37 | if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
|
|---|
| 38 | return NULL;
|
|---|
| 39 | }
|
|---|
| 40 | return(modname);
|
|---|
| 41 | }
|
|---|
| 42 | //******************************************************************************
|
|---|
| 43 | //******************************************************************************
|
|---|
| 44 | BOOL OSLibGetDllName(ULONG hModule, char *name, int length)
|
|---|
| 45 | {
|
|---|
| 46 | return DosQueryModuleName(hModule, length, name) == 0;
|
|---|
| 47 | }
|
|---|
| 48 | //******************************************************************************
|
|---|
| 49 | /******************************************************************************
|
|---|
| 50 | * Name : ULONG OSLibiGetModuleHandleA
|
|---|
| 51 | * Purpose : replacement for IBM Open32's GetModuleHandle
|
|---|
| 52 | * Parameters: LPCTSTR lpszModule
|
|---|
| 53 | * Variables :
|
|---|
| 54 | * Result : HMODULE hModule or NULLHANDLE in case of error
|
|---|
| 55 | * Remark :
|
|---|
| 56 | * Status : REWRITTEN UNTESTED
|
|---|
| 57 | *
|
|---|
| 58 | * Author : Patrick Haller [Sun, 1998/04/04 01:55]
|
|---|
| 59 | *****************************************************************************/
|
|---|
| 60 |
|
|---|
| 61 | ULONG OSLibiGetModuleHandleA(char * pszModule)
|
|---|
| 62 | {
|
|---|
| 63 | HMODULE hModule; /* module handle */
|
|---|
| 64 | APIRET rc; /* API returncode */
|
|---|
| 65 | static HMODULE hModuleExe = 0; /* "cached" hModuleExe */
|
|---|
| 66 | PTIB pTIB; /* parameters for DosGetInfoBlocks */
|
|---|
| 67 | PPIB pPIB;
|
|---|
| 68 |
|
|---|
| 69 | dprintf(("KERNEL32:GetModuleHandle(%x)\n",
|
|---|
| 70 | pszModule));
|
|---|
| 71 |
|
|---|
| 72 | /* @@@PH 98/04/04
|
|---|
| 73 |
|
|---|
| 74 | this Open32 function is broken for pszModule == NULL
|
|---|
| 75 | return(GetModuleHandle(pszModule));
|
|---|
| 76 |
|
|---|
| 77 | Open32 always returns -1 here, however it should return the handle
|
|---|
| 78 | of the current process. MFC30 crashes.
|
|---|
| 79 |
|
|---|
| 80 | SvL, me thinks for PELDR support, you'll have to rewrite
|
|---|
| 81 | this code anyway :)
|
|---|
| 82 |
|
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | if (NULL == pszModule) /* obtain handle to current executable */
|
|---|
| 86 | {
|
|---|
| 87 | if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
|
|---|
| 88 | return (hModuleExe);
|
|---|
| 89 |
|
|---|
| 90 | rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
|
|---|
| 91 | &pPIB);
|
|---|
| 92 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 93 | {
|
|---|
| 94 | return (NULLHANDLE); /* signal failure */
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | hModuleExe = pPIB->pib_hmte; /* set cached module */
|
|---|
| 98 | hModule = pPIB->pib_hmte; /* module table entry ID */
|
|---|
| 99 | }
|
|---|
| 100 | else
|
|---|
| 101 | {
|
|---|
| 102 | rc = DosQueryModuleHandle(pszModule, /* query module handle */
|
|---|
| 103 | &hModule);
|
|---|
| 104 |
|
|---|
| 105 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 106 | {
|
|---|
| 107 | return (NULLHANDLE); /* signal failure */
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return (hModule); /* return determined handle */
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | ULONG OSLibQueryModuleHandle(char *modname)
|
|---|
| 116 | {
|
|---|
| 117 | HMODULE hModule;
|
|---|
| 118 | APIRET rc;
|
|---|
| 119 |
|
|---|
| 120 | rc = DosQueryModuleHandle(modname, /* query module handle */
|
|---|
| 121 | &hModule);
|
|---|
| 122 | if(rc)
|
|---|
| 123 | return(-1);
|
|---|
| 124 |
|
|---|
| 125 | return(hModule);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | void OSLibWait(ULONG msec)
|
|---|
| 129 | {
|
|---|
| 130 | DosSleep(msec);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | //******************************************************************************
|
|---|
| 134 | //Wrapper for Dos16AllocSeg
|
|---|
| 135 | //******************************************************************************
|
|---|
| 136 | ULONG OSLibAllocSel(ULONG size, USHORT *selector)
|
|---|
| 137 | {
|
|---|
| 138 | #if 1
|
|---|
| 139 | PVOID pSelMem;
|
|---|
| 140 | ULONG sel;
|
|---|
| 141 | APIRET rc;
|
|---|
| 142 |
|
|---|
| 143 | rc = DosAllocMem(&pSelMem, size, PAG_COMMIT|PAG_READ|PAG_WRITE|OBJ_TILE);
|
|---|
| 144 | if(rc != NO_ERROR) {
|
|---|
| 145 | dprintf(("OSLibAllocSel: DosAllocMem failed with %d", rc));
|
|---|
| 146 | DebugInt3();
|
|---|
| 147 | return FALSE;
|
|---|
| 148 | }
|
|---|
| 149 | *selector = (DosFlatToSel((ULONG)pSelMem) >> 16);
|
|---|
| 150 | return *selector != 0;
|
|---|
| 151 | #else
|
|---|
| 152 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
|
|---|
| 153 | #endif
|
|---|
| 154 | }
|
|---|
| 155 | //******************************************************************************
|
|---|
| 156 | //Wrapper for Dos16FreeSeg
|
|---|
| 157 | //******************************************************************************
|
|---|
| 158 | ULONG OSLibFreeSel(USHORT selector)
|
|---|
| 159 | {
|
|---|
| 160 | #if 1
|
|---|
| 161 | PVOID pSelMem;
|
|---|
| 162 | APIRET rc;
|
|---|
| 163 |
|
|---|
| 164 | pSelMem = (PVOID)DosSelToFlat(selector << 16);
|
|---|
| 165 | rc = DosFreeMem(pSelMem);
|
|---|
| 166 | return rc == NO_ERROR;
|
|---|
| 167 | #else
|
|---|
| 168 | return (Dos16FreeSeg(selector) == 0);
|
|---|
| 169 | #endif
|
|---|
| 170 | }
|
|---|
| 171 | //******************************************************************************
|
|---|
| 172 | //Wrapper for Dos32SelToFlat
|
|---|
| 173 | //******************************************************************************
|
|---|
| 174 | PVOID OSLibSelToFlat(USHORT selector)
|
|---|
| 175 | {
|
|---|
| 176 | return (PVOID)DosSelToFlat(selector << 16);
|
|---|
| 177 | }
|
|---|
| 178 | //******************************************************************************
|
|---|
| 179 | //Get TIB data
|
|---|
| 180 | //******************************************************************************
|
|---|
| 181 | ULONG OSLibGetTIB(int tiboff)
|
|---|
| 182 | {
|
|---|
| 183 | PTIB ptib;
|
|---|
| 184 | PPIB ppib;
|
|---|
| 185 | APIRET rc;
|
|---|
| 186 |
|
|---|
| 187 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 188 | if(rc) {
|
|---|
| 189 | return 0;
|
|---|
| 190 | }
|
|---|
| 191 | switch(tiboff)
|
|---|
| 192 | {
|
|---|
| 193 | case TIB_STACKTOP:
|
|---|
| 194 | return (ULONG)ptib->tib_pstacklimit;
|
|---|
| 195 | case TIB_STACKLOW:
|
|---|
| 196 | return (ULONG)ptib->tib_pstack;
|
|---|
| 197 | default:
|
|---|
| 198 | return 0;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Gets a PIB data.
|
|---|
| 204 | * @returns Requested PIB data.
|
|---|
| 205 | * 0 may indicate error or that the PIB data you requested actually is 0.
|
|---|
| 206 | * @param iPIB PIB data index. (one of the PIB_* defines in oslibmisc.h)
|
|---|
| 207 | * @author
|
|---|
| 208 | * @remark Spooky error handling.
|
|---|
| 209 | */
|
|---|
| 210 | ULONG OSLibGetPIB(int iPIB)
|
|---|
| 211 | {
|
|---|
| 212 | PTIB ptib;
|
|---|
| 213 | PPIB ppib;
|
|---|
| 214 | APIRET rc;
|
|---|
| 215 |
|
|---|
| 216 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 217 | if (rc)
|
|---|
| 218 | {
|
|---|
| 219 | dprintf(("KERNEL32: OSLibGetPIB(%d): DosGetInfoBlocks failed with rc=%d\n", iPIB, rc));
|
|---|
| 220 | return 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | switch(iPIB)
|
|---|
| 224 | {
|
|---|
| 225 | case PIB_TASKHNDL:
|
|---|
| 226 | return ppib->pib_hmte;
|
|---|
| 227 |
|
|---|
| 228 | case PIB_TASKTYPE:
|
|---|
| 229 | return (ppib->pib_ultype == 3) ? TASKTYPE_PM : TASKTYPE_VIO;
|
|---|
| 230 |
|
|---|
| 231 | case PIB_PCHCMD:
|
|---|
| 232 | return (ULONG)ppib->pib_pchcmd;
|
|---|
| 233 |
|
|---|
| 234 | default:
|
|---|
| 235 | dprintf(("KERNEL32: OSLibGetPIB(%d): Invalid PIB data index\n.", iPIB));
|
|---|
| 236 | DebugInt3();
|
|---|
| 237 | return 0;
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 | //******************************************************************************
|
|---|
| 241 | //Allocate local thread memory
|
|---|
| 242 | //******************************************************************************
|
|---|
| 243 | ULONG OSLibAllocThreadLocalMemory(int nrdwords)
|
|---|
| 244 | {
|
|---|
| 245 | APIRET rc;
|
|---|
| 246 | PULONG thrdaddr;
|
|---|
| 247 |
|
|---|
| 248 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
|
|---|
| 249 | if(rc) {
|
|---|
| 250 | dprintf(("DosAllocThreadLocalMemory failed %d", rc));
|
|---|
| 251 | return 0;
|
|---|
| 252 | }
|
|---|
| 253 | return (ULONG)thrdaddr;
|
|---|
| 254 | }
|
|---|
| 255 | //******************************************************************************
|
|---|
| 256 | //******************************************************************************
|
|---|
| 257 | char *OSLibStripPath(char *path)
|
|---|
| 258 | {
|
|---|
| 259 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
|
|---|
| 260 | char *pszFilename;
|
|---|
| 261 | char *pszFilename1;
|
|---|
| 262 |
|
|---|
| 263 | pszFilename = strrchr(path, '\\'); /* find rightmost backslash */
|
|---|
| 264 | pszFilename1 = strrchr(path, '/'); /* find rightmost slash */
|
|---|
| 265 | if(pszFilename > pszFilename1 && pszFilename != NULL)
|
|---|
| 266 | return (++pszFilename); /* return pointer to next character */
|
|---|
| 267 |
|
|---|
| 268 | if (pszFilename1 != NULL)
|
|---|
| 269 | return (++pszFilename1); /* return pointer to next character */
|
|---|
| 270 |
|
|---|
| 271 | return (path); /* default return value */
|
|---|
| 272 | }
|
|---|
| 273 | //******************************************************************************
|
|---|
| 274 | //******************************************************************************
|
|---|
| 275 | ULONG OSLibWinInitialize()
|
|---|
| 276 | {
|
|---|
| 277 | return (ULONG)WinInitialize(0);
|
|---|
| 278 | }
|
|---|
| 279 | //******************************************************************************
|
|---|
| 280 | //******************************************************************************
|
|---|
| 281 | ULONG OSLibWinQueryMsgQueue(ULONG hab)
|
|---|
| 282 | {
|
|---|
| 283 | ULONG hmq;
|
|---|
| 284 | APIRET rc;
|
|---|
| 285 | PTIB ptib;
|
|---|
| 286 | PPIB ppib;
|
|---|
| 287 |
|
|---|
| 288 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 289 | if(rc != NO_ERROR) {
|
|---|
| 290 | dprintf(("DosGetInfoBlocks failed with rc %d", rc));
|
|---|
| 291 | DebugInt3();
|
|---|
| 292 | return 0;
|
|---|
| 293 | }
|
|---|
| 294 | if(ppib->pib_ultype == 2) {
|
|---|
| 295 | dprintf(("Warning: app type changed back to VIO!!"));
|
|---|
| 296 | ppib->pib_ultype = 3;
|
|---|
| 297 | }
|
|---|
| 298 | hmq = WinQueueFromID(hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid);
|
|---|
| 299 |
|
|---|
| 300 | if(!hmq) {
|
|---|
| 301 | dprintf(("WinQueueFromID %x %x %x proc type %x failed with error %x", hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid, ppib->pib_ultype, WinGetLastError(hab)));
|
|---|
| 302 |
|
|---|
| 303 | hmq = (ULONG)WinCreateMsgQueue((HAB)hab, 0);
|
|---|
| 304 | if(!hmq) {
|
|---|
| 305 | dprintf(("WinCreateMsgQueue failed with error %x", WinGetLastError(hab)));
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 | return hmq;
|
|---|
| 309 | }
|
|---|
| 310 | //******************************************************************************
|
|---|
| 311 | //******************************************************************************
|
|---|
| 312 | ULONG OSLibWinSetCp(ULONG hmq, ULONG codepage)
|
|---|
| 313 | {
|
|---|
| 314 | return WinSetCp(hmq, codepage);
|
|---|
| 315 | }
|
|---|
| 316 | //******************************************************************************
|
|---|
| 317 | //******************************************************************************
|
|---|
| 318 | ULONG OSLibQueryCountry()
|
|---|
| 319 | {
|
|---|
| 320 | COUNTRYCODE Country = {0}; /* Country code info (0 = current country) */
|
|---|
| 321 | COUNTRYINFO CtryInfo = {0}; /* Buffer for country-specific information */
|
|---|
| 322 | ULONG ulInfoLen = 0;
|
|---|
| 323 | APIRET rc = NO_ERROR; /* Return code */
|
|---|
| 324 |
|
|---|
| 325 | rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country,
|
|---|
| 326 | &CtryInfo, &ulInfoLen);
|
|---|
| 327 |
|
|---|
| 328 | if (rc != NO_ERROR) {
|
|---|
| 329 | return -1;
|
|---|
| 330 | }
|
|---|
| 331 | return CtryInfo.country;
|
|---|
| 332 | }
|
|---|
| 333 | //******************************************************************************
|
|---|
| 334 | //******************************************************************************
|
|---|
| 335 | BOOL OSLibDisablePopups()
|
|---|
| 336 | {
|
|---|
| 337 | return DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR) == NO_ERROR;
|
|---|
| 338 | }
|
|---|
| 339 | //******************************************************************************
|
|---|
| 340 | //******************************************************************************
|
|---|