[SOLVED]Import class/struct from a DLL?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

[SOLVED]Import class/struct from a DLL?

Post 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;
}
Last edited by MadPumpkin on Tue Dec 14, 2010 1:02 pm, edited 1 time in total.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: Import class/struct from a DLL?

Post 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
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Import class/struct from a DLL?

Post 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();
Last edited by qpHalcy0n on Tue Dec 14, 2010 8:46 pm, edited 1 time in total.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

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

Post by MadPumpkin »

Thank you both! You both helped me a ton.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Import class/struct from a DLL?

Post by GroundUpEngine »

qpHalcy0n wrote:

Code: Select all

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