Advertisement
obernardovieira

Run-Time Link DLL Function

Sep 10th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. //Linked library
  2.  
  3. //main.cpp
  4.  
  5. #include <iostream>
  6. #define DECLDIR __declspec(dllexport)
  7. extern "C"
  8. {
  9.     DECLDIR int Add( int a, int b ) {
  10.         return( a + b );
  11.     }
  12.     DECLDIR int Subtract( int a, int b ) {
  13.         return( a - b );
  14.     }
  15. }
  16.  
  17. //main.def
  18. LIBRARY "calc"
  19. EXPORTS
  20.         Add @1
  21.         Subtract @2
  22.  
  23. //app.cpp
  24.  
  25. #include <windows.h>  
  26. typedef int (*funcAdd) (int, int);
  27. typedef int (*funcSubtract) (int, int);
  28.  
  29. int main( ) {
  30.     HINSTANCE hDLL = LoadLibrary(TEXT("calc.dll"));
  31.     if (hDLL == NULL) {
  32.         std::cout << "Failed to load library.\n";
  33.     }
  34.     else {
  35.         funcAdd Add = (funcAdd)GetProcAddress(hDLL, "Add");
  36.         funcSubtract Subtract = (funcSubtract)GetProcAddress(hDLL, "Subtract");
  37.  
  38.         if (Add)
  39.             std::cout << "10+10=" << Add(10, 10) << std::endl;
  40.  
  41.         if (Subtract)
  42.             std::cout << "50-10=" << Subtract(50, 10) << std::endl;
  43.  
  44.         FreeLibrary(hDLL);
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement