0

When I try and get a proc address for a function called print, it is able to load the ManualLinking.dll but not the function. The error code that windows gives is 127. The client app is almost a direct copy paste from windows.

DLL:

#include"pch.h"
#include<string>
#include<iostream>

__declspec(dllexport) void __stdcall print(std::string data) {
    std::cout << data << std::endl;
}

CPP:

#include <windows.h> 
#include<iostream>
#include"Header.h"
#include<string>

typedef void(__stdcall* MYPROC)(std::string data);

int main(void)
{
    HINSTANCE hinstLib;
    MYPROC ProcAdd;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    // Get a handle to the DLL module.

    hinstLib = LoadLibrary(TEXT("ManualLinking.dll"));

    // If the handle is valid, try to get the function address.

    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC)GetProcAddress(hinstLib, "print");

        // If the function address is valid, call the function.

        if (NULL != ProcAdd)
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd)("Message sent to the DLL function\n");
        }
        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib);
    }

    // If unable to call the DLL function, use an alternative.
    if (!fRunTimeLinkSuccess) {
        printf("Message printed from executable\n");
        std::cout << GetLastError() << std::endl;
    }

    std::cin.get();
    return 0;
}
Grady
  • 45
  • 6
  • I think your problem is this: [https://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function](https://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function) – drescherjm Aug 11 '20 at 18:29
  • I tried doing this but it still gave me the same error. – Grady Aug 11 '20 at 18:55
  • You probably should add the output of `dumpbin /exports ManualLinking.dll` – drescherjm Aug 11 '20 at 19:02
  • Where would I add this in visual studio? – Grady Aug 11 '20 at 19:30
  • You should be able to run that from a Visual Studio 2019 developer command prompt. Or at least I can. – drescherjm Aug 11 '20 at 19:32

1 Answers1

0

You need to replace DLL code with:

#include"pch.h"
#include<string>
#include<iostream>
extern "C"{
__declspec(dllexport) void __stdcall print(std::string data) {
    std::cout << data << std::endl;
}
}

If you still have same error, please, check if the executable can actually find the .dll

/*...*/
    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC)GetProcAddress(hinstLib, "print");

        // If the function address is valid, call the function.

        if (NULL != ProcAdd)
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd)("Message sent to the DLL function\n");
        }
        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib);
    }
    else 
    {
       std::cout << "Cannot find dll" << std::endl;
    }
/*...*/

AndryS1
  • 139
  • 2
  • "*You need to replace DLL code*" - or, use a [`.DEF` file](https://learn.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?view=vs-2019) instead. See [Exporting from a DLL Using DEF Files](https://learn.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-def-files?view=vs-2019) on MSDN. – Remy Lebeau Aug 11 '20 at 22:03
  • I already figured it out but you were close. I needed to add extern c; but I also needed to switch from __stdcall to __cdecl. This changed the name of the function from _print@20 to _print. My problem was I was just looking for print and did not know its decorated name. – Grady Aug 12 '20 at 21:26