0

I have a external DLL file with functions names as "Test123::MyFunction", the problem here is that i can call DLL names successfully if the name do not contains "::" character, how can i pass the full function name "Test123::MyFunction" in the DLL call?

Full soure code:

#include "pch.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
int MyFunction(char* buf);
int main(int argc, char** argv)
{
    /* get handle to dll */
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"CallMe.dll");

    /* get pointer to the function in the dll*/
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL), "MyFunction"); // Here the name MyFunction should be Test123::MyFunction

    typedef int(__stdcall * pICFUNC)(char *);

    pICFUNC MyFunction;
    MyFunction = pICFUNC(lpfnGetProcessID);

    /* The actual call to the function contained in the dll */
    int intMyReturnVal = MyFunction(argv[1]);

    /* Release the Dll */
    FreeLibrary(hGetProcIDDLL);

    return intMyReturnVal;

    /* The return val from the dll */

}

Thank you

  • 1
    `GetProcAddress` -- This is a very simple function in terms of what it expects. The name you use must match *exactly* with the exported name. That includes casing and special characters. Obviously you are mistaken as to the correct exported name. Use a tool such as Dependency Walker or dumpbin to get the **exact** name. – PaulMcKenzie Jul 10 '19 at 00:38
  • [See this answer](https://stackoverflow.com/questions/23996769/creating-and-using-a-dll-in-cpp/23997441#23997441) – PaulMcKenzie Jul 10 '19 at 00:44

1 Answers1

1

Within the visual C++ install, there should be a little utility called dumpbin.exe. If you throw that at your C++ DLL, you should be able to list the mangled names for the C++ methods you have exported. It's those text names you want to pass to GetProcAddress.

Most people however would disable name mangling on the exported functions by simply doing:

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

Which will export the names of the function as "startPicadorVisual" and "getPicadorReading". NOTE: When exporting functions using the C naming convention, it means you will not be able to use function overloading (since both of those funcs will end up with the same name).

robthebloke
  • 9,331
  • 9
  • 12