Help me with DLL animals :)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 1,371
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Help me with DLL animals :)

 
-1
  #1
Jul 5th, 2009
I have this DLL code, but I cannot access function.
Where am I wrong?

main.h
  1. #ifndef __MAIN_H__
  2. #define __MAIN_H__
  3.  
  4. #include <windows.h>
  5. /* To use this exported function of dll, include this header
  6.  * in your project.
  7.  */
  8.  
  9. #ifdef BUILD_DLL
  10. #define DLL_EXPORT __declspec(dllexport)
  11. #else
  12. #define DLL_EXPORT __declspec(dllimport)
  13. #endif
  14.  
  15.  
  16. #ifdef __cplusplus
  17. extern "C"
  18. {
  19. #endif
  20.  
  21. //simple functions are being exported
  22. EXPORT_DLL double Addition(double a, double b);
  23. EXPORT_DLL double Multiplication(double a, double b);
  24. #ifdef __cplusplus
  25. }
  26. #endif
  27.  
  28. #endif // __MAIN_H__

main.cpp
  1. #include "main.h"
  2.  
  3. //add them and return the answer
  4. double DLL_EXPORT Addition(double a, double b)
  5. {
  6. double c = a+b;
  7. return c;
  8. }
  9.  
  10. //multiply them and return the answer
  11. double DLL_EXPORT Multiplication(double a, double b)
  12. {
  13. double c = a*b;
  14. return c;
  15. }


And here is the console App that calls the DLL
  1. #include <iostream>
  2. #include <windows.h>
  3. typedef double(*FuncPtr)(double a, double b);
  4. //declare DLL handle
  5. HINSTANCE hDLL;
  6. //declare pointers to the functions
  7. FuncPtr AddFunc, MultiFunc;
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14. //create handle to our DLL
  15. hDLL = LoadLibrary("mathdll");
  16. //Get proc address if handle is valid
  17. if (hDLL!=NULL)
  18. {
  19. cout<<"Loaded DLL! \n"<<"Getting Proc Address"<<endl;
  20. AddFunc = (FuncPtr)GetProcAddress(hDLL, "Addition");
  21. if(AddFunc)
  22. {
  23. cout<<"Sum of 8 and 2 is: "<<AddFunc(8.0, 2.0);
  24. FreeLibrary(hDLL);
  25. }
  26. else
  27. {
  28. cout<<"Failed to get a function! Unloading the DLL"<<endl;
  29. FreeLibrary(hDLL);
  30. }
  31. }
  32. return 0;
  33.  
  34. }
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,371
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Help me with DLL animals :)

 
-2
  #2
Jul 5th, 2009
Please help me as I'm just diving in DLL issues
Cheers
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC