943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 445
  • C++ RSS
Jul 5th, 2009
-1

Help me with DLL animals :)

Expand Post »
I have this DLL code, but I cannot access function.
Where am I wrong?

main.h
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Jul 5th, 2009
-2

Re: Help me with DLL animals :)

Please help me as I'm just diving in DLL issues
Cheers
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Start project - help
Next Thread in C++ Forum Timeline: Need help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC