ViewHTML.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  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 General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Renegade Setup/Autorun/ViewHTML.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Maria_l $
  27. *
  28. * VERSION INFO
  29. * $Modtime: 2/16/01 11:32a $
  30. * $Revision: 3 $
  31. *
  32. ******************************************************************************/
  33. #pragma warning(disable : 4201 4310)
  34. #include <windows.h>
  35. #include "ViewHTML.h"
  36. //#include "..\win.h"
  37. #include <stdio.h>
  38. //#include "debugprint.h"
  39. #include "wnd_file.h"
  40. /******************************************************************************
  41. *
  42. * NAME
  43. * ViewHTML
  44. *
  45. * DESCRIPTION
  46. * Launch the default browser to view the specified URL
  47. *
  48. * INPUTS
  49. * URL - Website address
  50. * Wait - Wait for user to close browser (default = false)
  51. * Callback - User callback to invoke during wait (default = NULL callback)
  52. *
  53. * RESULT
  54. * Success - True if successful; otherwise false
  55. *
  56. ******************************************************************************/
  57. bool ViewHTML(const char* url, bool wait, CallbackHook& callback)
  58. {
  59. // DebugPrint("ViewHTML()\n");
  60. Msg( __LINE__, TEXT(__FILE__), TEXT("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ));
  61. Msg( __LINE__, TEXT(__FILE__), TEXT("ViewHTML()" ));
  62. Msg( __LINE__, TEXT(__FILE__), TEXT("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ));
  63. //--------------------------------------------------------------------------
  64. // Just return if no URL specified
  65. //--------------------------------------------------------------------------
  66. if ((url == NULL) || (strlen(url) == 0))
  67. {
  68. // DebugPrint("***** No URL specified.\n");
  69. Msg( __LINE__, TEXT(__FILE__), TEXT("***** No URL specified." ));
  70. return false;
  71. }
  72. //--------------------------------------------------------------------------
  73. // Create unique temporary HTML filename
  74. // JFS: Fixed so that it would go to the temp folder which was crashing
  75. // on limited users.
  76. //--------------------------------------------------------------------------
  77. char tempPath[MAX_PATH];
  78. char filename1[MAX_PATH];
  79. char filename2[MAX_PATH];
  80. // Expand the TMP environment variable.
  81. {
  82. DWORD dwResult;
  83. dwResult = ExpandEnvironmentStrings( "%TEMP%", tempPath, MAX_PATH);
  84. if(dwResult == 0)
  85. return false;
  86. }
  87. GetTempFileName(tempPath, "WS", 0, filename1);
  88. strcpy( filename2, filename1 );
  89. char* extPtr = strrchr(filename2, '.');
  90. strcpy(extPtr, ".html");
  91. // DebugPrint(filename);
  92. Msg( __LINE__, TEXT(__FILE__), TEXT("filename = %s"), filename2 );
  93. //--------------------------------------------------------------------------
  94. // Create file
  95. //--------------------------------------------------------------------------
  96. HANDLE file = CreateFile(
  97. filename2,
  98. GENERIC_WRITE,
  99. 0,
  100. NULL,
  101. CREATE_ALWAYS,
  102. FILE_ATTRIBUTE_NORMAL,
  103. NULL);
  104. if (file == INVALID_HANDLE_VALUE)
  105. {
  106. // DebugPrint("***** Unable to create temporary HTML file '%s'\n", filename);
  107. Msg( __LINE__, TEXT(__FILE__), TEXT("***** Unable to create temporary HTML file '%s"), filename2 );
  108. return false;
  109. }
  110. // Write generic contents
  111. const char* contents = "<title>ViewHTML</title>";
  112. DWORD written;
  113. WriteFile(file, contents, strlen(contents), &written, NULL);
  114. CloseHandle(file);
  115. // Find the executable that can launch this file
  116. char exeName[MAX_PATH];
  117. HINSTANCE hInst = FindExecutable(filename2, NULL, exeName);
  118. // Delete temporary file
  119. DeleteFile(filename2);
  120. DeleteFile(filename1);
  121. if ((int)hInst <= 32)
  122. {
  123. // DebugPrint("***** Unable to find executable that will display HTML files.\n");
  124. Msg( __LINE__, TEXT(__FILE__), TEXT("***** Unable to find executable that will display HTML files."));
  125. return false;
  126. }
  127. // Launch browser with specified URL
  128. char commandLine[MAX_PATH];
  129. sprintf(commandLine, "[open] %s", url);
  130. STARTUPINFO startupInfo;
  131. memset(&startupInfo, 0, sizeof(startupInfo));
  132. startupInfo.cb = sizeof(startupInfo);
  133. PROCESS_INFORMATION processInfo;
  134. BOOL createSuccess = CreateProcess(
  135. exeName,
  136. commandLine,
  137. NULL,
  138. NULL,
  139. FALSE,
  140. 0,
  141. NULL,
  142. NULL,
  143. &startupInfo,
  144. &processInfo);
  145. if (createSuccess == FALSE)
  146. {
  147. // DebugPrint("\t**** Failed to CreateProcess(%s, %s)\n", exeName, commandLine);
  148. Msg( __LINE__, TEXT(__FILE__), TEXT("\t**** Failed to CreateProcess(%s, %s)"), exeName, commandLine );
  149. return false;
  150. }
  151. if (wait == true)
  152. {
  153. WaitForInputIdle(processInfo.hProcess, 5000);
  154. bool waiting = true;
  155. while (waiting == true)
  156. {
  157. if (callback.DoCallback() == true)
  158. {
  159. break;
  160. }
  161. Sleep(100);
  162. DWORD exitCode;
  163. GetExitCodeProcess(processInfo.hProcess, &exitCode);
  164. if (exitCode != STILL_ACTIVE)
  165. {
  166. waiting = false;
  167. }
  168. }
  169. }
  170. return true;
  171. }