1 #############################################################################
3 # Copyright 2008 Jose Fonseca
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #############################################################################
24 SHORT = Intrinsic("SHORT", "%i")
25 USHORT = Intrinsic("USHORT", "%u")
26 INT = Intrinsic("INT", "%i")
27 UINT = Intrinsic("UINT", "%u")
28 LONG = Intrinsic("LONG", "%li")
29 ULONG = Intrinsic("ULONG", "%lu")
30 FLOAT = Intrinsic("FLOAT", "%f")
32 INT32 = Intrinsic("INT32", "%i")
33 UINT32 = Intrinsic("UINT32", "%i")
35 BYTE = Intrinsic("BYTE", "0x%02lx")
36 WORD = Intrinsic("WORD", "0x%04lx")
37 DWORD = Intrinsic("DWORD", "0x%08lx")
39 BOOL = Intrinsic("BOOL", "%i")
41 LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%llx")
43 HRESULT = Alias("HRESULT", Int)
45 PVOID = Intrinsic("PVOID", "%p")
46 HANDLE = Intrinsic("HANDLE", "%p")
47 HWND = Intrinsic("HWND", "%p")
48 HDC = Intrinsic("HDC", "%p")
49 HMONITOR = Intrinsic("HMONITOR", "%p")
51 REFIID = Alias("REFIID", PVOID)
52 GUID = Alias("GUID", PVOID)
53 LUID = Alias("LUID", PVOID)
55 POINT = Struct("POINT", (
60 RECT = Struct("RECT", (
67 PALETTEENTRY = Struct("PALETTEENTRY", (
74 RGNDATA = Struct("RGNDATA", ())
75 REFGUID = Alias("REFGUID", PVOID)
78 IUnknown = Interface("IUnknown")
81 Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))),
82 Method(ULONG, "AddRef", ()),
83 Method(ULONG, "Release", ()),
89 def __init__(self, name):
92 if self not in towrap:
96 return "Wrap" + self.name
98 def wrap_pre_decl(self):
102 print 'static HINSTANCE g_hDll = NULL;'
103 print 'Log * g_pLog = NULL;'
104 print 'static TCHAR g_szDll[MAX_PATH] = {0};'
106 print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);'
110 print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {'
111 #print r' Log * pLog;'
112 print r' switch(fdwReason) {'
113 print r' case DLL_PROCESS_ATTACH:'
114 print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))'
115 print r' return FALSE;'
116 print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name
117 #print r' if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)'
118 #print r' return FALSE;'
119 #print r' if(!g_pLog)'
120 #print r' g_pLog = new Log(TEXT("%s"));' % self.name
121 print r' case DLL_THREAD_ATTACH:'
122 #print r' pLog = new Log(TEXT("%s"));' % self.name
123 #print r' TlsSetValue(dwTlsIndex, pLog);'
124 print r' return TRUE;'
125 print r' case DLL_THREAD_DETACH:'
126 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
127 #print r' if (pLog != NULL)'
128 #print r' delete pLog;'
129 print r' return TRUE;'
130 print r' case DLL_PROCESS_DETACH:'
131 #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);'
132 #print r' if (pLog != NULL)'
133 #print r' delete pLog;'
134 #print r' TlsFree(dwTlsIndex);'
135 print r' if(g_pLog) {'
136 print r' delete g_pLog;'
137 print r' g_pLog = NULL;'
139 print r' if(g_hDll) {'
140 print r' FreeLibrary(g_hDll);'
141 print r' g_hDll = NULL;'
143 print r' return TRUE;'
145 print r' (void)hinstDLL;'
146 print r' (void)lpvReserved;'
147 print r' return TRUE;'
150 for function in self.functions:
151 type = 'P' + function.name
152 print function.prototype() + ' {'
155 print ' delete g_pLog;'
156 print ' g_pLog = new Log(TEXT("%s"));' % self.name
157 #print ' g_pLog->ReOpen();'
158 print ' typedef ' + function.prototype('* %s' % type) + ';'
159 print ' %s pFunction;' % type
160 if function.type is Void:
163 print ' %s result;' % function.type
165 print ' if(!g_hDll) {'
166 print ' g_hDll = LoadLibrary(g_szDll);'
168 print ' ExitProcess(0);'
170 print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name)
171 print ' if(!pFunction)'
172 print ' ExitProcess(0);'
173 print ' g_pLog->BeginCall("%s");' % (function.name)
174 print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args]))
175 print ' g_pLog->EndCall();'
176 for type, name in function.args:
178 type.wrap_instance(name)
179 if function.type is not Void:
180 function.type.wrap_instance('result')
181 if function.type is not Void:
182 print ' return result;'