0

I'm importing a function in a console application from an external .dll the function copies a struct out of shared memory (if you want to test it then any global memory should work)

Here is the function in the dll

struct DataMemBuff {
    double High[5] = { 0,0,0,0,0 };
};

#ifdef __cplusplus    // If used by C++ code, 
    extern "C" {          // we need to export the C interface
#endif
    __declspec(dllexport) DataMemBuff __cdecl GetDatainBuf();
#ifdef __cplusplus
}
#endif  


DataMemBuff __cdecl GetDatainBuf()
{
    DataMemBuff tempbuf;
    memcpy(&tempbuf, lpvMem, sizeof(tempbuf));
    return tempbuf;
}

And here is an example of how I import that function into the console application

#include "stdafx.h"
#include <Windows.h>
#include <memory.h> 
#include <iostream>
#include <tchar.h>
using namespace std;

typedef DataMemBuff(CALLBACK* _cdecl GetData)(void);
GetData _GetData;

int _tmain(int argc, _TCHAR* argv[])
{
    HMODULE hDll = NULL;
    int x = 1;
    struct DataMemBuff tempIndcData; 

    hDll = LoadLibrary(_T("Data.dll"));
    if (hDll == NULL)
        {
            cout << "The dll did not load" << endl;
            printf("Here is the error %lu", GetLastError());
        }
    else
    {
        cout << "The dll loaded fine" << endl;
    }


    _GetData = (GetData)GetProcAddress(hDll, "GetDatainBuf");
    if (!_GetData)
    {
        // handle the error  
        cout << "The dll did not load the function" << endl;

    }
    else
    {
         // call the function  
         tempIndcData = _GetData();
         printf("The memory was copied\n");

    }

}

the function imports fine, however it has a problem returning the data on the stack back to the function because of the c style calling convention _cdecl, and it throws an exception on this line when it calls the imported funciton:

tempIndcData = _GetData();

Exception thrown: This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

I have tried throwing in a _cdecl in the declaration going from this:

typedef DataMemBuff(CALLBACK* GetData)(void);
GetData _GetData;

to this:

typedef DataMemBuff(CALLBACK* _cdecl GetData)(void);
GetData _cdecl _GetData; 

and that did not help, probably because I don't understand calling enough, but there must be some way to tell GetProcAddress that it is getting a function call with c-style calling conventions.

My question is: what syntax do I use to import a function that uses c-style calling conventions using GetProcAddress?

Voltage Spike
  • 162
  • 4
  • 15
  • First and foremost, remove CALLBACK from your code. – n. m. could be an AI Jan 07 '18 at 06:59
  • CALLBACK means `__stdcall`, the polar opposite of `__cdecl`. Declaring the calling convention attribute more than once is not slammed with an error message, following C++ standard conventions, but only the first one matters. So it really is stdcall and the chkstk() diagnostic that warns about the fatal stack imbalance is not unusual. Although the real code surely looks different from this snippet. It is "_GetDatainBuf" (leading underscore) in 32-bit code. Do favor implicit linking. – Hans Passant Jan 07 '18 at 08:34

0 Answers0