Sunday, March 24, 2013

Windows DLL Explicit Linking

Explicit Linking

The harder way to load a DLL is a little bit more complicated. You will need function pointers and some Windows functions. But, by loading DLLs this way, you do not need the .lib or the header file for the DLL, only the DLL. I'll list some code and then explain it.
 Find more about DLL from here
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. typedef int (*AddFunc)(int,int);
  5. typedef void (*FunctionFunc)();
  6.  
  7. int main()
  8. {
  9. AddFunc _AddFunc;
  10. FunctionFunc _FunctionFunc;
  11. HINSTANCE hInstLibrary = LoadLibrary("DLL_Tutorial.dll");
  12.  
  13. if (hInstLibrary)
  14. {
  15. _AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add");
  16. _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary,
  17. "Function");
  18.  
  19. if (_AddFunc)
  20. {
  21. std::cout << "23 = 43 = " << _AddFunc(23, 43) << std::endl;
  22. }
  23. if (_FunctionFunc)
  24. {
  25. _FunctionFunc();
  26. }
  27.  
  28. FreeLibrary(hInstLibrary);
  29. }
  30. else
  31. {
  32. std::cout << "DLL Failed To Load!" << std::endl;
  33. }
  34.  
  35. std::cin.get();
  36.  
  37. return 0;
  38. }

No comments:

Post a Comment