Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27
  1. #11
    Join Date
    Jan 2003
    Posts
    536
    Building them is pretty simple. In visual studio, just start a dll project, or change the configuration type to dynamic library (dll). This is about the only project change there is, I believe. I have converted exe projects to dll by messing with the project setup as well, so it isnt very different.

    Once youve got that setup, there are a few decisions to be made. First, you can give all your classes a dll interface so you can spawn them from an app like normal (this is only for C++ I believe), or if you want to use C style dll's you have to create a .def file and define some functions as extern "C". If you choose the C method, you need to add these functions to your .def file. I wont elaborate anymore on that since this is a C++ forum.

    To make your classes dll interface, all you need to do is add it to your declarations. The trick is that when youre building the dll, you must define the classes as:

    Code:
    class __declspec(dllexport) MyClass : public MyOtherClass
    {
    ...
    };
    When you are linking to the dll, you need to include the headers in the exe that uses the dll (otherwise how does it know about the classes, right?), and also link to the .lib file created with the dll. In the headers, the classes must be specified as:

    Code:
    class __declspec(dllimport) MyClass : public MyOtherClass
    {
    ...
    };
    So how does one have both, without having two sets of headers? Magic.

    In your dll project, set up a header that is for general purpose #defines, or put it wherever you like. Now #define a constant for these dll interface specifiers, like so:

    Code:
    #define DLL_IMPORT __declspec(dllimport)
    #define DLL_EXPORT __declspec(dllexport)
    Then you need to #define one more variable to use in your classes. This is the fun part:

    Code:
    #ifndef BUILD_EXPORTS
    #define ENGINE_API DLL_IMPORT
    #else
    #define ENGINE_API DLL_EXPORT
    #endif
    What this is doing is #defining a constant that is defined different depending on where the header gets included. In your dll project, you must #define BUILD_EXPORTS before #including the header that includes all this mojo. Since only your dll project will have this #defined, it will cause the ENGINE_API to be DLL_EXPORT, and anything that does not have it #defined will be DLL_IMPORT, which is exactly what we want. THEN, in your classes, you simply need:

    Code:
    class ENGINE_API MyClass : public MyOtherClass
    {
    ...
    };
    That is probably the most difficult portion of the dll setup. Once you've got this working, the rest is fairly straightforward, save for a few gotcha's which pop up when you least expect.

    To have regular functions (not classes, just functions) be exported from the dll, its pretty much the same syntax:

    Code:
    ENGINE_API void MySuperCoolFunction();
    Be sure that the definition of the function is seperated from the declaration, otherwise you get a "redefinition" error when linking to your dll from the app. Generall you put the definition in a .cpp file, with the one exception being inline functions, in which case it is acceptable to put the definition in the header, but below the declaration. Like so:

    Code:
    // declaration
    ENGINE_API void MySuperCoolFunction();
    
    // definition
    inline void MySuperCoolFunction()
    {
    ...
    }
    Now, to load your dll at runtime (before you can use anything inside of it, you have to load it), you use a simple function which will do it for you. I'm not sure if this function is a win32 function or what, but the MSDN doesnt say anything about any required headers, so it should be safe.

    To load your dll, simply:

    Code:
    HINSTANCE Library;	// this is global, do not lose this
    
    Library = LoadLibrary("MyAwesomeDrv.dll");
    And when you're done with the dll, you should let the handle go. Windows keeps track of references to dll's and only unloads it when there are none left, so this can be fairly important.

    Code:
    if (Library)
    	FreeLibrary(Library);
    And that's pretty much it. There are a ton of rules about dll classes, and stuff, I will go over the basics, the more advanced version can be found on MSDN.

    1. dll-interface classes can only extend other dll-interface classes.
    2. If a dll-interface class has a variable of another type of class, that other class must also be dll-interface. The exception here is when that variable is PRIVATE and is never used outside of that class, but you will still recieve a compile warning.
    3. With regular functions (not classes), you can NOT include the definition of a function with the declaration, they must be seperate, even for inline functions.
    4. You can also dll-interface specific functions of classes, check MSDN for syntax (its pretty simple).


    If I've missed anything, ask.
    Last edited by Bonehed316; 08-05-2005 at 03:46 PM.

  2. #12
    Join Date
    Aug 2003
    Location
    Madrid - Spain
    Posts
    1,153
    Wow, I'm gonna test it know. Thank you!
    n4x0 -- C++ Programmer
    Tank Game - Progress: 11%

  3. #13
    Join Date
    Aug 2003
    Location
    Peabody, MA
    Posts
    919
    that is what i intend to cover in the video
    Last edited by Zecster; 08-09-2005 at 03:23 AM. Reason: is not a class on dll's hust one simple video and then i will take it from there.

  4. #14
    Join Date
    Aug 2003
    Location
    Peabody, MA
    Posts
    919
    I think I will have the video file ready by tonight or tomorrow night.

  5. #15
    Join Date
    Jul 2003
    Location
    Calgary, Alberta
    Posts
    1,355
    Awesome, I'll be waiting
    confused by earlier errors
    bailing out

  6. #16
    Join Date
    Aug 2003
    Location
    Peabody, MA
    Posts
    919
    yea, i pm'ed zak and buzz to see if you could look at it before i released it out to public but I forgot to send the link with that this morning and they both replied about needing the link, so i just sent it to them so it will probobly be tomorrow unless they get to look at it tonight.

  7. #17
    Join Date
    Aug 2003
    Location
    Peabody, MA
    Posts
    919
    yea, if it goes well i might invest more time in going more indepth will dll's plus i have a few more ideas, like a tutorial on creating your own custom linked list just like the one in the STL. I don't know yet I will probloby post a poll if you people like this short one to be released.

  8. #18
    Join Date
    Mar 2005
    Posts
    144
    soooooo.... how long until we get to see it?

  9. #19
    Join Date
    Aug 2003
    Location
    Peabody, MA
    Posts
    919
    well, at the last tomorrow night, I would like to give buzz and zak and chance to look over it tomorrow so I can re shoot it so it will be better than it is now but I will release it tomorrow night even if they don't. I would not get your hopes up to high after how good it is but then again i always think that my stuff is never that good but we will see shortly.

  10. #20
    Join Date
    Jan 2005
    Posts
    8
    That wuld be heaps helpful.

    I starting C++ and i want to learn more and a .dll tut would be great

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •