ViewHTML.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ** Command & Conquer Generals(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. //--------------------------------------------------------------------------
  75. char tempPath[MAX_PATH];
  76. GetWindowsDirectory(tempPath, MAX_PATH);
  77. char filename1[MAX_PATH];
  78. char filename2[MAX_PATH];
  79. GetTempFileName(tempPath, "WS", 0, filename1);
  80. strcpy( filename2, filename1 );
  81. char* extPtr = strrchr(filename2, '.');
  82. strcpy(extPtr, ".html");
  83. // DebugPrint(filename);
  84. Msg( __LINE__, TEXT(__FILE__), TEXT("filename = %s"), filename2 );
  85. //--------------------------------------------------------------------------
  86. // Create file
  87. //--------------------------------------------------------------------------
  88. HANDLE file = CreateFile(
  89. filename2,
  90. GENERIC_WRITE,
  91. 0,
  92. NULL,
  93. CREATE_ALWAYS,
  94. FILE_ATTRIBUTE_NORMAL,
  95. NULL);
  96. if (file == INVALID_HANDLE_VALUE)
  97. {
  98. // DebugPrint("***** Unable to create temporary HTML file '%s'\n", filename);
  99. Msg( __LINE__, TEXT(__FILE__), TEXT("***** Unable to create temporary HTML file '%s"), filename2 );
  100. return false;
  101. }
  102. // Write generic contents
  103. const char* contents = "<title>ViewHTML</title>";
  104. DWORD written;
  105. WriteFile(file, contents, strlen(contents), &written, NULL);
  106. CloseHandle(file);
  107. // Find the executable that can launch this file
  108. char exeName[MAX_PATH];
  109. HINSTANCE hInst = FindExecutable(filename2, NULL, exeName);
  110. // Delete temporary file
  111. DeleteFile(filename2);
  112. DeleteFile(filename1);
  113. if ((int)hInst <= 32)
  114. {
  115. // DebugPrint("***** Unable to find executable that will display HTML files.\n");
  116. Msg( __LINE__, TEXT(__FILE__), TEXT("***** Unable to find executable that will display HTML files."));
  117. return false;
  118. }
  119. // Launch browser with specified URL
  120. char commandLine[MAX_PATH];
  121. sprintf(commandLine, "[open] %s", url);
  122. STARTUPINFO startupInfo;
  123. memset(&startupInfo, 0, sizeof(startupInfo));
  124. startupInfo.cb = sizeof(startupInfo);
  125. PROCESS_INFORMATION processInfo;
  126. BOOL createSuccess = CreateProcess(
  127. exeName,
  128. commandLine,
  129. NULL,
  130. NULL,
  131. FALSE,
  132. 0,
  133. NULL,
  134. NULL,
  135. &startupInfo,
  136. &processInfo);
  137. if (createSuccess == FALSE)
  138. {
  139. // DebugPrint("\t**** Failed to CreateProcess(%s, %s)\n", exeName, commandLine);
  140. Msg( __LINE__, TEXT(__FILE__), TEXT("\t**** Failed to CreateProcess(%s, %s)"), exeName, commandLine );
  141. return false;
  142. }
  143. if (wait == true)
  144. {
  145. WaitForInputIdle(processInfo.hProcess, 5000);
  146. bool waiting = true;
  147. while (waiting == true)
  148. {
  149. if (callback.DoCallback() == true)
  150. {
  151. break;
  152. }
  153. Sleep(100);
  154. DWORD exitCode;
  155. GetExitCodeProcess(processInfo.hProcess, &exitCode);
  156. if (exitCode != STILL_ACTIVE)
  157. {
  158. waiting = false;
  159. }
  160. }
  161. }
  162. return true;
  163. }