Ignore:
Timestamp:
Jan 5, 2003, 1:31:26 PM (23 years ago)
Author:
sandervl
Message:

added dll load hook and function to override named or ordinal exports

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/winimagepeldr.cpp

    r9537 r9617  
    1 /* $Id: winimagepeldr.cpp,v 1.102 2002-12-20 11:39:42 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.103 2003-01-05 12:31:25 sandervl Exp $ */
    22
    33/*
     
    4444#include <win32api.h>
    4545#include <heapcode.h>
     46#include <custombuild.h>
    4647#include "winimagebase.h"
    4748#include "winimagepeldr.h"
     
    13721373}
    13731374//******************************************************************************
     1375//Install a hook that gets called when the exports have been processed
     1376//******************************************************************************
     1377static ODINPROC_DLLLOAD pfnDllLoad = NULL;
     1378//******************************************************************************
     1379BOOL WIN32API ODIN_SetDllLoadCallback(ODINPROC_DLLLOAD pfnMyDllLoad)
     1380{
     1381    pfnDllLoad = pfnMyDllLoad;
     1382    return TRUE;
     1383}
     1384//******************************************************************************
    13741385//******************************************************************************
    13751386BOOL Win32PeLdrImage::processExports(char *win32file)
     
    14371448        }
    14381449    }
     1450  }
     1451
     1452  //Call the dll load hook; must be done here so noone has the opportunity
     1453  //to use this dll (get exports)
     1454  if(pfnDllLoad) {
     1455      pfnDllLoad(hinstance);
    14391456  }
    14401457
     
    19151932//******************************************************************************
    19161933//******************************************************************************
    1917 ULONG Win32PeLdrImage::getApi(char *name)
     1934NameExport *Win32PeLdrImage::findApi(char *name)
    19181935{
    19191936  ULONG       apiaddr, i, apilen;
     
    19401957        {
    19411958            if(strcmp(curexport->name, apiname) == 0)
    1942                 return(curexport->virtaddr);
     1959                return curexport;
    19431960        }
    19441961        curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
    19451962    }
    1946     return(0);
    1947 }
    1948 //******************************************************************************
    1949 //******************************************************************************
    1950 ULONG Win32PeLdrImage::getApi(int ordinal)
     1963    return NULL;
     1964}
     1965//******************************************************************************
     1966//******************************************************************************
     1967ULONG Win32PeLdrImage::getApi(char *name)
     1968{
     1969    NameExport *curexport;
     1970
     1971    curexport = findApi(name);
     1972    if(curexport) {
     1973        return(curexport->virtaddr);
     1974    }
     1975    return 0;
     1976}
     1977//******************************************************************************
     1978//Override a name export
     1979//******************************************************************************
     1980ULONG Win32PeLdrImage::setApi(char *name, ULONG pfnNewProc)
     1981{
     1982    NameExport *curexport;
     1983
     1984    curexport = findApi(name);
     1985    if(curexport) {
     1986        ULONG pfnOldProc = curexport->virtaddr;
     1987
     1988        curexport->virtaddr = pfnNewProc;
     1989        return pfnOldProc;
     1990    }
     1991    return -1;
     1992}
     1993//******************************************************************************
     1994//******************************************************************************
     1995OrdExport *Win32PeLdrImage::findApi(int ordinal)
    19511996{
    19521997 ULONG       apiaddr, i;
    19531998 OrdExport  *curexport;
    1954  NameExport *nexport;
    19551999
    19562000    curexport = ordexports;
     
    19782022    else
    19792023      if (iThisExport == ordinal)   // found the export?
    1980         return curexport[i].virtaddr;
     2024        return &curexport[i];
    19812025      else
    19822026        i -= min(iStep, (iThisExport-ordinal));                 // move farther up the list
     
    20032047          iThisExport = curexport[i].ordinal;
    20042048          if(iThisExport == ordinal)
    2005             return(curexport[i].virtaddr);
     2049            return &curexport[i];
    20062050          else
    20072051            if (iThisExport > ordinal)
     
    20182062          iThisExport = curexport[i].ordinal;
    20192063          if(curexport[i].ordinal == ordinal)
    2020             return(curexport[i].virtaddr);
     2064            return &curexport[i];
    20212065          else
    20222066            if (iThisExport < ordinal)
     
    20302074    }
    20312075  }
     2076  return NULL;
     2077}
     2078//******************************************************************************
     2079//******************************************************************************
     2080ULONG Win32PeLdrImage::getApi(int ordinal)
     2081{
     2082    OrdExport  *curexport;
     2083    NameExport *nexport;
     2084
     2085    curexport = findApi(ordinal);
     2086    if(curexport) {
     2087        return curexport->virtaddr;
     2088    }
    20322089
    20332090    //Name exports also contain an ordinal, so check this
    20342091    nexport = nameexports;
    2035     for(i=0;i<nrNameExports;i++) {
     2092    for(int i=0;i<nrNameExports;i++) {
    20362093        if(nexport->ordinal == ordinal)
    20372094            return(nexport->virtaddr);
     
    20402097    }
    20412098    return(0);
     2099}
     2100//******************************************************************************
     2101//Override an ordinal export
     2102//******************************************************************************
     2103ULONG Win32PeLdrImage::setApi(int ordinal, ULONG pfnNewProc)
     2104{
     2105    OrdExport  *curexport;
     2106    NameExport *nexport;
     2107
     2108    curexport = findApi(ordinal);
     2109    if(curexport) {
     2110        ULONG pfnOldProc = curexport->virtaddr;
     2111
     2112        curexport->virtaddr = pfnNewProc;
     2113        return pfnOldProc;
     2114    }
     2115
     2116    //Name exports also contain an ordinal, so check this
     2117    nexport = nameexports;
     2118    for(int i=0;i<nrNameExports;i++)
     2119    {
     2120        if(nexport->ordinal == ordinal) {
     2121            ULONG pfnOldProc = nexport->virtaddr;
     2122
     2123            nexport->virtaddr = pfnNewProc;
     2124            return pfnOldProc;
     2125        }
     2126
     2127        nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
     2128    }
     2129    return -1;
    20422130}
    20432131//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.