Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

shared.pas 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {*
  2. * Shared methods / classes / functions between Windows programs
  3. *
  4. * This application launches DMDirc on windows and passes control to the
  5. * update engine as necessary.
  6. *
  7. * DMDirc - Open Source IRC Client
  8. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
  9. * Michael Nixon
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. *}
  29. unit shared;
  30. interface
  31. uses Windows, SysUtils, Vista;
  32. function nicesize(dsize: extended): string;
  33. function askQuestion(Question: String; Title: string): boolean;
  34. procedure showmessage(message: String; Title: string; context:String = 'Information');
  35. procedure showError(ErrorMessage: String; Title: string; addFooter: boolean = true; includeDescInXP: boolean = true);
  36. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  37. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  38. procedure RunProgram(sProgramToRun: String; wait: boolean);
  39. function RunJava(arguments: String): Longword;
  40. { ---------------------------------------------------------------------------- }
  41. implementation
  42. { ----------------------------------------------------------------------------
  43. Takes a size, <dsize> in bytes, and converts it a human readable string with
  44. a suffix (MB or GB).
  45. ---------------------------------------------------------------------------- }
  46. function nicesize(dsize: extended): string;
  47. var
  48. kbytes: single;
  49. mbytes: single;
  50. gbytes: single;
  51. begin
  52. kbytes := dsize / 1024;
  53. mbytes := kbytes / 1024;
  54. gbytes := mbytes / 1024;
  55. if kbytes < 1024 then begin
  56. result := FloatToStrF(kbytes, ffFixed, 10, 2) + ' kB';
  57. exit;
  58. end;
  59. if mbytes < 1024 then begin
  60. result := FloatToStrF(mbytes, ffFixed, 10, 2) + ' MB';
  61. exit;
  62. end;
  63. result := FloatToStrF(gbytes, ffFixed, 10, 2) + ' GB';
  64. exit;
  65. end;
  66. { ----------------------------------------------------------------------------
  67. Ask a question and return True for YES and False for NO
  68. Uses nifty vista task dialog if available
  69. ---------------------------------------------------------------------------- }
  70. function askQuestion(Question: String; Title: string): boolean;
  71. begin
  72. Result := TaskDialog(0, Title, 'Question', Question, TD_ICON_QUESTION, TD_BUTTON_YES + TD_BUTTON_NO) = mrYes;
  73. end;
  74. { ----------------------------------------------------------------------------
  75. Show a message box (information)
  76. Uses nifty vista task dialog if available
  77. ---------------------------------------------------------------------------- }
  78. procedure showmessage(message: String; Title: string; context:String = 'Information');
  79. begin
  80. TaskDialog(0, Title, context, message, TD_ICON_INFORMATION, TD_BUTTON_OK);
  81. end;
  82. { ----------------------------------------------------------------------------
  83. Show an error message
  84. Uses nifty vista task dialog if available
  85. ---------------------------------------------------------------------------- }
  86. procedure showError(ErrorMessage: String; Title: string; addFooter: boolean = true; includeDescInXP: boolean = true);
  87. begin
  88. if addFooter then begin
  89. ErrorMessage := ErrorMessage+#13#10;
  90. ErrorMessage := ErrorMessage+#13#10+'If you feel this is incorrect, or you require some further assistance,';
  91. if not IsWindowsVista then ErrorMessage := ErrorMessage+#13#10;
  92. ErrorMessage := ErrorMessage+'please feel free to contact us.';
  93. end;
  94. TaskDialog(0, Title, 'Sorry, ' + Title + ' is unable to continue.', ErrorMessage, TD_ICON_ERROR, TD_BUTTON_OK, includeDescInXP, false);
  95. end;
  96. { ----------------------------------------------------------------------------
  97. Launch a process (hidden if requested) and immediately return control to
  98. the current thread
  99. ---------------------------------------------------------------------------- }
  100. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  101. var
  102. StartupInfo: TStartupInfo;
  103. begin
  104. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  105. with StartupInfo do begin
  106. cb := SizeOf(TStartupInfo);
  107. dwFlags := STARTF_USESHOWWINDOW;
  108. if hide then wShowWindow := SW_HIDE
  109. else wShowWindow := SW_SHOWNORMAL;
  110. end;
  111. CreateProcess(nil, PChar(sProgramToRun), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, Result);
  112. end;
  113. { ----------------------------------------------------------------------------
  114. Launch a process (hidden if requested) and wait for it to finish
  115. ---------------------------------------------------------------------------- }
  116. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  117. var
  118. ProcessInfo: TProcessInformation;
  119. begin
  120. ProcessInfo := Launch(sProgramToRun, hide);
  121. getExitCodeProcess(ProcessInfo.hProcess, Result);
  122. while Result = STILL_ACTIVE do begin
  123. sleep(1000);
  124. GetExitCodeProcess(ProcessInfo.hProcess, Result);
  125. end;
  126. end;
  127. { ----------------------------------------------------------------------------
  128. Launch a process and either waits for it or returns control immediately
  129. ---------------------------------------------------------------------------- }
  130. procedure RunProgram(sProgramToRun: String; wait: boolean);
  131. begin
  132. if wait then ExecAndWait(sProgramToRun)
  133. else Launch(sProgramToRun);
  134. end;
  135. { ----------------------------------------------------------------------------
  136. Launch java, allowing for 64 bit windows to be really shit.
  137. ---------------------------------------------------------------------------- }
  138. function RunJava(arguments: String): Longword;
  139. type
  140. TEnableRedirection = function(dwThreadId: Pointer): BOOL; stdcall;
  141. TDisableRedirection = function(dwThreadId: Pointer): BOOL; stdcall;
  142. var
  143. K32Handle: THandle;
  144. EnableRedirection: TEnableRedirection;
  145. DisableRedirection: TDisableRedirection;
  146. hasWow64: boolean = false;
  147. javaCommand: String = 'javaw.exe';
  148. begin
  149. K32Handle := GetModuleHandle('kernel32.dll');
  150. if (K32Handle > 0) then begin
  151. @DisableRedirection := GetProcAddress(K32Handle, 'Wow64DisableWow64FsRedirection');
  152. @EnableRedirection := GetProcAddress(K32Handle, 'Wow64RevertWow64FsRedirection');
  153. hasWow64 := Assigned(DisableRedirection) and Assigned(EnableRedirection);
  154. end;
  155. javaCommand := javaCommand+' '+arguments;
  156. if hasWow64 then begin
  157. // Look for 64Bit Java.
  158. DisableRedirection(nil);
  159. result := ExecAndWait(javaCommand);
  160. EnableRedirection(nil);
  161. // If it didn't work, try 32 bit.
  162. // Ideally we should only perform this check if the failure was caused
  163. // by the file not being found, which I think is error codes 2 and/or 3.
  164. // if ((result = 2) or (result = 3)) then begin
  165. if (result <> 0) then begin
  166. result := ExecAndWait(javaCommand);
  167. end;
  168. end
  169. else begin
  170. // 32Bit Windows just uses 32bit
  171. result := ExecAndWait(javaCommand);
  172. end;
  173. end;
  174. { ----------------------------------------------------------------------------
  175. ---------------------------------------------------------------------------- }
  176. end.