Archived

This topic is now archived and is closed to further replies.

How to create DLL's?

This topic is 6015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I create DLL''s? On Flipcode.com there is a tutorial that covers functions. But when I tried to apply it to classes and other things it wouldn''t work. So would someone point me to a place that will explain how to create DLL''s?

Share this post


Link to post
Share on other sites
Thanks a lot! That solves many of the problems. Now I have just one more question for now. When my DLL is created will people have to use the resulting library along with the DLL or can I somehow make it so that they can load it themselves in their code so that they do not have to update their code when the DLL is updated?

Share this post


Link to post
Share on other sites
Basically you got 2 ways of using DLLs: load-time linking (MSDN lingo) that is pretty much the easiest way to use DLLs: All you need to compile is the static library and the header the defines the exported functions (Pretty much like how the DirectX libraries works, I guess) and you also got run-time linking which is more annoying, but sometimes necessary:
You load the library with LoadLibrary(), then, if you want to use a function, you need to get it''s address in memory with GetProcAddress() and save it with a function pointer (It''s not that complicated).

The difference is pretty simple: When you use load-time linking, the DLL is REQUIRED for the application to run, meaning that if the DLL isn''t around, the user get a nasty error ("Required Library was not found") but with run-time linking, you load the library yourself (Your application is not directly linked with the library, so you can choose if to show an error message or not and so on)



- Goblineye Entertainment
The road to success is always under construction

Share this post


Link to post
Share on other sites
I don''t think that answered my question. Nice information to know however. Let me rephrase the question.

When I compile the DLL project there are three files created. The DLL, a static library(*.lib), and an ''object file''. I have no idea what the object file is for. Now, currently when I test my DLL''s I use the static library to create the test programs. I then update the DLL by changing something in the functions in such a way as that the functions themselves, context wise, do not change. But when the new DLL is placed with the old program the program doesn''t like it and it gives me that error mentioned in the previous response.

What I want is so that people who use my DLL won''t have to re-compile their programs. They will just have to replace the DLL when a new version comes out. This is, of course, assuming that the update didn''t change the context or parameters of a funcition used in the program.

Share this post


Link to post
Share on other sites
--
When I compile the DLL project there are three files created. The DLL, a static library(*.lib), and an ''object file''. I have no idea what the object file is for. Now, currently when I test my DLL''s I use the static library to create the test programs. I then update the DLL by changing something in the functions in such a way as that the functions themselves, context wise, do not change. But when the new DLL is placed with the old program the program doesn''t like it and it gives me that error mentioned in the previous response.
--
What error message?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey Running Wolf, what you want to do is called COM, though if your just talking DLL without COM specifics, you have to distribute the static lib with it if anything name specific(outside member functons or functions) there will need to be a recompile(unless you are using COM or a COM like method :-))
and remember for any C functions to specify
#ifdef _cplusplus

extern "C"
{

}

cuz C++ compilers mangle the names and it gets confused when it tries to resolve them, also try looking up virtual function tables in MSDN itll help alot.

Share this post


Link to post
Share on other sites
Thanks a lot!!! I''ll look that up! I have just one question....which will probably be answered when I wake up. It''s late where I am so I''m probably not thinking straight but what do you mean by ''name specific''?

Share this post


Link to post
Share on other sites
quote:
Original post by Running_Wolf
What I want is so that people who use my DLL won''t have to re-compile their programs. They will just have to replace the DLL when a new version comes out. This is, of course, assuming that the update didn''t change the context or parameters of a funcition used in the program.



You don''t have to use COM for that. Let''s say you got a function called DoSomething(). Now let''s say you update the DLL with a new version, you fixed a couple of bugs in DoSomething(), but you didn''t change the function name or parameters, then it should work.
Also, like anony said, add #extern "C" before functions you want to export or the C++ compiler will mess up the name.



- Goblineye Entertainment
The road to success is always under construction

Share this post


Link to post
Share on other sites
I still can''t get my DLL to work. Where do I put the ''extern "c"'' command you talked about. In the source file or the header file? And then you put...
exter "C"
{
}
Is the function contained inside those brackets?

Share this post


Link to post
Share on other sites
I use a .def file for creating .dlls ( you''re probably verrrry confused by now, sorry for that)..

What I do is, I create an empty .dll workspace, then I add some .cpp files, add a DllMain ( the way it is when you choose to create a dll that exports some symbols), add my functions to it and finally create a .def file... You can look up the MSDN ''bout .def files.. seems like they''re kinda outdate, but hey.. it''s quite simple... You can''t import/export classes though.. (as far a I know)..

cya,
Phil


Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states

Share this post


Link to post
Share on other sites
quote:
Original post by Running_Wolf
I still can't get my DLL to work. Where do I put the 'extern "c"' command you talked about. In the source file or the header file? And then you put...
exter "C"
{
}
Is the function contained inside those brackets?


Basically you can use extern "C" like this:

extern "C" int SomeCoolFunction(int x)
{
...
return(1);
}

You should put it in the source file.
But that won't export your function, to export it, you should use a .def file (Like phueppl1 suggested) or more simply add this before the function (In the source):

extern "C" _declspec(dllexport) int SomeCoolFunction(int x)
{
...
return(1);
}

It's easier after you get used to it


- Goblineye Entertainment
The road to success is always under construction

Edited by - Tornado on June 29, 2001 12:28:54 PM

Share this post


Link to post
Share on other sites
I can get it to work with one class. But when I add another header with another class in it the thing will compile but I can''t use it with anything. VC++ starts complaining that the first class is redefined. BTW, I use the first class in the second class. Does anybody know why it does that?

Share this post


Link to post
Share on other sites