At work one brand of battery cyclers we are using has a proprietarily encodd data format which I would like to directly read in my scripts instead of the much larger ASCII version of the data. The manufacturer provided a DLL (no library or header) along with some Delphi example code of its use, very little documentation and an example executable (which fails to run with error 0xc000007b). Following this tutorial I managed to create a .lib and link my VS2018 project to the DLL.
With the following code I can call one of the desired functions from the DLL:
#pragma comment(lib, "MacReadDataFileLIB.lib")
#include <string>
extern "C" int OpenDataFile(const char*);
int main()
{
std::string path = "K:\\Testfile.036";
auto test = OpenDataFile(path.c_str());
}
When I step through the code, the function returns -1001 i. e. fails and an exception is thrown.
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
From the documentation I know that stdcall
is used.
Other relevant information from the documentation:
function OpenDataFile(FileName: PChar): int32; function LoadAndGetNextTimeData(Handle: int32; PTimeData: pointer): int32; procedure CloseDataFile(Handle: int32);
Open the Maccor data file with OpenDataFile and get a handle returned. If the handle is equal to or larger than 0, the file is successfully open. (...) Since it is common to read the entire data file, there is a dual purpose function, LoadAndGetNextTimeData, to load the time data and get/return the most common data. All functions takes at minimum the handle returned by OpenDataFile. (...) To read the data, keep calling LoadAndGetNextTimeData as long as it returns 0. Finally, the file must be closed with CloseDataFile to free up allocated memory.
When I change the function declaration to
extern "C" int __stdcall OpenDataFile(const char*);
It fails to compile with 2 error codes:
LNK1120 1 unresolved externals
LNK2019 unresolved external symbol _OpenDataFile@4 referenced in function _main
I understand that this is due to C++ name mangling, but at this point I am stuck.