Thread: Dll?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Dll?

    okey, i have to admit, i am at a total lose. I have spent the evening reading the borlan helpfile and trying to figure out how, fistly, to create a dll and then secondly, in another project use that dll. Are dll's supported in C? if so,

    Can anyone explain the steps in creating a dll and then using them. e.g how to define your functions and the whole process.
    (i read about __del?? export import etc The dll wizard also created a WINAPI entry point. Is that ansi??)

    I guess there is a lot to it, but a few pointers will do. Just a very simple example will do.

    Cheers
    G'n'R

  2. #2
    Unregistered
    Guest
    As far as I know, a DLL is just like a header file. It's precompiled and often written by someone else and included in the driver program. It contains functions and definitions that are not declared and defined in the driver program to save on space and make the whole application more modular.

    I'm not sure how to include one, try #include "name.dll"
    But writing one shouldn't be hard at all, just write a header file and save it as a .dll.

  3. #3
    Registered User Cruxus's Avatar
    Join Date
    Aug 2001
    Posts
    87
    The easiest way to use a DLL, a dynamic-link library, is to get its header files and import library, a special static-link library that links the actual DLL in at program load time.

    Programming a DLL is a somewhat different matter. For one thing, your compiler's settings must be set to create both the DLL and its import library. A DllMain() is somewhat like WinMain() in a normal executable program. The functions that you want to expose as the interface must be declared in a certain way so that proper compiling and linking are done (you have to use __declspec(dllexport) and __declspec(dllimport) ).

    G'n'R wrote:

    The dll wizard also created a WINAPI entry point. Is that ansi??
    int WINAPI DllMain(/*. . .*/); is not ANSI/ISO C or C++. It is an OS-specific extension for Windows.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    create a dll project

    dll.h:
    Code:
    #ifndef _MY_DLL_
    #define _MY_DLL_
    
    #ifdef _IMPORTING_
    #define _DLLAPIMAIN_ __declspec(dllimport)
    #else
    #define _DLLAPIMAIN_ __declspec(dllexport)
    #endif
    
    _DLLAPIMAIN_ int multiply(int a, int b);
    
    #endif
    dll.c
    Code:
    #include "dll.h"
    
    int multiply(int a, int b)
    {
        return a * b;
    }
    compile:
    create a normal project
    copy the dll.dll, dll.lib , and dll.h file to the new project directory

    main.c
    Code:
    #include <stdio.h>
    #define _IMPORTING_
    #include <dll.h>
    
    int main()
    {
        printf("1 * 3 = %d",multiply(1,3));
        return 0;
    }
    thats all there is to it.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Cool, thanks for that. I will give it a go

    Cheers
    G'n'R

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Okey i have given it ago. Using no-one's example. But i keep getting this error when trying to make the project. It does however compile with no problems.

    [Linker Error] Unresolved external '_multiply' referenced from C:\DOCUMENTS AND SETTINGS\GNR\MY DOCUMENTS\C-C++\DLL\TEST.OBJ

    Another thing. __declspec(dllimport) __declspec(dllexport)

    I know you use dllexport if you want to export a function. That is make it availble to others you use export, right?? But what do you use dllimport for? And...how does the compiler know what dll you want to use. I gather one can have a lot of different dll on your hardrive. Dont you have to specify location etc of that dll?

    G'n'R

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    most compilers know to link to the dll by what libraries you link the program to, each dll you compile also compiles a library that you must link to inorder to use the dll,

    > But what do you use dllimport for?

    for importing the exported functions, it lets the compiler know the imported functions are actually exported in a dll, and not to look for them localy.

    >
    [Linker Error] Unresolved external '_multiply' referenced from C:\DOCUMENTS AND SETTINGS\GNR\MY DOCUMENTS\C-C++\DLL\TEST.OBJ
    <

    i forgot to tell you you have to link to the dll's library, sorry, but if you link the dll's lib this should go away.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Unregistered
    Guest
    yep i am trying to do that but i cant get it to work. I am using Borland and doing this in project options etc.
    however nevermind. i will figure it out.

    G'nR

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    are you certain,
    if you need more help just ask.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    hi no-one. once i made a dll in vcc (cant remember now) but i remember there was a defs file involved. do you need that?

    it seems that there are many different ways of doing dll, or i got it all wrong. many compilers when you say you wanna do dll, they produce a strange switch-case block that i never understood. do you know anything about that?

    thanks

    null

  11. #11
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >
    once i made a dll in vcc (cant remember now) but i remember there was a defs file involved. do you need that?
    it seems that there are many different ways of doing dll, or i got it all wrong.
    <

    there are in fact two methods of making Dll's one involves a .def the other does not, there could be more ways to make them but i havn't heard of any.

    >
    many compilers when you say you wanna do dll, they produce a strange switch-case block that i never understood. do you know anything about that?
    <

    not to sure what you mean, there are a lot of different uses for switch-cases blocks,
    was it a premade project?
    what was the compiler?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM