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