source: trunk/src/kernel32/oslibmisc.cpp@ 2865

Last change on this file since 2865 was 2865, checked in by sandervl, 26 years ago

Fixed wrong calling convention for SetLastError calls in OSLibiGetModuleHandleA

File size: 7.4 KB
Line 
1/* $Id: oslibmisc.cpp,v 1.7 2000-02-23 00:57:39 sandervl Exp $ */
2//#define DEBUG
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#define DBG_LOCALLOG DBG_oslibmisc
27#include "dbglocal.h"
28
29//******************************************************************************
30//TODO: not reentrant!
31//******************************************************************************
32char *OSLibGetDllName(ULONG hModule)
33{
34 static char modname[CCHMAXPATH] = {0};
35
36 if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
37 return NULL;
38 }
39 return(modname);
40}
41//******************************************************************************
42/*****************************************************************************
43 * Name : ULONG OSLibiGetModuleHandleA
44 * Purpose : replacement for IBM Open32's GetModuleHandle
45 * Parameters: LPCTSTR lpszModule
46 * Variables :
47 * Result : HMODULE hModule or NULLHANDLE in case of error
48 * Remark :
49 * Status : REWRITTEN UNTESTED
50 *
51 * Author : Patrick Haller [Sun, 1998/04/04 01:55]
52 *****************************************************************************/
53
54ULONG OSLibiGetModuleHandleA(char * pszModule)
55{
56 HMODULE hModule; /* module handle */
57 APIRET rc; /* API returncode */
58 static HMODULE hModuleExe = 0; /* "cached" hModuleExe */
59 PTIB pTIB; /* parameters for DosGetInfoBlocks */
60 PPIB pPIB;
61
62 _interrupt(3);
63 dprintf(("KERNEL32:GetModuleHandle(%x)\n",
64 pszModule));
65
66 /* @@@PH 98/04/04
67
68 this Open32 function is broken for pszModule == NULL
69 return(GetModuleHandle(pszModule));
70
71 Open32 always returns -1 here, however it should return the handle
72 of the current process. MFC30 crashes.
73
74 SvL, me thinks for PELDR support, you'll have to rewrite
75 this code anyway :)
76
77 */
78
79 if (NULL == pszModule) /* obtain handle to current executable */
80 {
81 if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
82 return (hModuleExe);
83
84 rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
85 &pPIB);
86 if (rc != NO_ERROR) /* check for errors */
87 {
88 return (NULLHANDLE); /* signal failure */
89 }
90
91 hModuleExe = pPIB->pib_hmte; /* set cached module */
92 hModule = pPIB->pib_hmte; /* module table entry ID */
93 }
94 else
95 {
96 rc = DosQueryModuleHandle(pszModule, /* query module handle */
97 &hModule);
98
99 if (rc != NO_ERROR) /* check for errors */
100 {
101 return (NULLHANDLE); /* signal failure */
102 }
103 }
104
105 return (hModule); /* return determined handle */
106}
107
108
109ULONG OSLibQueryModuleHandle(char *modname)
110{
111 HMODULE hModule;
112 APIRET rc;
113
114 rc = DosQueryModuleHandle(modname, /* query module handle */
115 &hModule);
116 if(rc)
117 return(-1);
118
119 return(hModule);
120}
121
122void OSLibWait(ULONG msec)
123{
124 DosSleep(msec);
125}
126
127//******************************************************************************
128//Wrapper for Dos16AllocSeg
129//******************************************************************************
130ULONG OSLibAllocSel(ULONG size, USHORT *selector)
131{
132 return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
133}
134//******************************************************************************
135//Wrapper for Dos16FreeSeg
136//******************************************************************************
137ULONG OSLibFreeSel(USHORT selector)
138{
139 return (Dos16FreeSeg(selector) == 0);
140}
141//******************************************************************************
142//Wrapper for Dos32SelToFlat
143//******************************************************************************
144PVOID OSLibSelToFlat(USHORT selector)
145{
146 return (PVOID)DosSelToFlat(selector << 16);
147}
148//******************************************************************************
149//Get TIB data
150//******************************************************************************
151ULONG OSLibGetTIB(int tiboff)
152{
153 PTIB ptib;
154 PPIB ppib;
155 APIRET rc;
156
157 rc = DosGetInfoBlocks(&ptib, &ppib);
158 if(rc) {
159 return 0;
160 }
161 switch(tiboff)
162 {
163 case TIB_STACKTOP:
164 return (ULONG)ptib->tib_pstacklimit;
165 case TIB_STACKLOW:
166 return (ULONG)ptib->tib_pstack;
167 default:
168 return 0;
169 }
170}
171//******************************************************************************
172//Get PIB data
173//******************************************************************************
174ULONG OSLibGetPIB(int piboff)
175{
176 PTIB ptib;
177 PPIB ppib;
178 APIRET rc;
179
180 rc = DosGetInfoBlocks(&ptib, &ppib);
181 if(rc) {
182 return 0;
183 }
184 switch(piboff)
185 {
186 case PIB_TASKHNDL:
187 return ppib->pib_hmte;
188 case PIB_TASKTYPE:
189 if(ppib->pib_ultype == 3) {
190 return TASKTYPE_PM;
191 }
192 else return TASKTYPE_VIO;
193 default:
194 return 0;
195 }
196}
197//******************************************************************************
198//Allocate local thread memory
199//******************************************************************************
200ULONG OSLibAllocThreadLocalMemory(int nrdwords)
201{
202 APIRET rc;
203 PULONG thrdaddr;
204
205 rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
206 if(rc) {
207 dprintf(("DosAllocThreadLocalMemory failed %d", rc));
208 return 0;
209 }
210 return (ULONG)thrdaddr;
211}
212//******************************************************************************
213//******************************************************************************
214char *OSLibStripPath(char *path)
215{
216 /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
217 char *pszFilename;
218
219 pszFilename = strrchr(path, '\\'); /* find rightmost slash */
220 if (pszFilename != NULL)
221 return (++pszFilename); /* return pointer to next character */
222
223 pszFilename = strrchr(path, '/'); /* find rightmost slash */
224 if (pszFilename != NULL)
225 return (++pszFilename); /* return pointer to next character */
226
227 return (path); /* default return value */
228}
229//******************************************************************************
230//******************************************************************************
231ULONG OSLibWinInitialize()
232{
233 return (ULONG)WinInitialize(0);
234}
235//******************************************************************************
236//******************************************************************************
237ULONG OSLibWinQueryMsgQueue(ULONG hab)
238{
239// ULONG hmq;
240
241// hmq = WinQueryWindowULong(HWND_DESKTOP, QWL_HMQ);
242 return (ULONG)WinCreateMsgQueue((HAB)hab, 0);
243}
244//******************************************************************************
245//******************************************************************************
Note: See TracBrowser for help on using the repository browser.