-
Notifications
You must be signed in to change notification settings - Fork 175
/
pdx.py
77 lines (62 loc) · 2.77 KB
/
pdx.py
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
#
# PyDBG
# Copyright (C) 2006 Pedram Amini <[email protected]>
#
# $Id: pdx.py 238 2010-04-05 20:40:46Z rgovostes $
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
'''
@author: Pedram Amini
@license: GNU General Public License 2.0 or later
@contact: [email protected]
@organization: www.openrce.org
'''
import os.path
from my_ctypes import *
from defines import *
# macos compatability.
try:
kernel32 = windll.kernel32
except:
kernel32 = CDLL(os.path.join(os.path.dirname(__file__), "libmacdll.dylib"))
class pdx (Exception):
'''
This class is used internally for raising custom exceptions and includes support for automated Windows error message
resolution and formatting. For example, to raise a generic error you can use::
raise pdx("Badness occured.")
To raise a Windows API error you can use::
raise pdx("SomeWindowsApi()", True)
'''
message = None
error_code = None
####################################################################################################################
def __init__ (self, message, win32_exception=False):
'''
'''
self.message = message
self.error_code = None
if win32_exception:
self.error_msg = c_char_p()
self.error_code = kernel32.GetLastError()
kernel32.FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
None,
self.error_code,
0x00000400, # MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
byref(self.error_msg),
0,
None)
####################################################################################################################
def __str__ (self):
if self.error_code != None:
return "[%d] %s: %s" % (self.error_code, self.message, self.error_msg.value)
else:
return self.message