Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++


CodeBlocks is a open source, free C/C++ IDE that just works across platforms. The create-project wizard is so easy to use and it gives you a very good starting point for your project. The DLL (Dynamic Link Library) plays an important role in windows applications system. The *.dll files contain a bunch of frequently-used functions/entries that can be invoked from different applications. The windows system manages these files in memory. A reference counter is kept to count how many applications are using the DLLs and if the counter falls to zero for some DLL, it means that the DLL file is no longer needed and may be removed from the RAM (Random Access Memory). The DLL files are the same as *.EXE in terms of file format. They are both PE formats except that the DLL files do not have a clear entry point.

You can go to File — New — Project in Codeblocks to start the wizard.

codeblocks-create-dll-template Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

Select the DLL project item and click ‘Go’.

codeblocks-create-dll2 Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

Choose the project directory and click ‘Next’.

codeblocks-create-dll3 Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

Tick the compiler group/options, the default is with both ‘DEBUG’ and ‘RELEASE’. Click the ‘Finish’ and we will notice two files are created, which is main.cpp and main.h

codeblocks-create-dll4 Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

codeblocks-create-dll5 Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

Compile the project (Ctrl + F9) and you will have a SampleDLL.dll generated in the corresponding folder. Using DLL Export Viewer to see the list of the exported functions.

codeblocks-create-dll6 Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++ beginner c / c++ code programming languages windows

Source code for main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

It should be very straightforward if you want to export another function in the DLL project.

#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
703 words
Last Post: C/C++ Coding Exercise - Recursive Computation of Value e
Next Post: How To Use Compound Commands in Windows Command Shell

The Permanent URL is: Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++

One Response

  1. Dave B

Leave a Reply