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