@@ -34,79 +34,9 @@ namespace firebase {
3434namespace analytics {
3535
3636#if defined(_WIN32)
37- #define ANALYTICS_DLL_DEFAULT_FILENAME L" analytics_win.dll"
38- std::wstring g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
39- static HMODULE g_analytics_dll = 0 ;
40-
41- // Function to convert a UTF-8 string to a wide character (UTF-16) string.
42- std::wstring Utf8ToWide (const std::string& utf8String) {
43- if (utf8String.empty ()) {
44- return std::wstring ();
45- }
46-
47- // First, determine the required buffer size.
48- int wideCharCount = MultiByteToWideChar (
49- CP_UTF8, // Source code page (UTF-8)
50- 0 , // Flags
51- utf8String.c_str (), // Source UTF-8 string
52- -1 , // -1 indicates the string is null-terminated
53- nullptr , // No buffer provided, we're calculating the size
54- 0 // Requesting the buffer size
55- );
56-
57- if (wideCharCount == 0 ) {
58- // Handle error: GetLastError() can provide more details.
59- LogError (
60- " Error determining buffer size for UTF-8 to wide char conversion." );
61- return std::wstring ();
62- }
63-
64- // Allocate the wide character string.
65- std::wstring wideString (wideCharCount, 0 );
66-
67- // Second, perform the actual conversion.
68- int result =
69- MultiByteToWideChar (CP_UTF8, // Source code page (UTF-8)
70- 0 , // Flags
71- utf8String.c_str (), // Source UTF-8 string
72- -1 , // -1 indicates the string is null-terminated
73- &wideString[0 ], // Pointer to the destination buffer
74- wideCharCount // The size of the destination buffer
75- );
76-
77- if (result == 0 ) {
78- // Handle error: GetLastError() can provide more details.
79- LogError (" Error converting UTF-8 to wide char." );
80- return std::wstring ();
81- }
82-
83- // The returned wideString from MultiByteToWideChar will be null-terminated,
84- // but std::wstring handles its own length. We might need to resize it
85- // to remove the extra null character included in the count if we passed -1.
86- size_t pos = wideString.find (L' \0 ' );
87- if (pos != std::wstring::npos) {
88- wideString.resize (pos);
89- }
90-
91- return wideString;
92- }
93-
94- void SetAnalyticsLibraryPath (const char * path) {
95- if (path) {
96- g_analytics_dll_filename = Utf8ToWide (path);
97- } else {
98- g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
99- }
100- }
37+ #define ANALYTICS_DLL_FILENAME L" analytics_win.dll"
10138
102- void SetAnalyticsLibraryPath (const wchar_t * path) {
103- if (path) {
104- g_analytics_dll_filename = path;
105- } else {
106- g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
107- }
108- }
109- #endif
39+ static HMODULE g_analytics_module = 0 ;
11040
11141// Future data for analytics.
11242// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
@@ -127,14 +57,18 @@ void Initialize(const App& app) {
12757 g_fake_instance_id = 0 ;
12858
12959#if defined(_WIN32)
130- if (!g_analytics_dll) {
131- g_analytics_dll = LoadLibraryW (g_analytics_dll_filename.c_str ());
132- if (g_analytics_dll) {
133- LogInfo (" Loaded Google Analytics DLL" );
60+ if (!g_analytics_module) {
61+ // Only allow the DLL to be loaded from the application directory.
62+ g_analytics_module = LoadLibraryExW (ANALYTICS_DLL_FILENAME, NULL ,
63+ LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
64+ if (g_analytics_module) {
65+ LogInfo (" Loaded Google Analytics module" );
13466 int num_loaded = FirebaseAnalytics_LoadDynamicFunctions (g_analytics_dll);
13567 if (num_loaded < FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT) {
136- LogWarning (" Only loaded %d out of %d expected functions from DLL." ,
137- num_loaded, FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT);
68+ LogWarning (
69+ " Only loaded %d out of %d expected functions from the Google "
70+ " Analytics module." ,
71+ num_loaded, FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT);
13872 }
13973 } else {
14074 // Silently fail and continue in stub mode.
@@ -155,9 +89,9 @@ bool IsInitialized() { return g_initialized; }
15589void Terminate () {
15690#if defined(_WIN32)
15791 FirebaseAnalytics_UnloadDynamicFunctions ();
158- if (g_analytics_dll ) {
159- FreeLibrary (g_analytics_dll );
160- g_analytics_dll = 0 ;
92+ if (g_analytics_module ) {
93+ FreeLibrary (g_analytics_module );
94+ g_analytics_module = 0 ;
16195 }
16296#endif
16397
0 commit comments