我最近发布了有关使用DLL函数的问题,并且自从我试图缩小问题的范围以来。我重新创建了代码部分,仅包括DLL部分,这似乎是问题所在。该库是一个64位的库,我正在使用Mingw64-g ++进行编译。下面的代码是加载dll,加载库函数,然后使用它们。使用库函数可以正常工作,但是在我有机会做任何事情之前,为随后的调用分配内存失败(崩溃)。这是我当前拥有的代码。
#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);
}
输出:
0
1 1
1
558
0
Read Data
当我在底部评论内存分配时,输出:
0
1 1
1
558
0
Read Data
Compressed Successfully
Compressed Size: 225
ff8c 5 40 ffdc
Still Deco Size: 558
仅在此后才存在内存分配的情况下,运行GDB调试器才会在Compress函数上产生分段错误。
我拥有的文档与我正在使用的DLL的实际版本之间不匹配。我在IDA Pro中打开DLL,发现我缺少“压缩函数”调用的一个额外参数。
添加参数后,我发现还有2个附加参数,因此该函数最终需要使用int64_t ununsed, void* scratch, int64_t scratch_size
。