Jump to content

How to create and use .dll in C ?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Kuto

Kuto

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Please you answer for C, i don't use VB. A lot of examples "create in C, use in VB". I want simple source code plz. Thank you..

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Creating and using a .dll in C depends a little on your compiler. The basic idea is this:
When you compile your supporting files, you have the option to link the object files into a .dll instead of linking them into a .exe. Then when you compile the program that uses the supporting files, the linker can be told to dynamically link instead of statically link to the files. As long as the .dll with the dynamic links is in the same folder as the .exe or is in a folder in the path, you're in good shape.

Note: on *nix systems, it's a .so file.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Creating a DLL in C is easy, you only need to specify the entry point, wich will be the first function, and then just write your functions and compile the source to a dll.

Example (The source file):

/* This is dll.c */

#include <windows.h>


/* DLL Entry Point */
BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

/* Example function */

__declspec(dllexport) int sum(const int a, const int b){
    return a + b;
}


And the header file for include in any program:

/* dll.h */

__declspec(dllimport) int sum(const int a, const int b);