-1

I'm having trouble with loading a DLL in my assignment project.

Here's the header file:

I have omitted code that works and is irrelevant to the problem. Basically, hinstLib is not NULL but when the line Filter = (FILTPTR) GetProcAddress(hinstLib, "Filter"); is executed, Filter has no value. To me it seems like it is saying that the DLL has been found but it cannot find the function "Filter" inside the DLL and I have no idea why, albeit I could be wrong. I still haven't got my head around how some of this works.

Here is the DLL:

Any ideas anyone? All help is greatly appreciated!

  • James
James
  • 69
  • 1
  • 9
  • I've moved the function definitions into a separate CPP file stored within the SLL project in the assignment solution and the same problem occurs, as does it when all lines are below `#include "stdafx.h"` – James Dec 13 '18 at 03:15
  • Have you verified that your DLL exports the symbol you expect? IIRC the `dumpbin.exe` utility should be able to tell you. – jamesdlin Dec 13 '18 at 03:18
  • Also, why does your DLL's `.cpp` file have `#ifndef _ASEXPORT`? Your DLL's `.cpp` file should *always* be exporting. (It's just repeating stuff from `filter.h` anyway.) Also, `_declspec` looks wrong; it should be `__declspec` with two underscores, although that should have generated a compilation error. – jamesdlin Dec 13 '18 at 03:22
  • @jamesdlin I've changed both of those and the same error occurs – James Dec 13 '18 at 03:29
  • It is entirely normal for GetProcAddress() to fail, the name of an exported function is often different from its documented name. A basic way to double-check is to run Dumpbin.exe /exports on the DLL. If you did not use a .def file then its name is "_Filter". – Hans Passant Dec 13 '18 at 04:04
  • @JamesEaston [Get this tool](http://www.dependencywalker.com/) and check if the exported name is the one you are expecting. Also [read this answer](https://stackoverflow.com/questions/31282683/dll-call-with-stdcall-getprocaddress-in-vs2013/31283377#31283377) – PaulMcKenzie Dec 13 '18 at 04:08
  • It doesn't look like you're exporting the function. Where's the `__declspec(dllexport)` in the DLL? – Jonathan Potter Dec 13 '18 at 04:30

2 Answers2

0

Your specifiers are wrong. A good, concise way do to this is to use same header to in DLL and APP, defining the export-import interface., which uses macro like this:

#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif

And declarations:

extern "C" MY_DLL_API int Filter(int* data, int count, const WCHAR* parameterString);

Library's .cpp file would use this header and would define MY_DLL_EXPORTS.

If I understand your code right, you made it so that linker tries to export same function from both modules? ALso, function's prototype should be C-compatible to be actually extern "C"

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Because I've put `__declspec(DLLIMPORTEXPORT)` it essentially adds `__declspec` to whichever one it uses and the `extern"C"` is wrapped around the declaration so it does the same job in both cases. – James Dec 13 '18 at 03:39
  • And as I said, I'm very new to C++ and DLL's and am struggling to get my head around them. The main app and DLL already both use "filter.h" – James Dec 13 '18 at 03:40
  • @JamesEaston you need only one -dllexport - in DLL. And function should be defined in DLL's cpp. With dllimport you use it as prototype for the module that uses that .dll And you really don't need to declare it second time as extern, you already did that in header. – Swift - Friday Pie Dec 13 '18 at 03:51
0

when the line Filter = (FILTPTR) GetProcAddress(hinstLib, "Filter"); is executed, Filter has no value. To me it seems like it is saying that the DLL has been found but it cannot find the function "Filter" inside the DLL and I have no idea why

The function is likely being exported with a decorated name. You are not specifying a calling convention, so the default is usually __cdecl, which prefixes the function name with an underscore, thus it would be exported as "_Filter" instead. But this is compiler-specific behavior, so double-check your DLL's EXPORTS table with a PE viewer/dumper to see the actual name being exported. You may need to add a .def file to your project to ensure the function is exported as "Filter" as desired.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770