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