Files
BlitzNext/Linker/lib/dlltoexe.cpp
T

83 lines
1.6 KiB
C++
Raw Normal View History

2014-01-31 08:23:00 +13:00
2019-01-18 15:54:29 +01:00
#include "dlltoexe.hpp"
2019-01-18 17:03:57 +01:00
#include "std.hpp"
2019-01-18 15:54:29 +01:00
using namespace std;
2014-01-31 08:23:00 +13:00
2019-01-18 17:03:57 +01:00
#pragma pack(push, 1)
struct Head {
short machine, num_sects;
int timedata, sym_table, num_syms;
short opt_size, chars;
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:03:57 +01:00
struct Opt1 {
2014-01-31 08:23:00 +13:00
short magic;
2019-01-18 17:03:57 +01:00
char major, minor;
int code_size, data_size, udata_size;
int entry, code_base, data_base;
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:03:57 +01:00
struct Sect {
char name[8];
int virt_size, virt_addr; //in mem
int data_size, data_addr; //on disk
int relocs, lines; //file ptrs
short num_relocs, num_lines;
int chars;
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:03:57 +01:00
#pragma pack(pop)
2014-01-31 08:23:00 +13:00
2019-01-18 17:03:57 +01:00
bool dllToExe(const char* exe_file, const char* dll_file, const char* entry_func)
{
2014-01-31 08:23:00 +13:00
//find proc address of bbWinMain
2019-01-18 17:03:57 +01:00
HMODULE hmod = LoadLibrary(dll_file);
if (!hmod)
return false;
int proc = (int)GetProcAddress(hmod, entry_func);
int entry = proc - (int)hmod;
FreeLibrary(hmod);
if (!proc)
return false;
2014-01-31 08:23:00 +13:00
//Convert dll to exe
2019-01-18 17:03:57 +01:00
fstream in(dll_file, ios_base::binary | ios_base::in);
if (!in.is_open())
return false;
fstream out(exe_file, ios::binary | ios_base::out | ios_base::trunc);
if (!out.is_open())
return false;
2014-01-31 08:23:00 +13:00
int offs;
2019-01-18 17:03:57 +01:00
in.seekg(0x3c);
in.read((char*)&offs, 4);
2014-01-31 08:23:00 +13:00
//copy first bit...
2019-01-18 17:03:57 +01:00
in.seekg(0);
for (int k = 0; k < offs + 4; ++k)
out.put(in.get());
2014-01-31 08:23:00 +13:00
//reader file header
2019-01-18 17:03:57 +01:00
Head head = {0};
in.read((char*)&head, sizeof(head));
2014-01-31 08:23:00 +13:00
//change DLL to EXE
2019-01-18 17:03:57 +01:00
head.chars = 0x10e;
out.write((char*)&head, sizeof(head));
2014-01-31 08:23:00 +13:00
//read opts 1
2019-01-18 17:03:57 +01:00
Opt1 opt1 = {0};
in.read((char*)&opt1, sizeof(opt1));
opt1.entry = entry;
out.write((char*)&opt1, sizeof(opt1));
2014-01-31 08:23:00 +13:00
//copy rest of file...
2019-01-18 17:03:57 +01:00
while (!in.eof()) {
out.put(in.get());
2014-01-31 08:23:00 +13:00
}
out.close();
in.close();
return true;
}