Page 1 of 1

[SOLVED]Import class/struct from a DLL?

Posted: Mon Dec 13, 2010 12:20 pm
by MadPumpkin
I just need to know how to import a class and struct from a DLL, so far I can import a function just fine,

This is the code I have

Code: Select all

#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

#define MAXMODULE 50
#define DLL_IMPORT_EXPORT


typedef void (WINAPI*cfunc)();
cfunc TestModule;

int main() {
   
      HINSTANCE hLib=LoadLibrary("SPINELESSAPI.DLL");


      if(hLib==NULL) {

           cout << "Unable to load library!" << endl;
           getch();
           return 1;
      }

      char mod[MAXMODULE];

      GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
      cout << "Library loaded: " << mod << endl;


      TestModule=(cfunc)GetProcAddress((HMODULE)hLib, "TestModule");
      

      if((TestModule==NULL) )//|| (TestRect==NULL))
      {
           cout << "Unable to load function(s)." << endl;
           FreeLibrary((HMODULE)hLib);
           return 1;
      }

      TestModule();
      SRECTANGLE Rectangle; //--This line right here doesn't work, and I can't figure out why...--//
      
      FreeLibrary((HMODULE)hLib);
      
      getch();
      return 0;
}

Re: Import class/struct from a DLL?

Posted: Tue Dec 14, 2010 12:07 am
by JesseGuarascia
Hey there! I'm probably replaying a little late, but it's pretty simple. If you've ever used SDL, then you should know the gist of it, if not, lemme give you the low-down.

Using a .DLL file (I'm assuming that you made) is quite simple. All you have to do is go to your project's properties, and change the files that link to it, such as myLib.lib in the Input sub-section of the Linker section of your properties page. Add the include and library directories of the .DLL to your project or the directories if you can do that. Make sure the .DLL file is in your project folder. Finally, include the header file(s) in your .cpp file of the project using the .DLL and you should be good to go.

Just Google, "Adding Include and Library Directories for..." and then you're IDE. If its Visual Studio 2008/2010 and you want help, I should be able to. :D

Re: Import class/struct from a DLL?

Posted: Tue Dec 14, 2010 11:58 am
by qpHalcy0n
This method's a little more difficult.



EXPORT

Code: Select all

#ifdef EXPORT
     #define EXPORTSHIT __declspec(dllexport)         // Define EXPORT in your symbols only when actually exporting
#else
     #pragma comment(lib, "libname.lib")
     #define EXPORTSHIT __declspec(dllimport)
#endif
     

class shit
{
     public:
          shit();
          ~shit();
      
          void eat();

     private:
          int someshit;
};

extern "C"
void* EXPORT GetShit();       // This needs a C signature
implementation of the export (must be done elsewhere)

Code: Select all

void* EXPORT GetShit()
{
      return (void*)(new shit);
}
IMPORT

Code: Select all


HINSTANCE hdll = NULL;
shit* myShit = NULL;
typedef void* (*pvfunc)();
pvfunc CreateShit;


hdll = LoadLibrary("dllname.dll");		
CreateShit = (pvfunc)(GetProcAddress( hdll, "GetShit" ) );
myShit = (shit*)(CreateShit());

myShit->eat();

Re: Import class/struct from a DLL?[SOLVED]

Posted: Tue Dec 14, 2010 1:02 pm
by MadPumpkin
Thank you both! You both helped me a ton.

Re: Import class/struct from a DLL?

Posted: Tue Dec 14, 2010 5:11 pm
by GroundUpEngine
qpHalcy0n wrote:

Code: Select all

myShit.eat();
:lol: but seriously.. awesome technique!