1

i try to load a library using LoadLibrary and GetProcAddress to use functions in this library. however, the dll is loaded fine but i dont get the function.

dll.h

#ifndef TEST_DLL_H
#define TEST_DLL_H

void __declspec(dllexport) __stdcall tests();

#endif // TEST_DLL_H

dll.cpp

#include <iostream>
#include "test_dll.h"

void __declspec(dllexport) __stdcall tests() {
    std::cout << "tests" << std::endl;
}

main.cpp

typedef void(*f_funci)();

int main() {
    HMODULE hMod = LoadLibrary(L"plugins/test_dll.dll");
    if (!hMod)
        throw(std::runtime_error("failed to load dll"));

    f_funci f = (f_funci)GetProcAddress(hMod, "tests");
    if (!f) {
        std::cout << GetLastError() << "\n"; // code is 127 "The specified procedure could not be found."
        throw(std::runtime_error("could not locate function"));
    }

    f();
}

so i tried to dump the dll (dumpbin /exports) and saw the tests functions is defined as ?tests@@YGXXZ.

00000000 characteristics
52DD9D2B time date stamp Mon Jan 20 23:03:23 2014
    0.00 version
       1 ordinal base
       1 number of functions
       1 number of names

ordinal hint RVA      name

      1    0 00011037 ?tests@@YGXXZ = @ILT+50(?tests@@YGXXZ)

Summary

    1000 .data
    1000 .idata
    3000 .rdata
    1000 .reloc
    1000 .rsrc
    B000 .text
   10000 .textbss

now if i try

f_funci f = (f_funci)GetProcAddress(hMod, "?tests@@YGXXZ");

it works.

so my question is, why is the function resolution wrong?

edit:

i'm using visual studio 2013 express.

character set is not set.

user1810087
  • 5,146
  • 1
  • 41
  • 76

1 Answers1

0

There is another option besides that given in the "duplicate" post. This option involves creating a .def file for your dll.

From http://msdn.microsoft.com/en-us/library/d91k01sh.aspx :

If you are exporting functions in a C++ file, you have to either place the decorated names in the .def file or define your exported functions with standard C linkage by using extern "C".

ooga
  • 15,423
  • 2
  • 20
  • 21