title: GetProcAddress with all loaded libraries
body: <p>With <code>dlopen</code> you can provide <code>NULL</code> as the library name and get a handle that allows you to find a symbol in <em>any</em> of the loaded libraries:</p>
  
  <blockquote>
    <p>If filename is a NULL pointer, then the returned handle is for the
    main program. When given to dlsym(), this handle causes a search for a
    symbol in the main program, followed by all shared libraries loaded at
    program startup, and then all shared libraries loaded by dlopen() with
    the flag RTLD_GLOBAL.</p>
  </blockquote>
  
  <p>Can you do the same with <code>GetProcAddress</code>? I want to search for the presence of a Windows API but different libraries are loaded in Windows 8.</p>
  
  <p>I know what libraries are loaded by looking in the COFF headers, I guess I could loop through the handles there...</p>
  
  <p>This is the code I'm currently using:</p>
  
  <p><code>.hpp</code></p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;stdexcept&gt;
  
  /**
   * @~english
   * Looks up a Windows API function. Make sure you set @c _WIN32_WINNT so that the definition is available at compile
   * time.
   * @par Example
   * @code
   * # undef _WIN32_WINNT
   * # define _WIN32_WINNT 0x600
   * # include &lt;system/inc/nt/windows.h&gt;
   * static const auto initialize_srw_lock_ptr = FunctionPtrLookup(InitializeSRWLock, "kernel32");
   * @endcode
   * @param function the function definition to lookup
   * @retval nullptr the function did not exist on this version of Windows
   * @returns a function pointer to invoke
   */
  #define FunctionPtrLookup(function, library) \
    FunctionLookup&lt;decltype(function)&gt;(#function, library)
  
  /**
   * @~english
   * The return type of FunctionLookup
   */
  typedef void(*FunctionLookupPtr)();
  
  /**
   * @~english
   * Looks up a Windows API function. 
   * @param name the name of the function to find in the library
   * @retval nullptr the function did not exist on this version of Windows
   * @returns a function pointer to invoke
   * @see FunctionPtrLookup
   */
  FunctionLookupPtr FunctionLookup(const std::string&amp; name, const std::string&amp; library);
  
  /// @copydoc FunctionLookup
  template&lt;typename Signature&gt;
  const Signature * FunctionLookup(const std::string&amp; name, const std::string&amp; library) {
    return reinterpret_cast&lt;const Signature*&gt;(FunctionLookup(name, library));
  }
  </code></pre>
  
  <p><code>.cpp</code></p>
  
  <pre><code>FunctionLookupPtr FunctionLookup(const std::string&amp; name, const std::string&amp; library) {
    const auto wide_library = Utf8ToWide(library);
    const auto lib = LoadLibraryW(wide_library.c_str());
    if (!lib) {
      return nullptr;
    }
    return reinterpret_cast&lt;FunctionLookupPtr&gt;(GetProcAddress(lib, name.c_str()));
  }
  </code></pre>
  
  <p>Ideally, I'd want to remove the <code>library</code> variable.</p>