Loading a function from a DLL from C

Licenced under CC-BY-4.0, created by Peter Molnar (), published at to canonical URL https://petermolnar.net/loading-a-function-from-a-dll-from-c/ with keywords #C #code #DLL

Calling functions from a DLL in a C code? Don’t. But it’s possible.

I was doing one of the nastiest task ever in the last few weeks: call a function from a DLL written in C++ from native C code. Here’s how to do it with latest MinGW.

The DLL contains a function, named Test, returning an int, and have one single int parameter and - how suprising - returns an int as status.



#include <windows.h>
#include <winbase.h>
#include <windef.h>
#include <stdio.h>

typedef int (*TestFunc)(int);

int loadDLL( void );



#include "dllhandler.h"

int loadDLL( )
    int status = 0;
    TestFunc _TestFunc;
    HINSTANCE testLibrary = LoadLibrary("test.dll");

    if (testLibrary)
        _TestFunc = (TestFunc)GetProcAddress(testLibrary, "Test");
        if (_TestFunc)
            status = _TestFunc();
    return status;