Link to a DLL in Pelles C

2019-07-25 08:43发布

问题:

I have a 218KB .dll and a 596KB .so file, both with identical names. I want to link to the .dll to avoid the "unresolved external symbol" error that the linker returns, but I can't find a way to link to the DLL file.

According to this Pelles C forum topic, I need to use the .def file to create a .lib... but I don't have a .def file. This forum topic shows how to use polink to create a .lib from the command line, so I ran polink /? to get some more options. I noticed a /MAKEDEF option, but running this with both the .dll and the .so gives a "No library file specified" fatal error.

I have been trying to do this for three hours, and am out of ideas. I have got to the point where my web searches turn up my own help-requests. There must be a way to do this... How can I link to a .dll?

回答1:

With information found in the header #include and your details, here is a way to replace the missing function by calling them dynamically from your software. 1- the following prototype is in #include :

typedef float (* XPLMFlightLoop_f)(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void * inRefcon);

2- some const that you can fill as needed:

const char *sDllPathName = "<Your XPLM_API DLL>.dll";
const char *sXPLMRegisterFlightLoopCallbackName = "XPLMRegisterFlightLoopCallback";

In order to confirm the sXPLMRegisterFlightLoopCallbackName, you can use the freeware Dependency Walker and check name and format of the exported functions.

3- declare the prototype of the external function:

Be aware to the calling convention __cdecl or __stdcall

In the current case, the keyword XPLM_API is defined in the XPLMDefs.h as follow:

#define XPLM_API __declspec(dllexport) // meaning __cdecl calling convention

typedef void (__cdecl *XPLMRegisterFlightLoopCallback_PROC)(XPLMFlightLoop_f, float, void *);

4- clone the function to call it in your software:

#include <windows.h>

void XPLMRegisterFlightLoopCallback(XPLMFlightLoop_f inFlightLoop, float inInterval, void * inRefcon)
{
    HINSTANCE hInstDLL;
    XPLMRegisterFlightLoopCallback_PROC pMyDynamicProc = NULL;

    // Load your DLL in memory
    hInstDLL = LoadLibrary(sDllPathName);
    if (hInstDLL!=NULL)
    {
        // Search for the XPLM Function
        pMyDynamicProc = (XPLMRegisterFlightLoopCallback_PROC) GetProcAddress(hInstDLL, sXPLMRegisterFlightLoopCallbackName);
        if (pMyDynamicProc != NULL)
        {
            // Call the XPLM Function with the orignal parameter
            (pMyDynamicProc)(inFlightLoop,inInterval,inRefcon);
            return;
        }
    }
    // Do something when DLL is missing or function not found
}

5- just add your described call:

...
XPLMRegisterFlightLoopCallback(callbackfunction, 0, NULL);
...