| 1 | /*
|
|---|
| 2 | * Misc util. procedures
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 5 | * Copyright 1998 Peter FitzSimmons
|
|---|
| 6 | * Copyright 1998 Patrick Haller
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 | #define INCL_BASE
|
|---|
| 13 | #define INCL_DOSPROCESS
|
|---|
| 14 | #include <os2.h>
|
|---|
| 15 | #include <string.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 | #include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 18 | #include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 19 | #include "os2util.h"
|
|---|
| 20 | #include "misc.h"
|
|---|
| 21 | #include "version.h"
|
|---|
| 22 |
|
|---|
| 23 | /***********************************
|
|---|
| 24 | * PH: fixups for missing os2win.h *
|
|---|
| 25 | ***********************************/
|
|---|
| 26 |
|
|---|
| 27 | void _System SetLastError(ULONG ulError);
|
|---|
| 28 |
|
|---|
| 29 | //******************************************************************************
|
|---|
| 30 | //******************************************************************************
|
|---|
| 31 | void OS2SetExitList(unsigned long handler)
|
|---|
| 32 | {
|
|---|
| 33 | APIRET rc;
|
|---|
| 34 |
|
|---|
| 35 | rc = DosExitList(EXLST_ADD | 0x00002A00, (PFNEXITLIST)handler);
|
|---|
| 36 | if(rc) {
|
|---|
| 37 | dprintf(("DosExitList returned %d\n", rc));
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | //******************************************************************************
|
|---|
| 42 | void OS2ClearExitList()
|
|---|
| 43 | {
|
|---|
| 44 | DosExitList(EXLST_EXIT, NULL);
|
|---|
| 45 | }
|
|---|
| 46 | //******************************************************************************
|
|---|
| 47 | //******************************************************************************
|
|---|
| 48 | void OS2RemoveExitList(unsigned long handler)
|
|---|
| 49 | {
|
|---|
| 50 | DosExitList(EXLST_REMOVE, (PFNEXITLIST)handler);
|
|---|
| 51 | }
|
|---|
| 52 | //******************************************************************************
|
|---|
| 53 | //TODO: not reentrant!
|
|---|
| 54 | //******************************************************************************
|
|---|
| 55 | char *OS2GetDllName(ULONG hModule)
|
|---|
| 56 | {
|
|---|
| 57 | static char modname[CCHMAXPATH] = {0};
|
|---|
| 58 |
|
|---|
| 59 | DosQueryModuleName(hModule, CCHMAXPATH, modname);
|
|---|
| 60 | return(modname);
|
|---|
| 61 | }
|
|---|
| 62 | //******************************************************************************
|
|---|
| 63 | void SYSTEM CheckVersion(ULONG version, char *modname)
|
|---|
| 64 | {
|
|---|
| 65 | dprintf(("CheckVersion of %s, %d\n", modname, version));
|
|---|
| 66 | if(version != PE2LX_VERSION){
|
|---|
| 67 | static char msg[300];
|
|---|
| 68 | int r;
|
|---|
| 69 | dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
|
|---|
| 70 | sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
|
|---|
| 71 | do{
|
|---|
| 72 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
|---|
| 73 | }while(r == MBID_RETRY); // giggle
|
|---|
| 74 | if( r != MBID_IGNORE )
|
|---|
| 75 | exit(987);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
|
|---|
| 80 | {
|
|---|
| 81 | char name[_MAX_PATH];
|
|---|
| 82 |
|
|---|
| 83 | // query name of dll.
|
|---|
| 84 | if(!DosQueryModuleName(hModule, sizeof(name), name))
|
|---|
| 85 | CheckVersion(version, name);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /*****************************************************************************
|
|---|
| 89 | * Name : HMODULE OS2iGetModuleHandleA
|
|---|
| 90 | * Purpose : replacement for IBM Open32's GetModuleHandle
|
|---|
| 91 | * Parameters: LPCTSTR lpszModule
|
|---|
| 92 | * Variables :
|
|---|
| 93 | * Result : HMODULE hModule or NULLHANDLE in case of error
|
|---|
| 94 | * Remark :
|
|---|
| 95 | * Status : REWRITTEN UNTESTED
|
|---|
| 96 | *
|
|---|
| 97 | * Author : Patrick Haller [Sun, 1998/04/04 01:55]
|
|---|
| 98 | *****************************************************************************/
|
|---|
| 99 |
|
|---|
| 100 | HMODULE OS2iGetModuleHandleA(PSZ pszModule)
|
|---|
| 101 | {
|
|---|
| 102 | HMODULE hModule; /* module handle */
|
|---|
| 103 | APIRET rc; /* API returncode */
|
|---|
| 104 | static HMODULE hModuleExe; /* "cached" hModuleExe */
|
|---|
| 105 | PTIB pTIB; /* parameters for DosGetInfoBlocks */
|
|---|
| 106 | PPIB pPIB;
|
|---|
| 107 |
|
|---|
| 108 | dprintf(("KERNEL32:GetModuleHandle(%s)\n",
|
|---|
| 109 | pszModule));
|
|---|
| 110 |
|
|---|
| 111 | /* @@@PH 98/04/04
|
|---|
| 112 |
|
|---|
| 113 | this Open32 function is broken for pszModule == NULL
|
|---|
| 114 | return(GetModuleHandle(pszModule));
|
|---|
| 115 |
|
|---|
| 116 | Open32 always returns -1 here, however it should return the handle
|
|---|
| 117 | of the current process. MFC30 crashes.
|
|---|
| 118 |
|
|---|
| 119 | SvL, me thinks for PELDR support, you'll have to rewrite
|
|---|
| 120 | this code anyway :)
|
|---|
| 121 |
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | if (NULL == pszModule) /* obtain handle to current executable */
|
|---|
| 125 | {
|
|---|
| 126 | if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
|
|---|
| 127 | return (hModuleExe);
|
|---|
| 128 |
|
|---|
| 129 | rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
|
|---|
| 130 | &pPIB);
|
|---|
| 131 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 132 | {
|
|---|
| 133 | SetLastError(rc); /* set error code */
|
|---|
| 134 | return (NULLHANDLE); /* signal failure */
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | hModuleExe = pPIB->pib_hmte; /* set cached module */
|
|---|
| 138 | hModule = pPIB->pib_hmte; /* module table entry ID */
|
|---|
| 139 | }
|
|---|
| 140 | else
|
|---|
| 141 | {
|
|---|
| 142 | rc = DosQueryModuleHandle(pszModule, /* query module handle */
|
|---|
| 143 | &hModule);
|
|---|
| 144 |
|
|---|
| 145 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 146 | {
|
|---|
| 147 | SetLastError(rc); /* set error code */
|
|---|
| 148 | return (NULLHANDLE); /* signal failure */
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return (hModule); /* return determined handle */
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | HMODULE OS2QueryModuleHandle(char *modname)
|
|---|
| 157 | {
|
|---|
| 158 | HMODULE hModule;
|
|---|
| 159 | APIRET rc;
|
|---|
| 160 |
|
|---|
| 161 | rc = DosQueryModuleHandle(modname, /* query module handle */
|
|---|
| 162 | &hModule);
|
|---|
| 163 | if(rc)
|
|---|
| 164 | return(-1);
|
|---|
| 165 |
|
|---|
| 166 | return(hModule);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | //SvL: only for RT_RCDATA!
|
|---|
| 170 | ULONG OS2GetResourceSize(HMODULE hinstance, int id)
|
|---|
| 171 | {
|
|---|
| 172 | APIRET rc;
|
|---|
| 173 | ULONG size;
|
|---|
| 174 |
|
|---|
| 175 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
|---|
| 176 | if(rc) {
|
|---|
| 177 | dprintf(("DosQueryResourceSize returned %d, %X id = %d\n", rc, hinstance, id));
|
|---|
| 178 | return(0);
|
|---|
| 179 | }
|
|---|
| 180 | return(size);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | BOOL OS2GetResource(HMODULE hinstance, int id, char *destbuf, int bufLength)
|
|---|
| 184 | {
|
|---|
| 185 | APIRET rc;
|
|---|
| 186 | char *resdata;
|
|---|
| 187 | ULONG size;
|
|---|
| 188 |
|
|---|
| 189 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
|---|
| 190 | if(rc) {
|
|---|
| 191 | dprintf(("OS2GetResource: Can't get resource size of %d!!!\n", id));
|
|---|
| 192 | return(0);
|
|---|
| 193 | }
|
|---|
| 194 | rc = DosGetResource(hinstance, RT_RCDATA, id, (PPVOID)&resdata);
|
|---|
| 195 | if(rc) {
|
|---|
| 196 | dprintf(("OS2GetResource: Can't find resource %d!!!\n", id));
|
|---|
| 197 | return(0);
|
|---|
| 198 | }
|
|---|
| 199 | dprintf(("OS2GetResoure: bufLength %d, size %d, id %d", bufLength, size, id));
|
|---|
| 200 | size = min(size, bufLength);
|
|---|
| 201 | memcpy(destbuf, resdata, size);
|
|---|
| 202 | DosFreeResource(resdata);
|
|---|
| 203 |
|
|---|
| 204 | return(FALSE);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 | void OS2Wait(ULONG msec)
|
|---|
| 211 | {
|
|---|
| 212 | DosSleep(msec);
|
|---|
| 213 | }
|
|---|