[llvm] [Support][Windows] Add utility function for retrieving a DLL-exported function (PR #97905)

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 6 10:18:49 PDT 2024


================
@@ -245,6 +245,12 @@ std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
 std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
                           size_t MaxPathLen = MAX_PATH);
 
+/// Dynamically retrieve an exported function from a DLL, while casting it to a
+/// known type. This avoids -Wcast-function-type-mismatch.
+template <typename T> T funcFromModule(HMODULE Mod, StringRef Name) {
+  return (T)(void *)::GetProcAddress(Mod, Name.data());
+}
----------------
compnerd wrote:

What about something like:

```c++
template <typename ReturnType, typename... Arguments>
auto funcFromModule(HMODULE hMod, StringRef Name) -> ReturnType ((*)(Arguments...)) {
  return reinterpret_cast<ReturnType (*)(Arguments...)>(::GetProcAddress(hMod, Name.data());
}
```

https://github.com/llvm/llvm-project/pull/97905


More information about the llvm-commits mailing list