LaunchWeb.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. ** Command & Conquer Renegade(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: /Commando/Code/wwlib/LaunchWeb.cpp $
  22. *
  23. * PROGRAMMER
  24. * Denzil E. Long, Jr.
  25. * $Author: Denzil_l $
  26. *
  27. * VERSION INFO
  28. * $Revision: 2 $
  29. * $Modtime: 6/22/01 4:39p $
  30. *
  31. ******************************************************************************/
  32. #include "LaunchWeb.h"
  33. #include <windows.h>
  34. #include <shellapi.h>
  35. #include <stdio.h>
  36. #include <assert.h>
  37. /******************************************************************************
  38. *
  39. * NAME
  40. * LaunchWebBrowser
  41. *
  42. * DESCRIPTION
  43. * Launch the default browser to view the specified URL
  44. *
  45. * INPUTS
  46. * URL - Website address
  47. * Wait - Wait for user to close browser (default = false)
  48. * Callback - User callback to invoke during wait (default = NULL callback)
  49. *
  50. * RESULT
  51. * Success - True if successful; otherwise false
  52. *
  53. ******************************************************************************/
  54. bool LaunchWebBrowser(const char* url)
  55. {
  56. // Just return if no URL specified
  57. if (!url || (strlen(url) == 0))
  58. {
  59. return false;
  60. }
  61. // Create a temporary file with HTML content
  62. char tempPath[MAX_PATH];
  63. GetWindowsDirectory(tempPath, MAX_PATH);
  64. char filename[MAX_PATH];
  65. GetTempFileName(tempPath, "WWS", 0, filename);
  66. char* extPtr = strrchr(filename, '.');
  67. strcpy(extPtr, ".html");
  68. HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  69. FILE_ATTRIBUTE_NORMAL, NULL);
  70. assert(INVALID_HANDLE_VALUE != file && "Failed to create temporary HTML file.");
  71. if (INVALID_HANDLE_VALUE == file)
  72. {
  73. return false;
  74. }
  75. // Write generic contents
  76. const char* contents = "<title>ViewHTML</title>";
  77. DWORD written;
  78. WriteFile(file, contents, strlen(contents), &written, NULL);
  79. CloseHandle(file);
  80. // Find the executable that can launch this file
  81. char exeName[MAX_PATH];
  82. HINSTANCE hInst = FindExecutable(filename, NULL, exeName);
  83. assert(((int)hInst > 32) && "Unable to find executable that will display HTML files.");
  84. // Delete temporary file
  85. DeleteFile(filename);
  86. if ((int)hInst <= 32)
  87. {
  88. return false;
  89. }
  90. // Launch browser with specified URL
  91. char commandLine[MAX_PATH];
  92. sprintf(commandLine, "[open] %s", url);
  93. STARTUPINFO startupInfo;
  94. memset(&startupInfo, 0, sizeof(startupInfo));
  95. startupInfo.cb = sizeof(startupInfo);
  96. PROCESS_INFORMATION processInfo;
  97. BOOL createSuccess = CreateProcess(exeName, commandLine, NULL, NULL, FALSE,
  98. 0, NULL, NULL, &startupInfo, &processInfo);
  99. assert(createSuccess && "Failed to launch default WebBrowser.");
  100. return (TRUE == createSuccess);
  101. }