0

I need to create a dll which contains stuff I have in my executable project in visual studio 2010. I realized instead of creating a dll project, I can just change the project configuration in project properties >> General >> 'configuration type' to 'dll' and it builds fine. It creates the dll. I added an additional .h/.cpp files which contains the export functions I want in the dll.

My first concern is that is this a legit dll? I am trying to load it using LoadLibrary() but I get error code 126 (The specified module could not be found) although the dll is in the project directory (same as executable). I am just wondering if it has to do with the fact it may not be a fully qualified dll for any reason? My exe project is MFC project.

** Update **

Thanks to the comments, I can now load the dll successfully - it was dependencies issue. However GetProcAddress() doesn't return valid pointer for the export function. The dumpbin /exports utility shows the dll has no export functions!

So I have added just .h/cpp files to the original project which has a simple dummy function for export right now.

__declspec(dllexport) int MakeDouble(int value);

I also included the header file in the app class just in case. I am wondering why does this function does't appear as an export? What do I have to do?

zar
  • 11,361
  • 14
  • 96
  • 178
  • It is probably a legit DLL, but whether or not is has what you want inside it, or if it is using the same toolchain as your other project, is up for grabs. you can try `dumpbin /exports ` – IdeaHat Dec 30 '13 at 17:57
  • 1
    126 may come from a missing dependency. See that answer: http://stackoverflow.com/a/14362289/1374704 – manuell Dec 30 '13 at 18:00
  • @manuell It does have other dependencies, good suggestion! – zar Dec 30 '13 at 18:03
  • A DLL also needs a DllMain. A better way to do this is to create a new skeleton DLL Project then add all the files used to build the EXE and compile that. – snowdude Dec 30 '13 at 18:15
  • 1
    What @manuell said. depends.exe is your friend! – Taylor Brandstetter Dec 30 '13 at 19:04
  • 1
    Beware of the C++ name decoration! Maybe you'll want to declare the function as `__declspec(dllexport) extern "C"`. – rodrigo Dec 31 '13 at 15:08
  • Please double check that dumpbin show no exported function after you added the `dllexport` Dependency Walker also shows the exports. – manuell Jan 03 '14 at 14:09

1 Answers1

2

First:

__declspec(dllexport) int MakeDouble(int value);

Function declaration should have the same signature than the definition and, of course, the function must have a definition (at simple return 0; should work }

Second:

The exported function name is decorated with beautiful weird characters, you should use extern "C" (or the MS specific stdcall + the .def file).:

//.h
extern "C" __declspec(dllexport) int MakeDouble(int value);

//.cpp
extern "C" __declspec(dllexport) int MakeDouble(int value) {
    return 0;
}

You should also check in project properties the option:

Configuration Properties -> C/C++ -> Code Generation -> Runtime Library

Make sure the value contains the word DLL.

ichramm
  • 6,437
  • 19
  • 30
  • thanks, that did it. My mistake was the function was not declared with `__declspec(dllexport)` in cpp file – zar Jan 07 '14 at 16:10