Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dll's with classes

Started by _Rambo_ Jul 20, 2002 at 5:34 PM 15 replies 1.9k views
Original Post
_Rambo_
_Rambo_
Hi, someone could show me a piece of source code or tell me any link with source about how to put classes into dll''s and how to load them during run-time ? Thanks. Bye
niyaw
niyaw
Create a "Win32 DLL" project and choose "dll that exports some symbols" option. You'll have a simple DLL that exports a variable, a function, and a class. Using it is as simple as including the generated header file and linking with the created .lib.

---
Come to #directxdev IRC channel on AfterNET

[edited by - niyaw on July 20, 2002 6:38:20 PM]
_Rambo_
_Rambo_
Hi, i''ve alreay created my .dll file but now how can i load a class from it ? I''ve stored 2 classes in it.
All i know is that i need to use LoadLibrary() function to load .dll file but i don''t know nothing more.

Thanks.
niyaw
niyaw
you can''t "load classes" from a dll because they aren''t really exported. what is exported are the classes'' member functions. therefore, if you want to go GetProcAddress way, you need to retrieve function pointers to whatever member functions you want to call and call them that way. i wouldn''t recommend this route because:

1. you''ll need to manually call constructors and destructors, which is error prone (you can forget) and requires a bunch of code.

2. you''ll need to deal with mangled function names, which is even worse. open dependency viewer on your dll (you can right-click it and select view dependencies) and look at the names of exported functions. you need to import those names.

so unless you really enjoy torturing yourself, i wouldn''t recomment retrieving member functions using GetProcAddress.


the alternative is to use virtual functions. you would export a function to create an instance of a particular class from a dll, like so:

extern "C" {
MYDLL_API CMyClass *CreateMyClass() { return new CMyClass; }
}

extern "C" will prevent name mangling, and you should be able to use GetProcAddress("CreateMyClass") to get the address of that function. then you''d make all methods of CMyClass either virtual or inline. inline methods won''t be exported/imported because the compiler will expand them inplace, and virtual methods will be called through vtable which comes with the CMyClass pointer, and need not be retrieved separately.

i''m not sure if you need an interface class with pure virtual functions and an implementation class that exists only in the dll, or if a class with just virtual functions will do. try the latter, and if it doesn''t work, try the former.



of course, this assumes that you really need to use GetProcAddress. are you sure you can''t just link with the import library (mydll.lib) file? if you can, you don''t have to do anything special to call the functions exported from the dll, as the compiler and the linker will take care of that.

---
Come to #directxdev IRC channel on AfterNET
niyaw
niyaw
quote:
Original post by _Rambo_
All i know is that i need to use LoadLibrary() function to load .dll file but i don''t know nothing more.


that''s not the only way to load a dll. if you implicitly link to its lib file, the dll will be loaded automatically. are you sure you need runtime loading with LoadLibrary?
_Rambo_
_Rambo_
Hi, well...i''m new with this kind of stuff(.dll''s and .lib''s), i just started today playing with this, what i wanted to do is to place my Vector.h and Matrix.h in a .dll or .lib file so then i could use them later instead of writing this code all over again and it still is a good exercise to start working with this stuff.

Thanks.
_Rambo_
_Rambo_
Well...i guess that what i need for this is a .lib and not a .dll right ?

Thanks.
niyaw
niyaw
when you create a dll, the linker also creates a lib in the same directory. link your exe with that lib and you should be fine. is there any particular error you''re getting?
Homer Simpson
Homer Simpson
Hey guys a question , I''ve tryied to create an empity dll but it don''t makes the lib how do i create4 a empity dll that generates a lib???
______::Homer Simpson::
Homer Simpson
Homer Simpson
Hey guys a question , I''ve tryied to create an empity dll but it don''t makes the lib how do i create4 a empity dll that generates a lib???
______::Homer Simpson::
niyaw
niyaw
why would you want to have an empty dll in the first place? and what should be in that lib file? put a dummy variable in your dll and export it:

__declspec(dllexport) int dummy;
Homer Simpson
Homer Simpson
empity dll is the name of the DLL that VC++
can create it creates a clean dll file end u write it nothing in the file like if u chose Win32 DLL that export some symbols...
And you need the lib to use dll functions and variables
______::Homer Simpson::
_Rambo_
_Rambo_
Hi, niyaw, well... there''s no .lib in the same folder of .dll file, the linker are not creating .lib file but no errors were reported... weird ?

--------------------Configuration: 3dlib - Win32 Debug--------------------

3dlib.dll - 0 error(s), 0 warning(s)


Well... got a question for you.

1º What are the advantages of using a .lib file ? If i use a .lib file i''ll need to include the header files in my code, right ? If so... what are the differences of just using the headers in my code ?

Thanks.
nietzsche
nietzsche
I''ll show you a way how to do it, which has been used in production code.

First define DUMMY_EXPORTS in your preprocessor settings.

Then define this before your class declarations:

#ifdef _DLL
#ifdef DUMMY_EXPORTS
#define DUMMYAPI __declspec(dllexport)
#else
#define DUMMYAPI __declspec(dllimport)
#endif // DUMMY_EXPORTS
#else
#define DUMMYAPI
#endif // _DLL

Now decorate your classes/functions/variables like this:

class DUMMYAPI FooBar {
};

extern DUMMYAPI void CallMePlenty();

extern DUMMYAPI int whereTheSunNeverShines;

Note that it works when building dynamic as well as static libraries.
_Rambo_
_Rambo_
Hi, well... i''ve created a Win 32 Static Library and it is working fine, .lib contatins the code and .h contains declarations, that''s it, isn''t it ? Well... got a question for you.

If you already used OpenGL, well... we need to include the .h files and .lib as well, so... .lib contains the code and .h the declarations but i noticed that there''s .dll''s too, and i ask, for what ?!

Thanks
nietzsche
nietzsche
Shared code is stored in dynamic libraries (.dll). The accompanying .lib files contain stub functions which will be included in your final executable file, just as with a regular static library. Stub functions are necessary because addresses of procedures contained in dynamic libraries are not known at compile time. They will be resolved when the executable is run.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.