Search code examples
cwindowsdllload

Don't know how to load and use dll file in C


I couldn't find much tutorial about loading a DLL file in C program, but in the ones I found they said I shouldn't use c(it is better to use c++). Why? I'm working on Windows 7 64-bit using Geany and c and I have a dll file that someone else wrote and I need to use the functions that are defined inside. So could you please explain how can I do this in the simplest possible way or recommend a tutorial.

Thanks. ps. I'm new at this so be gentle


Solution

  • In order to make your program use a DLL file you must instruct the compiler to include that library in your project. Generally, the library will consist of a .lib file (for linking to your .exe), a .dll file (for dynamically linked code) and an include folder with lots of .h files. You cannot use a .dll file on its own, you must have the headers and lib files.

    First, you have to add the lib and include folders to the library and include directories respectively, I don't know how this is done in the exact IDE you're using, but generally you can do it from the properties page of your project.

    Then, you must add the name of the libraries (without the extensions) to the "libraries" property of your project.

    Thirdly, you must #include "header.h" at the top of your .c file for all of the library components that you want to use in your program.

    Then, you should simply be able to compile, and move the dll file to the same folder as your program, and if the stars align you should be okay.

    Note that some libraries can be statically linked into your program, this means that the code that would normally be contained in a .dll file will instead be compiled directly into your executable file.


    C++ is better than C because it is C but with more features, for instance, the built-in ability to use strings is one. Another one is having better OOP (Object Oriented Programming). Do yourself a favour and stray away from online tutorials, look around for a generally recommended book for the language of your choice, and read it from cover to cover.

    In general, unless you have a good reason to NOT use C++, you should use it.

    I'd suggest either moving to Visual Studio Community on Windows, or better yet, switch to Linux for coding and learn how to use autoconf, your brain will thank you.