Check-in [57ea797fa4]

Not logged in
Overview
SHA1 Hash:57ea797fa44d1f8c1fe8c1dd639aeffc744bc73c
Date: 2015-05-05 06:54:57
User: kinaba
Comment:Correct fix for std file handle closing issue.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes
hide diffs unified diffs patch

Modified src/dllmain.d from [9533046d0b42b4c6] to [1df1b32171114527].

1 import core.sys.windows.dll; 1 import core.sys.windows.dll; 2 import core.sys.windows.windows; 2 import core.sys.windows.windows; > 3 import std.stdio; 3 4 4 //---------------------------------------------------------------- 5 //---------------------------------------------------------------- 5 // おきまりのDLL初期化ルーチン 6 // おきまりのDLL初期化ルーチン 6 //---------------------------------------------------------------- 7 //---------------------------------------------------------------- 7 8 8 extern (Windows) 9 extern (Windows) 9 BOOL DllMain( HINSTANCE inst, ULONG reason, void* reserved ) 10 BOOL DllMain( HINSTANCE inst, ULONG reason, void* reserved ) ................................................................................................................................................................................ 11 switch( reason ) 12 switch( reason ) 12 { 13 { 13 case DLL_PROCESS_ATTACH: 14 case DLL_PROCESS_ATTACH: 14 dll_process_attach( inst, true ); 15 dll_process_attach( inst, true ); 15 break; 16 break; 16 17 17 case DLL_PROCESS_DETACH: 18 case DLL_PROCESS_DETACH: > 19 _fcloseallp = null; // Do not close stdin/out/errs!!! 18 dll_process_detach( inst, true ); 20 dll_process_detach( inst, true ); 19 break; 21 break; 20 22 21 case DLL_THREAD_ATTACH: 23 case DLL_THREAD_ATTACH: 22 dll_thread_attach( true, true ); 24 dll_thread_attach( true, true ); 23 break; 25 break; 24 26 ................................................................................................................................................................................ 26 dll_thread_detach( true, true ); 28 dll_thread_detach( true, true ); 27 break; 29 break; 28 30 29 default: 31 default: 30 } 32 } 31 return true; 33 return true; 32 } 34 } 33 < 34 /* < 35 // Hack! < 36 // < 37 // _acrtused_dllが勝手に終了時に標準入出力ハンドルを < 38 // 閉じちゃって困るので、その辺りだけは閉じないような < 39 // CloseHandleを突っ込んで回避。 < 40 // < 41 // dmd v0.99 では動いていたのだけど最近のだとこれを入れる < 42 // 至る所でクラッシュするので考え直す必要がある。 < 43 // TODO: bug report. < 44 < 45 extern(Windows) < 46 { < 47 private alias BOOL function(HANDLE) ClHnT; < 48 private ClHnT Real_CloseHandle; < 49 private HANDLE stdin; < 50 private HANDLE stdout; < 51 private HANDLE stderr; < 52 < 53 BOOL CloseHandle( HANDLE h ) < 54 { < 55 if( h==stdin ) return TRUE; < 56 if( h==stdout ) return TRUE; < 57 if( h==stderr ) return TRUE; < 58 return Real_CloseHandle(h); < 59 } < 60 } < 61 < 62 static this() < 63 { < 64 Real_CloseHandle = cast(ClHnT) < 65 GetProcAddress( GetModuleHandleA("kernel32.dll"), "CloseHandle" < 66 stdin = GetStdHandle(STD_INPUT_HANDLE ); < 67 stdout = GetStdHandle(STD_OUTPUT_HANDLE); < 68 stderr = GetStdHandle(STD_ERROR_HANDLE ); < 69 } < 70 */ <

Modified src/testexe.d from [cc2ab2b91c87057b] to [e4521a091f44ac97].

3 import std.string; 3 import std.string; 4 import core.runtime; 4 import core.runtime; 5 5 6 alias extern(Windows) int function( HWND a, immutable char* b, char* c, DWORD d 6 alias extern(Windows) int function( HWND a, immutable char* b, char* c, DWORD d 7 7 8 void main() 8 void main() 9 { 9 { 10 HINSTANCE h = cast(HINSTANCE) Runtime.loadLibrary("QBga32.DLL"); < 11 for(;;) { 10 for(;;) { 12 write("> "); 11 write("> "); 13 string s = readln(); 12 string s = readln(); 14 if(s.length > 0) { 13 if(s.length > 0) { > 14 HINSTANCE h = cast(HINSTANCE) Runtime.loadLibrary("QBga3 15 CmdFuncT Bga = cast(CmdFuncT)GetProcAddress(h, "Bga"); 15 CmdFuncT Bga = cast(CmdFuncT)GetProcAddress(h, "Bga"); 16 int r = Bga(NULL, s.toStringz(), null, 0); 16 int r = Bga(NULL, s.toStringz(), null, 0); > 17 Runtime.unloadLibrary(h); 17 writeln("Return: ", r); 18 writeln("Return: ", r); 18 } 19 } 19 } 20 } 20 Runtime.unloadLibrary(h); < 21 } 21 }