0

I am working on a project where I am creating a small DLL and then also creating a windows application to use it.

I can not figure out what is going on.

I have a function in the DLL called "startPicadorVisual" which takes one parameter which is a std::string.

In the application which is dependent on the DLL I have the following code in a mostly auto-generated .h file:

typedef void (__stdcall *f_startPicadorVisual)(string s);

namespace PicadorPrototype {
    f_startPicadorVisual startPicadorVisual;
    Form1(void) {
    //Load DLL Funcs
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"..\\Debug\\Picador.dll");

    if (!hGetProcIDDLL) {
        std::cout << "could not load the dynamic library" << std::endl;
        throw "Bad Stuff";
    }

    startPicadorVisual = (f_startPicadorVisual)GetProcAddress(hGetProcIDDLL, "startPicadorVisual");

    if (!startPicadorVisual) {
        std::cout << "could not locate the function" << std::endl;
        throw "More Bad Stuff";
    }

When which fails on the second step when I call GetProcAddress.

The functions are defined as follows in my DLL:

void __declspec(dllexport) startPicadorVisual(string fixtureNamet);
PicadorResults __declspec(dllexport) getPicadorReading(string fixtureName);

Can anyone tell me why this isn't working?

Tucker Downs
  • 92
  • 1
  • 11
  • 2
    I believe it's because of name mangling. http://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function – Ivan Jun 02 '14 at 14:21
  • 3
    The easiest way to use a DLL is to allow the C runtime to perform the load library and the linkage to the dynamic library. When you compile and link a DLL there should be at least two things created: (1) the .dll itself and (2) a .lib file that contains the stubs for the .dll. You can link with the .lib file and include the .dll file along with your executable. The linker will link with the stubs in the .lib file which will then handle loading the library and connecting the various stubs with the actual functions in the .dll. – Richard Chambers Jun 02 '14 at 14:25
  • Thanks @RichardChambers I am still interested in why this code doesn't work but your comment made me think again about using that system and with time not on my side right now I thought it would be good to just do it. Things are working now. – Tucker Downs Jun 02 '14 at 14:31
  • @IvanGrynko I think you are probably right, unfortunately I haven't had time to go back and check. Which I suppose is a good thing, it means I'm making progress on my project. – Tucker Downs Jun 24 '14 at 15:04

1 Answers1

3

GetProcAddress fails if the name you give GetProcAddress doesn't match exactly the name of the function you're calling. By exact I mean everything -- characters that make up the function name, the function name must match casing, etc.

So either your DLL exported a different name and you didn't realize it, or you're not exporting the name at all.

The way you can find out the names of the exported DLL functions easily, you can use the Dependency Walker program found here: http://www.dependencywalker.com/

Also, it isn't a good idea to use C++ objects that allocate dynamic memory such as std::string as parameters. If you do that, your DLL will only work for applications that

  1. Are compiled with the same version of Visual C++ as the DLL
  2. Use the same compiler options when building the application and DLL
  3. All components (DLL and app) must use the DLL version of the C runtime library.

Otherwise, your code would have undefined behavior, more than likely crash, even if you got as far as retrieving the function pointer correctly.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45