Thread: DLL's

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    DLL's

    I was wondering if anyone has a tutorial on how to make DLL's for windows, (or possibly making DLL's for other OS's also) b/c my apartment complex burnt down yesterday and I lost all my books (Programming Windows 5th edition and about 30 other books) are all gone untill my stupid insurance pays off. So someone please help out a down and out code junkie.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Sorry to hear about your home.........that's terrible. Hope you get back on your feet soon.

    Here's a small example I did for someone on the C++ board - here

    Basically, all you are doing is creating functions and storing them outside of your program. You tell the compiler to export certain functions from the dll (see the #define DllExport extern "C" __declspec (dllexport) thing....then put DllExport in front of every exported function) and then it creates a dll file and a lib file. store that lib file in your main project.

    Then declare the exported functions in the main project as you would for any normal forward declaration, but this time declare Macro in front of every decalaration as "#define DllImport extern "C" __declspec (dllimport)". Then prepend DllImport to every declaration. As long as the Dll is in the same Dir as your program (or one of the normal system dirs for a Dll), your program should run.

    You should also try to stick to normal C for dlls......

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Why should I stick to using C in a DLL? I was thinking about putting some classes into one, is this a bad idea? Thanks for the info so far.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The main problem is that funcs in C++ have name mangling issues (more than 1 name of a function so overloading can work)....therefore other languages choke as they cannot use these functions....

    I add the "#define DllImport extern "C" to my dlls because I think that allows C to read them at the cost of operator overloading (I have never tested this though........)

    If you are working in C++ exclusively, then you may be ok......

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well I generally dont use function overloading unless I really really really have to but usually I dont need to (other than constructors!). Can I put a class into a DLL? Sorry about all the questions but no place and all this stress has made me not tired and i want to code something on my rent's old pc which im on and let me tell you it sucks! It cant even run Quake2
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by xds4lx
    Well I generally dont use function overloading unless I really really really have to but usually I dont need to (other than constructors!). Can I put a class into a DLL? Sorry about all the questions but no place and all this stress has made me not tired and i want to code something on my rent's old pc which im on and let me tell you it sucks! It cant even run Quake2
    Yeah, I guess, but to tell the truth I have not tried......

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Ok im assuming that this will only work for Window's, is there any way to make DLL's that are cross platform? or does that require a totally different code layout?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  8. #8
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb Well....

    I use Classes in DLL's all the time! It'z possible, but az he said, it's not good for thing'z made to be used in 'multi-language' stuff.

    the 'DLL' concept itself i'z platform independant, but a 'DLL' file itself iz just 4 window'z, u can figure out the 'dll' type thing on that OS, and 'recompile'.

    SPH

    P.S., if doing classes, you will obviously need to exclude the "extern "C"" part!

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    So should I use extern "C++" instead?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Smile No! :)

    If you're using a C++ Compiler, you're only using "extern "C"" to tell the compielr to compile in C for a section, you need not tell a C++ compiler to compile in C++!

    Hope that answer'z ur Question, feal free to AIM me:

    aGaBoOgAmOnGeR
    Email/MSNIM: [email protected]

    Thankx ~SPH

  11. #11
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Whats the difference between __declspec (dllexport) and __declspec(dllimport) ?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  12. #12
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb Diff =

    dllimport imports symbolz (using the dll), dllexport exports (creating the dll).

    SPH

  13. #13
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Ok, well that doesnt seem clear enough, b/c in the source im looking at they give two examples of the same thing and they just say that __declspec(dllimport) just makes a little more efficient code and thats it. They use both dllimport and dllexport on the same function and i sure as hell dont see any difference. Also, is it better to just use the header you compiled the DLL with or write a new one w/ a different name?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by xds4lx
    Ok, well that doesnt seem clear enough, b/c in the source im looking at they give two examples of the same thing and they just say that __declspec(dllimport) just makes a little more efficient code and thats it. They use both dllimport and dllexport on the same function and i sure as hell dont see any difference.
    I think __declspec is a compiler command for VC++ that allows you to create dlls without having to create .def files (These files were to help the compiler know which functions to export).......I think its just a conveinience thing and its obvioiusly not part of the language.....worth putting in.



    Also, is it better to just use the header you compiled the DLL with or write a new one w/ a different name?
    Sorry, I dont understand.

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Also, is it better to just use the header you compiled the DLL with or write a new one w/ a different name?<<

    I think you can use the same one if you use a #ifdef..#endif block to #define __declspec(dllimport) or __declspec(dllexport) depending on your requirements.
    eg
    Code:
    #if defined IMPORT_DLL
    #define DLL_FN_THINGY __declspec(dllimport)
    #else
    #define DLL_FN_THINGY __declspec(dllexport) 
    #endif
    To use just #define IMPORT_DLL as required.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Protection of DLLs
    By Poche in forum C# Programming
    Replies: 5
    Last Post: 06-04-2009, 08:30 PM
  2. Some doubts on DLLs
    By BrownB in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2007, 02:25 AM
  3. standart dlls
    By keeper in forum C++ Programming
    Replies: 3
    Last Post: 07-05-2006, 07:32 PM
  4. Can't load string from resource DLL's string table
    By s_k in forum Windows Programming
    Replies: 4
    Last Post: 07-15-2003, 06:43 AM
  5. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM