Using C++ decltype to ease implementation of “late” binding to a DLL

You can just include the file with the prototype of the function exported by the DLL you’ll load with LoadLibrary. Then use decltype on it:

#include <iostream>
#include <UserEnv.h>

int wmain() {
   auto h  = LoadLibrary(L"Userenv.dll");
   if (!h)
      return -1;
   auto farproc = GetProcAddress(h, "GetDefaultUserProfileDirectoryW");
   if (!farproc)
      return -2;
   auto fn = reinterpret_cast<decltype(& GetDefaultUserProfileDirectory)>(farproc);
   wchar_t path[MAX_PATH];
   DWORD count;
   if(!fn(path, & count))
      return -3;
   std::wcout << path 
              << std::endl;
   return 0;
}
Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s