Loading DLLs at Runtime

Started by
4 comments, last by allsorts46 21 years, 2 months ago
Hi, I asked about this sometime before christmas but its gone now so I'll have to make this new thread. I was trying to load a DLL at runtime, and get a class from that DLL, then use it as if it was defined in the program itself rather than in the external DLL. Several people offered help on this, thanks for that, and I tried what you suggested, but I still cant quite seem to make it work. Here is what I have, prehaps you can see something I havent done, or have done wrong There are two separate VC6 projects, one for the DLL and one for the program: Project IIR_Library IIR_Library.cpp:
      
#include <iostream.h>
#include "iir.h"

testpt* getclass (void);

void test::draw(void)
	{
	cout << "\n\nIT WORKS!\n";
	}

testpt* getclass (void)
	{
        return (testpt*) new test;
	}
  
IIR_Library.def
        
LIBRARY      "IIR_Library"
DESCRIPTION  'IP Independant Renderer'

EXPORTS
getclass      @1
   
  
Project IIR_Program IIR_Program.cpp:
  
#include <windows.h>
#include <iostream.h>
#include "..\IIR_Library\iir.h"

void main (void)
	{
	HINSTANCE thelib;
	testpt* theclass;

	cout << "Searching for IIR_Library.DLL... ";
	thelib = LoadLibrary("iir_library");

	if (thelib==NULL)
		{
		cout << "Not found\n"; 
		return;
		}

	cout << "\nUsing getclass() to return pointer to renderer...\n";
        theclass = (testpt*)(GetProcAddress(thelib, "getclass")());

	cout << "Returned from getclass(), renderer is apparently at " << &theclass << "\n"; 
	cout << "Calling member function draw()...";

	// Following line produces the error :

	//		error C2228: left of '.draw' must have class/struct/union type


	// Also tried using "test* theclass" instead of "testpt* theclass" and getting getclass() to return a "test*",

	// but this results in exactly the same thing.


	theclass.draw();
	}
  
Shared File IIR.h:
      
struct testpt
{
	virtual void draw(void) = 0;
};

class test : public testpt
{
	private:
	public:
	virtual void draw(void);
};
  
This might not be quite right, I have been playing with it a bit and I dont have VC installed right now (a job for thursday), but thats what the files are currently. Hope someone can help me. Also if anyone else has done something similar prehaps they could share this with me? Thank you, Ashley Warning in life.cpp (8): const Happiness is declared but never used. EDIT: Messed up the source text box thingies EDIT 2: Uh, all the includes seem to have gone in one big line unexplainably. Its not like that in the source files obviously EDIT 3: Editing this message messes it up [edited by - allsorts46 on January 21, 2003 5:10:10 PM] [edited by - allsorts46 on January 21, 2003 5:12:24 PM] [edited by - allsorts46 on January 21, 2003 5:15:51 PM]
"The finger of blame has turned upon itself"
Advertisement
One more time. No-one have any idea?
"The finger of blame has turned upon itself"
You have a pointer to an object, so use -> instead of . when calling the method draw ().
without looking at your code to closely, wouldn''t this work?

theclass->draw();
Thanks a lot, works fine now. What exactly does ''->'' do?

Warning in life.cpp (8): const Happiness is declared but never used.
"The finger of blame has turned upon itself"
a pointer is a variable which contains the address of data. The . operator increments in memory to the location of the member you''re accessing, of a typical structure. i think. it makes sense anyway. With a pointer, it only is using 4 bytes of memory, but points to another location which actually contains the actual declared structure object thingie. So you can''t use ., you use ->, which goes to the new memory location.

struct FOO
{
int a;
int b;
}

FOO bob;
bob.a = 1;

FOO * pbob = new FOO; //Allocate a FOO
pbob->a = 1; //Dereference pbob, accessing a

i guess i think it looks like this in memory:
bob[ aaaa bbbb ] a takes up four bytes, so does b
where as
pbob[ #### ] four bytes which point to..
####[ aaaa bbbb ] lol, i guess that''s good enough...

sorry if i confuse and screw you up more.

This topic is closed to new replies.

Advertisement