4

Help, please. A have a dll-file. I know it's functions and parameters. How can I use it in Eclipse with MinGW?

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Country
  • 465
  • 3
  • 7
  • 9
  • [LoadLibrary](http://msdn.microsoft.com/en-us/library/ms684175\(v=vs.85\).aspx) then [GetProcAddress](http://msdn.microsoft.com/en-us/library/ms683212\(v=vs.85\).aspx) can be used. – user786653 Aug 09 '11 at 14:04
  • 1
    it's always a struggle to get those tools to do our bidding. it's always poorly documented, if it all. and after you succeed you forget how, until five years later. and so on. if you want to link statically (which here means, the linker adds references and the loader does the DLL binding) you might start by looking into module definition files. perhaps your toolchain supports them. – Cheers and hth. - Alf Aug 09 '11 at 14:14
  • 1
    Often, if you _do_ have the function names and parameters, but not _the_ header, the easiest solution is to just write _a_ header yourself. (You'd also need to know the calling convention, but that's required in any case) – MSalters Aug 16 '11 at 15:02

4 Answers4

4

I am assuming you are using windows. In WINAPI you have LoadLibrary and GetProcAddress functions. Here's an example of usage

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • It's not possible just to add path to the dll into the compiler properties or somewhere else, like in case of static libraries? – Country Aug 09 '11 at 14:11
  • @Country Static libraries need the header to know what to call. – Deanna Aug 09 '11 at 16:06
  • 1
    @Deanna yes, but I'm talking about dynamic ones – Country Aug 10 '11 at 06:49
  • Can you get full information about the API of functions? Like does the DLL itself store information about what parameter types it expects and what return type it has, or is that information only contained in the header file? – B T Aug 20 '15 at 04:29
  • If the functions are exported as C++, then the name decoration will denote what the parameters and their types are, and you can use various tools to "unmangle" the function names to the prototypes. If the functions are exported as C, then there's no way to tell what the parameters or their types are. – Dan Korn Oct 03 '17 at 19:46
1

I understood you know the DLL function signature and you don't have the header.

For a given function dll_function with the known signature:

long dll_function(long, long, char*, char*);

You can use LoadLibrary and GetProcAddress from Windows API like examplified in the following C++ code:

#include <windows.h>
#include <iostream>

typedef long(__stdcall *f_funci)(long, long, char*, char*);


struct dll_func_args {
    long arg1;
    long arg2;
    std::string arg3;
    std::string arg4;
};

// Borrowing from https://stackoverflow.com/a/27296/832621
std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

int main()
{
    std::string filename = "C:\\...\\mydllfile.dll";
    dll_func_args args;
    args.arg1 = 1;
    args.arg2 = 2;
    args.arg3 = "arg3";
    args.arg4 = "arg4";

   std::wstring tmp = s2ws(filename);
    HINSTANCE hGetProcIDDLL = LoadLibrary(tmp.c_str());

    if (!hGetProcIDDLL)
    {
        std::cerr << "Failed to load DLL" << std::endl;
        return EXIT_FAILURE;
    }

    // resolve function address here
    dll_func_ptr func = (dll_func_ptr)GetProcAddress(hGetProcIDDLL, "dll_function");
    if (!func)
    {
        std::cout << "Failed to load function inside DLL" << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << "Return value " << func(args.arg1, args.arg2, (char *)args.arg3.c_str(), (char *)args.arg4.c_str()) << std::endl;

    return EXIT_SUCCESS;
}
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
1

I created a wrapper to simplify this sort of thing a while back.

Update: I completely forgot about this post and deleted the blog post and associated source code. I wound up with a dangling pointer here ;-)

Luckily, someone did a much better job than I did: Boost.DLL

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
1

If you DO have appropriate .LIB file, and you have exact function prototype, you don't need header. Just declare the functions youself (possibly in your own custom header). Call those functions directly. Link with .LIB file. The DLL would get loaded by OS, and functions would be called.

If you don't have .LIB file to link to DLL, you need to use LoadLibrary and GetProcAddress as others have suggested.

Ajay
  • 18,086
  • 12
  • 59
  • 105