Test this code, please

This works on mingw-G++
Can someone test this on Microsoft compiler?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

#include <windows.h>
#include <excpt.h>

/* NOTE: these macros aren't mine */

#ifdef __GNUC__   // GCC-mingw only
#undef __try1
#undef __except1
//
// install exception handler 
//
#define __try1(pHandler) \
	__asm__ ("pushl %0;pushl %%fs:0;movl %%esp,%%fs:0;" : : "g" (pHandler));

//
// Remnove exception handler
//
#define	__except1	\
	__asm__ ("movl (%%esp),%%eax;movl %%eax,%%fs:0;addl $8,%%esp;" \
	 : : : "%eax");

#else  // FOR MS COMPILER ONLY
//
// install exception handler 
//
#define __try1(pHandler)                                                    \
    __asm                                                                   \
    {                           /* Build EXCEPTION_REGISTRATION record: */  \
        push    pHandler        /* Address of handler function          */  \
        push    FS:[0]          /* Address of previous handler          */  \
        mov     FS:[0],ESP      /* Install new EXECEPTION_REGISTRATION  */  \
    }




//
// Remnove exception handler
//
#define __except1 \
    __asm                                                                            \
    {                                                                                \
        mov     eax,[ESP]                                                            \
        mov     FS:[0], EAX     /* Install previous record*/                         \
        add     esp, 8          /* Clean our EXECEPTION_REGISTRATION off stack*/     \
    }


#endif

/* The exception handler */
EXCEPTION_DISPOSITION _exception_filter(_EXCEPTION_RECORD* ed,void *v1,_CONTEXT *cont,void* v2) 
{                                  
/// RaiseException()                   
switch(ed->ExceptionCode)
{
                 
case EXCEPTION_ACCESS_VIOLATION:
                          MessageBox(0,"Exception: Access vioalation","Exception",16);
                          break;  
case EXCEPTION_BREAKPOINT:
                          MessageBox(0,"Exception: Breakpoint","Exception",16);  
                          break;  
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
                          MessageBox(0,"Exception: Array index out of bounds","Exception",16); 
                          break;
case EXCEPTION_STACK_OVERFLOW:
                          MessageBox(0,"Exception: Stack overflow","Exception",16);
                          break;                          
case EXCEPTION_PRIV_INSTRUCTION:
                          MessageBox(0,"Exception: General Protection Fault","Exception",16); 
                          break;                           
case EXCEPTION_ILLEGAL_INSTRUCTION:
                          MessageBox(0,"Exception: Illegal instruction in program","Exception",16); 
                          break;
case EXCEPTION_INT_OVERFLOW:
                          MessageBox(0,"Exception: Integer overflow","Exception",16);
                          break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
                          MessageBox(0,"Exception: Integer division by zero","Exception",16);
                          break;
case EXCEPTION_FLT_UNDERFLOW :
                          MessageBox(0,"Exception: Floating point value underflow","Exception",16);
                          break;                          
default:       MessageBox(0,"Unknown exception","Exception",16);                     
}    

                                                                                            
exit(666);   // It's better to use ExitProcess()    
                                                                
}
/* Declare exception registration struct */
EXCEPTION_REGISTRATION *er;


/* This macro initializes SEH */
#define init_seh()                              \
{                                               \
    er=new EXCEPTION_REGISTRATION;              \
    er->handler=_exception_filter;              \
    __try1(_exception_filter);                  \
}

/* This stops SEH */
#define stop_seh(void)                          \
{                                               \
__except1;                                      \
}     



int main()
{
init_seh()

/* let's crash our program */
int a=a/0;
//short *ex=(short*)0xb800000;
//ex[0]='a';



stop_seh();
}
nope
VC++ says
main.cpp(93) : error C2143: syntax error : missing ';' before '*'
main.cpp(93) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(93) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(114) : error C2061: syntax error : identifier 'EXCEPTION_REGISTRATION'
main.cpp(114) : error C2227: left of '->handler' must point to class/struct/union/generic type
        type is 'int *'
main.cpp(114) : warning C4405: 'push' : identifier is reserved word
main.cpp(114) : error C2400: inline assembler syntax error in 'first operand'; found 'register'
main.cpp(114) : warning C4405: 'mov' : identifier is reserved word
main.cpp(123) : warning C4003: not enough actual parameters for macro 'stop_seh'
main.cpp(123) : warning C4405: 'mov' : identifier is reserved word
main.cpp(123) : error C2400: inline assembler syntax error in 'second operand'; found 'register'
main.cpp(123) : warning C4405: 'add' : identifier is reserved word


edit: I've missed the first line so add 1 to every line number in the output
Last edited on
***Updated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <windows.h>
#include <excpt.h>

/* NOTE: these macros aren't mine */


#ifdef __GNUC__   // GCC-mingw only

  #undef __try1
  #undef __except1
//
// install exception handler (NOTE: these macros are also defined in excpt.h)
//
  #define __try1(pHandler) \
	  __asm__ ("pushl %0;pushl %%fs:0;movl %%esp,%%fs:0;" : : "g" (pHandler));

//
// Remnove exception handler
//
  #define	__except1	\
  	  __asm__ ("movl (%%esp),%%eax;movl %%eax,%%fs:0;addl $8,%%esp;" \
	   : : : "%eax");

#else  // FOR MS COMPILER ONLY


//
// install exception handler 
//
#define __try1(pHandler)                                                    \
    __asm                                                                   \
    {                           /* Build EXCEPTION_REGISTRATION record: */  \
        push    pHandler        /* Address of handler function          */  \
        push    FS:[0]          /* Address of previous handler          */  \
        mov     FS:[0],ESP      /* Install new EXECEPTION_REGISTRATION  */  \
    }


//
// Remnove exception handler
//
#define __except1 \
    __asm                                                                            \
    {                                                                                \
        mov     eax,[ESP]                                                            \
        mov     FS:[0], EAX     /* Install previous record*/                         \
        add     esp, 8          /* Clean our EXECEPTION_REGISTRATION off stack*/     \
    }


#endif

/* The exception handler */
EXCEPTION_DISPOSITION _exception_filter(_EXCEPTION_RECORD* ed,void *v1,_CONTEXT *cont,void* v2) 
{                                  
/// RaiseException()                   
switch(ed->ExceptionCode)
{
                 
case EXCEPTION_ACCESS_VIOLATION:
                          MessageBox(0,"Exception: Access vioalation","Exception",16);
                          break;  
case EXCEPTION_BREAKPOINT:
                          MessageBox(0,"Exception: Breakpoint","Exception",16);  
                          break;  
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
                          MessageBox(0,"Exception: Array index out of bounds","Exception",16); 
                          break;
case EXCEPTION_STACK_OVERFLOW:
                          MessageBox(0,"Exception: Stack overflow","Exception",16);
                          break;                          
case EXCEPTION_PRIV_INSTRUCTION:
                          MessageBox(0,"Exception: General Protection Fault","Exception",16); 
                          break;                           
case EXCEPTION_ILLEGAL_INSTRUCTION:
                          MessageBox(0,"Exception: Illegal instruction in program","Exception",16); 
                          break;
case EXCEPTION_INT_OVERFLOW:
                          MessageBox(0,"Exception: Integer overflow","Exception",16);
                          break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
                          MessageBox(0,"Exception: Integer division by zero","Exception",16);
                          break;
case EXCEPTION_FLT_UNDERFLOW :
                          MessageBox(0,"Exception: Floating point value underflow","Exception",16);
                          break;                          
default:       MessageBox(0,"Unknown exception","Exception",16);                     
}    

                                                                                            
exit(666);   // It's better to use ExitProcess()    
                                                                
}
/* Declare exception registration struct */



/* This macro initializes SEH (****updated)*/
#define init_seh(void)                          \
{                                               \
    __try1(_exception_filter);                  \
}

/* This stops SEH */
#define stop_seh(void)                          \
{                                               \
__except1;                                      \
}     



int main()
{
init_seh();

/* let's crash our program */
int a=a/0;
//short *ex=(short*)0xb800000;
//ex[0]='a';



stop_seh();
}

1>d:\prog\_c\workspace_vis\null\null\main.cpp(114) : warning C4003: not enough actual parameters for macro 'init_seh'
1>d:\prog\_c\workspace_vis\null\null\main.cpp(114) : warning C4405: 'push' : identifier is reserved word
1>d:\prog\_c\workspace_vis\null\null\main.cpp(114) : error C2400: inline assembler syntax error in 'first operand'; found 'register'
1>d:\prog\_c\workspace_vis\null\null\main.cpp(114) : warning C4405: 'mov' : identifier is reserved word
1>d:\prog\_c\workspace_vis\null\null\main.cpp(123) : warning C4003: not enough actual parameters for macro 'stop_seh'
1>d:\prog\_c\workspace_vis\null\null\main.cpp(123) : warning C4405: 'mov' : identifier is reserved word
1>d:\prog\_c\workspace_vis\null\null\main.cpp(123) : error C2400: inline assembler syntax error in 'second operand'; found 'register'
1>d:\prog\_c\workspace_vis\null\null\main.cpp(123) : warning C4405: 'add' : identifier is reserved word
VC++:

1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(114) : warning C4003: not enough actual parameters for macro 'init_seh'
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(114) : warning C4405: 'push' : identifier is reserved word
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(114) : error C2400: inline assembler syntax error in 'first operand'; found 'register'
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(114) : warning C4405: 'mov' : identifier is reserved word
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(123) : warning C4003: not enough actual parameters for macro 'stop_seh'
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(123) : warning C4405: 'mov' : identifier is reserved word
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(123) : error C2400: inline assembler syntax error in 'second operand'; found 'register'
1>c:\users\tyler\documents\visual studio 2008\projects\terminal\terminal\main.cpp(123) : warning C4405: 'add' : identifier is reserved word

Basically the same as R0mai.
As people were already testing this in VC++ I though I would try Dev. The program runs but throws an exception at line 117: "Integer division by Zero".

EDIT: Just realised you OP sorry; "This works on mingw-G++"
Last edited on
Damn it. I'll better try VEH.
Topic archived. No new replies allowed.