22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ #include " platform/globals.h"
6+ #if defined(DART_HOST_OS_WINDOWS)
7+ #include < Psapi.h>
8+ #include < Windows.h>
9+ #include < combaseapi.h>
10+ #include < stdio.h>
11+ #include < tchar.h>
12+ #endif
13+
514#include " include/dart_api.h"
615#include " vm/bootstrap_natives.h"
716#include " vm/exceptions.h"
1524
1625namespace dart {
1726
18- #if defined(USING_SIMULATOR)
27+ #if defined(USING_SIMULATOR) || defined(DART_PRECOMPILER)
1928
2029DART_NORETURN static void SimulatorUnsupported () {
2130 Exceptions::ThrowUnsupportedError (
@@ -41,7 +50,7 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_providesSymbol, 0, 2) {
4150 SimulatorUnsupported ();
4251}
4352
44- #else // defined(USING_SIMULATOR)
53+ #else // defined(USING_SIMULATOR) || defined(DART_PRECOMPILER)
4554
4655static void * LoadDynamicLibrary (const char * library_file) {
4756 char * error = nullptr ;
@@ -56,9 +65,60 @@ static void* LoadDynamicLibrary(const char* library_file) {
5665 return handle;
5766}
5867
68+ #if defined(DART_HOST_OS_WINDOWS)
69+ // On windows, nullptr signals trying a lookup in all loaded modules.
70+ const nullptr_t kWindowsDynamicLibraryProcessPtr = nullptr ;
71+
72+ void * co_task_mem_alloced = nullptr ;
73+
74+ void * LookupSymbolInProcess (const char * symbol, char ** error) {
75+ // Force loading ole32.dll.
76+ if (co_task_mem_alloced == nullptr ) {
77+ co_task_mem_alloced = CoTaskMemAlloc (sizeof (intptr_t ));
78+ CoTaskMemFree (co_task_mem_alloced);
79+ }
80+
81+ HANDLE current_process =
82+ OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE ,
83+ GetCurrentProcessId ());
84+ if (current_process == nullptr ) {
85+ *error = OS::SCreate (nullptr , " Failed to open current process." );
86+ return nullptr ;
87+ }
88+
89+ HMODULE modules[1024 ];
90+ DWORD cb_needed;
91+ if (EnumProcessModules (current_process, modules, sizeof (modules),
92+ &cb_needed) != 0 ) {
93+ for (intptr_t i = 0 ; i < (cb_needed / sizeof (HMODULE)); i++) {
94+ if (auto result =
95+ reinterpret_cast <void *>(GetProcAddress (modules[i], symbol))) {
96+ CloseHandle (current_process);
97+ return result;
98+ }
99+ }
100+ }
101+ CloseHandle (current_process);
102+
103+ *error = OS::SCreate (
104+ nullptr ,
105+ " None of the loaded modules contained the requested symbol '%s'." ,
106+ symbol);
107+ return nullptr ;
108+ }
109+ #endif
110+
59111static void * ResolveSymbol (void * handle, const char * symbol) {
60112 char * error = nullptr ;
61- void * result = Utils::ResolveSymbolInDynamicLibrary (handle, symbol, &error);
113+ #if !defined(DART_HOST_OS_WINDOWS)
114+ void * const result =
115+ Utils::ResolveSymbolInDynamicLibrary (handle, symbol, &error);
116+ #else
117+ void * const result =
118+ handle == kWindowsDynamicLibraryProcessPtr
119+ ? LookupSymbolInProcess (symbol, &error)
120+ : Utils::ResolveSymbolInDynamicLibrary (handle, symbol, &error);
121+ #endif
62122 if (error != nullptr ) {
63123 const String& msg = String::Handle (String::NewFormatted (
64124 " Failed to lookup symbol '%s': %s" , symbol, error));
@@ -70,7 +130,15 @@ static void* ResolveSymbol(void* handle, const char* symbol) {
70130
71131static bool SymbolExists (void * handle, const char * symbol) {
72132 char * error = nullptr ;
133+ #if !defined(DART_HOST_OS_WINDOWS)
73134 Utils::ResolveSymbolInDynamicLibrary (handle, symbol, &error);
135+ #else
136+ if (handle == nullptr ) {
137+ LookupSymbolInProcess (symbol, &error);
138+ } else {
139+ Utils::ResolveSymbolInDynamicLibrary (handle, symbol, &error);
140+ }
141+ #endif
74142 if (error != nullptr ) {
75143 free (error);
76144 return false ;
@@ -91,8 +159,7 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_processLibrary, 0, 0) {
91159 defined (DART_HOST_OS_ANDROID) || defined (DART_HOST_OS_FUCHSIA)
92160 return DynamicLibrary::New (RTLD_DEFAULT);
93161#else
94- Exceptions::ThrowUnsupportedError (
95- " DynamicLibrary.process is not available on this platform." );
162+ return DynamicLibrary::New (kWindowsDynamicLibraryProcessPtr );
96163#endif
97164}
98165
0 commit comments