1
0
mirror of https://gitea.tendokyu.moe/Dniel97/segatools.git synced 2024-11-11 23:17:08 +01:00

Add DLL binding helper

This commit is contained in:
Tau 2021-05-23 11:39:33 -04:00
parent edd26aafe7
commit 10f2042bee
3 changed files with 82 additions and 0 deletions

47
util/dll-bind.c Normal file
View File

@ -0,0 +1,47 @@
#include <windows.h>
#include <assert.h>
#include <stdint.h>
#include "util/dll-bind.h"
#include "util/dprintf.h"
HRESULT dll_bind(
void *dest,
HINSTANCE src,
const struct dll_bind_sym **syms_pos,
size_t syms_count)
{
HRESULT hr;
void *src_result;
void **dest_field;
const struct dll_bind_sym *current_sym;
size_t i;
assert(dest != NULL);
assert(src != NULL);
assert(syms_pos != NULL);
hr = S_OK;
current_sym = *syms_pos;
assert(current_sym != NULL);
for (i = 0; i < syms_count; i++) {
src_result = GetProcAddress(src, current_sym->sym);
if (src_result == NULL) {
hr = E_NOTIMPL;
break;
}
dest_field = (void **)(((uint8_t *)dest) + current_sym->off);
*dest_field = src_result;
current_sym++;
}
*syms_pos = current_sym;
return hr;
}

33
util/dll-bind.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <windows.h>
#include <stdlib.h>
struct dll_bind_sym {
/* Symbol name to locate in the source DLL */
const char *sym;
/* Offset where the symbol pointer should be written in the target
structure */
ptrdiff_t off;
};
/*
Bind a list of DLL symbols into a structure that contains function
pointers.
- dest: Pointer to destination structure.
- src: Handle to source DLL.
- syms_pos: Pointer to a (mutable) pointer which points to the start of an
(immutable) table of symbols and structure offsets. This mutable
pointer is advanced until either a symbol fails to bind, in which case
an error is returned, or the end of the table is reached, in which
case success is returned.
- syms_count: Number of entries in the symbol table.
*/
HRESULT dll_bind(
void *dest,
HINSTANCE src,
const struct dll_bind_sym **syms_pos,
size_t syms_count);

View File

@ -11,6 +11,8 @@ util_lib = static_library(
'async.h',
'crc.c',
'crc.h',
'dll-bind.c',
'dll-bind.h',
'dprintf.c',
'dprintf.h',
'dump.c',