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