Ticket #238: exceptq.h

File exceptq.h, 5.6 KB (added by Yuri Dario, 13 years ago)
Line 
1/*****************************************************************************/
2/*
3 *  exceptq.h - exceptq public header
4 *
5 *  Parts of this file are
6 *    Copyright (c) 2000-2010 Steven Levine and Associates, Inc.
7 *    Copyright (c) 2010 Richard L Walsh
8 *
9*/
10/*****************************************************************************/
11
12#ifndef EXCEPTQ_H_INCLUDED
13  #define EXCEPTQ_H_INCLUDED
14
15#ifdef __cplusplus
16  extern "C" {
17#endif
18
19/*****************************************************************************/
20
21/** Install/Uninstall MyHandler() - 32-bit entrypoint **/
22
23#define APIENTRY16  _Far16 _Pascal
24#define PASCAL16    _Far16 _Pascal
25#define CDECL16     _Far16 _Cdecl
26typedef unsigned short APIRET16;
27
28APIRET   APIENTRY   InstallExceptq(PEXCEPTIONREGISTRATIONRECORD pExRegRec,
29                                   char * pszOptions);
30
31typedef APIRET APIENTRY _INSTEXQ(PEXCEPTIONREGISTRATIONRECORD, char*);
32typedef _INSTEXQ* PINSTEXQ;
33
34#define UninstallExceptq(pExRegRec) DosUnsetExceptionHandler((pExRegRec))
35
36/** The exception handler **/
37
38ULONG    APIENTRY   MYHANDLER(PEXCEPTIONREPORTRECORD pExRepRec,
39                              PEXCEPTIONREGISTRATIONRECORD pExRegRec,
40                              PCONTEXTRECORD pCtxRec,
41                              PVOID p);
42
43/** Install/Uninstall MyHandler() - 16-bit entrypoints **/
44
45APIRET16 APIENTRY16 SETEXCEPT(PEXCEPTIONREGISTRATIONRECORD _Seg16 pExRegRec);
46APIRET16 APIENTRY16 UNSETEXCEPT(PEXCEPTIONREGISTRATIONRECORD _Seg16 pExRegRec);
47
48/** Force the app to exit via a forced trap **/
49
50void     APIENTRY   FORCEEXIT();
51
52/*****************************************************************************/
53#ifdef INCL_LOADEXCEPTQ
54
55/* The following sample function loads and installs Exceptq dynamically
56 * so your application can use it without being dependent on its presence.
57 * By design, it will fail if it finds a version of Exceptq earlier than v7.0.
58 * You can either copy it into your source or you can #define INCL_LOADEXCEPTQ
59 * in *one* of your files to have it included.  It assumes you've #included
60 * <string.h> and #defined INCL_DOS.
61 *
62 * Note:  for each thread your app creates, you must call this function
63 * on entry and UninstallExceptq() on exit.  Typically, LoadExceptq()
64 * should be the first line of code in main() and in each threadproc.
65 * UninstallExceptq() should be called immediately before exiting main()
66 * and each threadproc.  To use the default options, pass a null string
67 * or a null pointer in pOpts.
68*/
69
70BOOL    LoadExceptq(EXCEPTIONREGISTRATIONRECORD* pExRegRec, char* pOpts)
71{
72  static BOOL      fLoadTried = FALSE;
73  static PINSTEXQ  pfnInstall = 0;
74
75  HMODULE   hmod = 0;
76  char      szFailName[16];
77
78  /* Make only one attempt to load the dll & resolve the proc address. */
79  if (!fLoadTried) {
80    fLoadTried = TRUE;
81
82    /* If the dll can't be found on the LIBPATH, look for it in the
83     * exe's directory (which may not be the current directory).
84     */
85    if (DosLoadModule(szFailName, sizeof(szFailName), "EXCEPTQ", &hmod)) {
86      PPIB      ppib;
87      PTIB      ptib;
88      char *    ptr;
89      char      szPath[CCHMAXPATH];
90
91      DosGetInfoBlocks(&ptib, &ppib);
92      if (DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH, szPath) ||
93          (ptr = strrchr(szPath, '\\')) == 0)
94        return FALSE;
95
96      strcpy(&ptr[1], "EXCEPTQ.DLL");
97      if (DosLoadModule(szFailName, sizeof(szFailName), szPath, &hmod))
98        return FALSE;
99    }
100
101    /* If the proc address isn't found (possibly because an older
102     * version of exceptq.dll was loaded), unload the dll & exit.
103     */
104    if (DosQueryProcAddr(hmod, 0, "InstallExceptq", (PFN*)&pfnInstall)) {
105      DosFreeModule(hmod);
106      return FALSE;
107    }
108  }
109
110  /* Ensure we have the proc address. */
111  if (!pfnInstall)
112    return FALSE;
113
114  /* Call InstallExceptq().  It really shouldn't fail, so if
115   * it does, zero-out the address to avoid further problems.
116   */
117  if (pfnInstall(pExRegRec, pOpts)) {
118    pfnInstall = 0;
119    return FALSE;
120  }
121
122  return TRUE;
123}
124
125BOOL    LoadExceptqAlways(EXCEPTIONREGISTRATIONRECORD* pExRegRec, char* pOpts)
126{
127  PINSTEXQ  pfnInstall = 0;
128
129  HMODULE   hmod = 0;
130  char      szFailName[16];
131
132    /* If the dll can't be found on the LIBPATH, look for it in the
133     * exe's directory (which may not be the current directory).
134     */
135    if (DosLoadModule(szFailName, sizeof(szFailName), "EXCEPTQ", &hmod)) {
136      PPIB      ppib;
137      PTIB      ptib;
138      char *    ptr;
139      char      szPath[CCHMAXPATH];
140
141      DosGetInfoBlocks(&ptib, &ppib);
142      if (DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH, szPath) ||
143          (ptr = strrchr(szPath, '\\')) == 0)
144        return FALSE;
145
146      strcpy(&ptr[1], "EXCEPTQ.DLL");
147      if (DosLoadModule(szFailName, sizeof(szFailName), szPath, &hmod))
148        return FALSE;
149    }
150
151    /* If the proc address isn't found (possibly because an older
152     * version of exceptq.dll was loaded), unload the dll & exit.
153     */
154    if (DosQueryProcAddr(hmod, 0, "InstallExceptq", (PFN*)&pfnInstall)) {
155      DosFreeModule(hmod);
156      return FALSE;
157    }
158
159  /* Ensure we have the proc address. */
160  if (!pfnInstall)
161    return FALSE;
162
163  /* Call InstallExceptq().  It really shouldn't fail, so if
164   * it does, zero-out the address to avoid further problems.
165   */
166  if (pfnInstall(pExRegRec, pOpts)) {
167    pfnInstall = 0;
168    return FALSE;
169  }
170
171  return TRUE;
172}
173
174#endif
175/*****************************************************************************/
176
177#ifdef __cplusplus
178  }
179#endif
180
181#endif /* EXCEPTQ_H_INCLUDED */
182