Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created October 19, 2016 03:02
Show Gist options
  • Select an option

  • Save toptensoftware/975dffc7d1645312552ff077fbb93ef2 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/975dffc7d1645312552ff077fbb93ef2 to your computer and use it in GitHub Desktop.
Creating thunks for exported methods
// Create thunks for all exported methods
foreach (var mi in this.GetType().GetMethods())
{
// Check it has an EntryPoint attribute
var ep = mi.GetCustomAttributes<EntryPointAttribute>().FirstOrDefault();
if (ep==null)
continue;
// Create a system thunk for it
var vmptr = _machine.CreateSystemThunk(() => {
// When thunk called, use reflection to call the method
mi.Invoke(this);
});
// Connect the ordinal and the thunk (GetProcAddress will
// return this ‘vmptr’ when asked for ordinal ‘ep.ordinal’)
_mapProcAddress[ep.ordinal] = vmptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment