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