0

I'm trying to include a use DLL's methods using C++.

I've tried to include the DLL using this code:

HMODULE DLL = LoadLibrary(_T("name.dll"));

        if (DLL)
        {
            std::cout << "DLL loaded!" << std::endl;


            if (_pdisconnect)
            {
                std::cout << "Successful link to function in DLL!" << std::endl;
            }

            else
            {
                std::cout << "Unable to link to function in DLL!" << std::endl;
            }
        }
        else
        {
            std::cout << "DLL failed to load!" << std::endl;
        }
    FreeLibrary(DLL);

That DLL that I'm trying to include has two classes PCls and TPCls. The PCls has a method which I'm trying to include is getOP(LONG a). How to use that method, please?

Thanks a lot!

user3508865
  • 153
  • 1
  • 1
  • 10
  • If you can't export the class itself you have no instance where to use those functions (assuming nonstatic ones). – Marco A. Apr 29 '14 at 08:16
  • @Marco, do you mean that I need to export for each class one DLL ? – user3508865 Apr 29 '14 at 11:16
  • No, I mean that a non-static member function, to be usable, requires an object of the class it belongs to. If you can't import that object you can't use that function – Marco A. Apr 29 '14 at 11:18
  • @Marco, but, the functions are public. That would be easily exported, isn't it? – user3508865 Apr 29 '14 at 11:21
  • If you have control over the class you're trying to export I suggest to read this first: http://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll/ – Marco A. Apr 29 '14 at 11:24

1 Answers1

2

The problem is that you can't import classes from a DLL, only functions. However, you could have factory functions in the DLL which creates the instances and return a pointer (or you pass in a reference to the factory function that it initializes).

To get a pointer to a function you use GetProcAddress. However note that you must pass it the mangled name of the function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It's worth noting that trying to `GetProcAddress` a mangled function name is [not necessarily a good idea](http://stackoverflow.com/questions/23314750/find-name-mangled-function-in-dynamically-loaded-dll). – cf- Apr 29 '14 at 08:35
  • @JoachimPileborg, Would you please complete the code for a better understand? – user3508865 Apr 29 '14 at 11:13