blob: 8e8adee903dc651178ca7f6d8d89f653760952e9 [file] [log] [blame]
José Fonsecad626cf42008-07-07 07:43:16 +09001"""windows.h"""
2
3from base import *
4
5INT = Intrinsic("INT", "%u")
6UINT = Intrinsic("UINT", "%u")
7LONG = Intrinsic("LONG", "%li")
8ULONG = Intrinsic("ULONG", "%lu")
9
10BYTE = Intrinsic("BYTE", "0x%02lu")
11WORD = Intrinsic("WORD", "0x%04lu")
12DWORD = Intrinsic("DWORD", "0x%08lu")
13
14BOOL = Intrinsic("BOOL", "%i")
15
16LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%x")
17
18HRESULT = Alias("HRESULT", Int)
19
20PVOID = Intrinsic("PVOID", "%p")
21HWND = Intrinsic("HWND", "%p")
22HMONITOR = Intrinsic("HMONITOR", "%p")
23
24REFIID = Alias("REFIID", PVOID)
25GUID = Alias("GUID", PVOID)
26
27POINT = Struct("POINT", (
28 (LONG, "x"),
29 (LONG, "y"),
30))
31
32RECT = Struct("RECT", (
33 (LONG, "left"),
34 (LONG, "top"),
35 (LONG, "right"),
36 (LONG, "bottom"),
37))
38
39PALETTEENTRY = Struct("PALETTEENTRY", (
40 (BYTE, "peRed"),
41 (BYTE, "peGreen"),
42 (BYTE, "peBlue"),
43 (BYTE, "peFlags"),
44))
45
46RGNDATA = Struct("RGNDATA", ())
47REFGUID = Alias("REFGUID", PVOID)
48
49
50IUnknown = Interface("IUnknown")
51
52IUnknown.methods = (
53 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
54 Method(ULONG, "AddRef", ()),
55 Method(ULONG, "Release", ()),
56)
57
58
59class Dll:
60
61 def __init__(self, name):
62 self.name = name
63 self.functions = []
64 if self not in towrap:
65 towrap.append(self)
66
67 def wrap_name(self):
68 return "Wrap" + self.name
69
70 def wrap_pre_decl(self):
71 pass
72
73 def wrap_decl(self):
José Fonsecad626cf42008-07-07 07:43:16 +090074 print 'static HINSTANCE g_hDll = NULL;'
75 print 'static Log * g_pLog = NULL;'
José Fonsecad7605892008-07-07 13:09:31 +090076 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
José Fonsecad626cf42008-07-07 07:43:16 +090077 print
78 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
79 print
80
81 def wrap_impl(self):
82 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
José Fonsecad7605892008-07-07 13:09:31 +090083 #print r' Log * pLog;'
José Fonsecad626cf42008-07-07 07:43:16 +090084 print r' switch(fdwReason) {'
85 print r' case DLL_PROCESS_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +090086 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
José Fonsecad626cf42008-07-07 07:43:16 +090087 print r' return FALSE;'
José Fonsecad7605892008-07-07 13:09:31 +090088 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
89 #print r' if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)'
90 #print r' return FALSE;'
91 #print r' if(!g_pLog)'
92 #print r' g_pLog = new Log(TEXT("%s"));' % self.name
José Fonsecad626cf42008-07-07 07:43:16 +090093 print r' case DLL_THREAD_ATTACH:'
José Fonsecad7605892008-07-07 13:09:31 +090094 #print r' pLog = new Log(TEXT("%s"));' % self.name
95 #print r' TlsSetValue(dwTlsIndex, pLog);'
José Fonsecad626cf42008-07-07 07:43:16 +090096 print r' return TRUE;'
97 print r' case DLL_THREAD_DETACH:'
José Fonsecad7605892008-07-07 13:09:31 +090098 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
99 #print r' if (pLog != NULL)'
100 #print r' delete pLog;'
101 print r' return TRUE;'
102 print r' case DLL_PROCESS_DETACH:'
103 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
104 #print r' if (pLog != NULL)'
105 #print r' delete pLog;'
106 #print r' TlsFree(dwTlsIndex);'
107 print r' if(g_pLog) {'
108 print r' delete g_pLog;'
109 print r' g_pLog = NULL;'
110 print r' }'
111 print r' if(g_hDll) {'
112 print r' FreeLibrary(g_hDll);'
113 print r' g_hDll = NULL;'
114 print r' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900115 print r' return TRUE;'
116 print r' }'
José Fonsecad7605892008-07-07 13:09:31 +0900117 print r' (void)hinstDLL;'
José Fonsecad626cf42008-07-07 07:43:16 +0900118 print r' (void)lpvReserved;'
119 print r' return TRUE;'
120 print r'}'
121 print
122 for function in self.functions:
123 type = 'P' + function.name
124 print function.prototype() + ' {'
José Fonsecad7605892008-07-07 13:09:31 +0900125 if 1:
126 print ' if(g_pLog)'
127 print ' delete g_pLog;'
128 print ' g_pLog = new Log(TEXT("%s"));' % self.name
129 #print ' g_pLog->ReOpen();'
José Fonsecad626cf42008-07-07 07:43:16 +0900130 print ' typedef ' + function.prototype('* %s' % type) + ';'
131 print ' %s pFunction;' % type
132 if function.type is Void:
133 result = ''
134 else:
135 print ' %s result;' % function.type
136 result = 'result = '
José Fonsecad7605892008-07-07 13:09:31 +0900137 print ' if(!g_hDll) {'
138 print ' g_hDll = LoadLibrary(g_szDll);'
139 print ' if(!g_hDll)'
140 print ' ExitProcess(0);'
141 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900142 print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
143 print ' if(!pFunction)'
144 print ' ExitProcess(0);'
145 print ' g_pLog->BeginCall("%s");' % (function.name)
146 print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
José Fonsecad7605892008-07-07 13:09:31 +0900147 print ' g_pLog->EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900148 for type, name in function.args:
149 if type.isoutput():
150 type.wrap_instance(name)
151 if function.type is not Void:
152 function.type.wrap_instance('result')
José Fonsecad626cf42008-07-07 07:43:16 +0900153 if function.type is not Void:
154 print ' return result;'
155 print '}'
156 print
157 print
158