José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame^] | 1 | """windows.h""" |
| 2 | |
| 3 | from base import * |
| 4 | |
| 5 | INT = Intrinsic("INT", "%u") |
| 6 | UINT = Intrinsic("UINT", "%u") |
| 7 | LONG = Intrinsic("LONG", "%li") |
| 8 | ULONG = Intrinsic("ULONG", "%lu") |
| 9 | |
| 10 | BYTE = Intrinsic("BYTE", "0x%02lu") |
| 11 | WORD = Intrinsic("WORD", "0x%04lu") |
| 12 | DWORD = Intrinsic("DWORD", "0x%08lu") |
| 13 | |
| 14 | BOOL = Intrinsic("BOOL", "%i") |
| 15 | |
| 16 | LARGE_INTEGER = Intrinsic("LARGE_INTEGER", "0x%x") |
| 17 | |
| 18 | HRESULT = Alias("HRESULT", Int) |
| 19 | |
| 20 | PVOID = Intrinsic("PVOID", "%p") |
| 21 | HWND = Intrinsic("HWND", "%p") |
| 22 | HMONITOR = Intrinsic("HMONITOR", "%p") |
| 23 | |
| 24 | REFIID = Alias("REFIID", PVOID) |
| 25 | GUID = Alias("GUID", PVOID) |
| 26 | |
| 27 | POINT = Struct("POINT", ( |
| 28 | (LONG, "x"), |
| 29 | (LONG, "y"), |
| 30 | )) |
| 31 | |
| 32 | RECT = Struct("RECT", ( |
| 33 | (LONG, "left"), |
| 34 | (LONG, "top"), |
| 35 | (LONG, "right"), |
| 36 | (LONG, "bottom"), |
| 37 | )) |
| 38 | |
| 39 | PALETTEENTRY = Struct("PALETTEENTRY", ( |
| 40 | (BYTE, "peRed"), |
| 41 | (BYTE, "peGreen"), |
| 42 | (BYTE, "peBlue"), |
| 43 | (BYTE, "peFlags"), |
| 44 | )) |
| 45 | |
| 46 | RGNDATA = Struct("RGNDATA", ()) |
| 47 | REFGUID = Alias("REFGUID", PVOID) |
| 48 | |
| 49 | |
| 50 | IUnknown = Interface("IUnknown") |
| 51 | |
| 52 | IUnknown.methods = ( |
| 53 | Method(HRESULT, "QueryInterface", ((REFIID, "riid"), (Pointer(Pointer(Void)), "ppvObj"))), |
| 54 | Method(ULONG, "AddRef", ()), |
| 55 | Method(ULONG, "Release", ()), |
| 56 | ) |
| 57 | |
| 58 | |
| 59 | class 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): |
| 74 | print '#pragma data_seg (".%s_shared")' % self.name |
| 75 | print 'static HINSTANCE g_hDll = NULL;' |
| 76 | print 'static Log * g_pLog = NULL;' |
| 77 | print '#pragma data_seg ()' |
| 78 | print |
| 79 | print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);' |
| 80 | print |
| 81 | |
| 82 | def wrap_impl(self): |
| 83 | print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {' |
| 84 | print r' TCHAR fileName[MAX_PATH];' |
| 85 | print r' switch(fdwReason) {' |
| 86 | print r' case DLL_PROCESS_ATTACH:' |
| 87 | print r' if(!GetSystemDirectory(fileName, MAX_PATH))' |
| 88 | print r' return FALSE;' |
| 89 | print r' _tcscat(fileName, TEXT("\\%s.dll"));' % self.name |
| 90 | print r' if(!g_hDll)' |
| 91 | print r' g_hDll = LoadLibrary(fileName);' |
| 92 | print r' if(!g_hDll)' |
| 93 | print r' return FALSE;' |
| 94 | print r' if(!g_pLog)' |
| 95 | print r' g_pLog = new Log("%s");' % self.name |
| 96 | print r' return TRUE;' |
| 97 | print r' case DLL_PROCESS_DETACH:' |
| 98 | print r' if(g_pLog)' |
| 99 | print r' delete g_pLog;' |
| 100 | print r' if(g_hDll)' |
| 101 | print r' FreeLibrary(g_hDll);' |
| 102 | print r' return TRUE;' |
| 103 | print r' case DLL_THREAD_ATTACH:' |
| 104 | print r' return TRUE;' |
| 105 | print r' case DLL_THREAD_DETACH:' |
| 106 | print r' return TRUE;' |
| 107 | print r' }' |
| 108 | print r' (void)lpvReserved;' |
| 109 | print r' return TRUE;' |
| 110 | print r'}' |
| 111 | print |
| 112 | for function in self.functions: |
| 113 | type = 'P' + function.name |
| 114 | print function.prototype() + ' {' |
| 115 | print ' typedef ' + function.prototype('* %s' % type) + ';' |
| 116 | print ' %s pFunction;' % type |
| 117 | if function.type is Void: |
| 118 | result = '' |
| 119 | else: |
| 120 | print ' %s result;' % function.type |
| 121 | result = 'result = ' |
| 122 | print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name) |
| 123 | print ' if(!pFunction)' |
| 124 | print ' ExitProcess(0);' |
| 125 | print ' g_pLog->BeginCall("%s");' % (function.name) |
| 126 | print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args])) |
| 127 | for type, name in function.args: |
| 128 | if type.isoutput(): |
| 129 | type.wrap_instance(name) |
| 130 | if function.type is not Void: |
| 131 | function.type.wrap_instance('result') |
| 132 | print ' g_pLog->EndCall();' |
| 133 | if function.type is not Void: |
| 134 | print ' return result;' |
| 135 | print '}' |
| 136 | print |
| 137 | print |
| 138 | |