Reputation: 1248
I recently posted about having an issue with using DLL functions and ever since I've been trying to narrow down the problem. I recreated my section of the code to include only the DLL part, and this is where it seems the issue arises. The library is a 64bit library and I'm using Mingw64-g++ to compile. The code below is to load the dll, load the library functions, and then use them. Using the Library function is working perfectly, however allocating memory afterwards for the following call is failing (crash) before I get the chance to do anything. Here's the code I currently have.
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
typedef int WINAPI CompressFunc(uint32_t codec, char *src_buf, int64_t src_len, char *dst_buf, int64_t level,
void *opts, int64_t offs);
typedef int64_t WINAPI DecompressFunc(
char *src_buf, int64_t src_len, char *dst_buf, int64_t dst_size, int fuzz, int crc, int verbose,
uint8_t *dst_base, size_t e, void *cb, void *cb_ctx, void *scratch, size_t scratch_size, int threadPhase
);
int main()
{
string dll_path = "oo2core_5_win64.dll";
HINSTANCE dll_object = LoadLibraryA(dll_path.c_str());
cout<<GetLastError()<<endl;
string compress_function_name = "OodleLZ_Compress";
string decompress_function_name = "OodleLZ_Decompress";
CompressFunc* CompressFunc_Func;
DecompressFunc* DecompressFunc_Func;
CompressFunc_Func = (CompressFunc*)GetProcAddress(dll_object, compress_function_name.c_str());
DecompressFunc_Func = (DecompressFunc*)GetProcAddress(dll_object, decompress_function_name.c_str());
cout<<bool(CompressFunc_Func)<<" "<<bool(DecompressFunc_Func)<<endl;
ifstream fin("0.decompressed", ios::binary);
cout<<fin.good()<<endl; // Prints 1
fin.seekg(0, ios::end);
uint64_t decompressed_size = fin.tellg();
cout<<decompressed_size<<endl; //Prints 558, correct
fin.seekg(0);
cout<<fin.tellg()<<endl; // Prints 0
cout.flush();
char* decompressed_data = new char [decompressed_size];
fin.read(decompressed_data, decompressed_size);
cout<<"Read Data"<<endl;
cout.flush();
char* compressed_data;
compressed_data = new char [decompressed_size + 0x10000]; //Upper Bound
int64_t compressed_size = CompressFunc_Func(7, decompressed_data, decompressed_size, compressed_data, 7, 0, 0);
cout<<"Compressed Successfully"<<endl;
cout<<"Compressed Size: "<<compressed_size<<endl; // Prints 225
cout.flush();
for (int i = 0; i < 4; i++)
{
cout<<hex<<(uint16_t)compressed_data[i]<<" ";
}
cout<<dec<<endl;
delete [] decompressed_data;
cout<<"Still Deco Size: "<<decompressed_size<<endl; //Prints 558, correct
char* x = new char [decompressed_size]; // Freezes here then stops
//DecompressFunc_Func(compressed_data, compressed_size, decompressed_data, decompressed_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
Output:
0
1 1
1
558
0
Read Data
When I comment the memory allocation at the bottom, Output:
0
1 1
1
558
0
Read Data
Compressed Successfully
Compressed Size: 225
ff8c 5 40 ffdc
Still Deco Size: 558
Running GDB debugger produces a Segmentation Fault at the Compress function only if the memory allocation exists afterwards.
Upvotes: 1
Views: 226
Reputation: 1248
There was a mismatch between the documentation I have and the actual version of the DLL I'm using. I opened up the DLL in IDA Pro and found that there's an extra parameter of the Compress Function call that I was missing.
Adding the parameter showed me that there were 2 additional parameters so the function would end up needing: int64_t ununsed, void* scratch, int64_t scratch_size
.
Upvotes: 1