RemoveDllImportHook - Remove a hook installed with InstallDllImportHook

Top  Previous  Next

Syntax

 

<lretVal> := RemoveDllImportHook( <aHookInfo> )

 

Parameters

 

<aHookInfo>

 

Array that was returned by InstallDllImportHook.

 

 

Return value

 

If <aHookInfo> is not an array, .F. is returned. Otherwise .T. is returned.

 

Description

 

RemoveDllImportHook removes a hook that was installed with InstallDllImportHook.

 

Please note that an import table hooks affects all threads in a process. The hook may be removed at any time, but the callback may not be released unless you are sure that no thread is currently using it.

 

Classification

 

Core

 

Category

 

Hook

 

Quick Info

 

Library: cckptcor.lib /cckptcor.dll

Header: DllImportHook.ch

 

See also

 

InstallDllImportHook

 

Example

 

 

* The Cockpit System

* (c) 2003-2004 Michael Hoffmann

* All Rights reserved

*

* HookAPI.PRG

* Sample program showing how to hook API Functions

*

 

#INCLUDE "CockpitPlatformLibs.ch"

#INCLUDE "Windows.ch"

#INCLUDE "DllImportHook.ch"

 

STATIC originalAddress

 

FUNCTION main

 

LOCAL hookInfo,envValue

 

* Hook any call from "xpprt1.dll" to "GetEnvironmentVariableA" in dll "kernel32.dll"

* to the callback function "MyHookFunc"

hookInfo := InstallDllImportHook("xpprt1.dll","GetEnvironmentVariableA",;

                                "kernel32.dll",MyHookFunc())

 

* That didn't work?

IF hookInfo == NIL

  MessageBox(0,"Hooking failed")

  CANCEL

ENDIF

 

* Save the original address for passsing on the call to the original function

originalAddress := hookInfo[DLLHOOKINFO_INDEX_ORIGINALPROCADDRESS]

 

MessageBox(0,"Hooking successful.")

 

* HOOKINFO is our special value

envValue := GETENV("HOOKINFO")

 

* Display what we received

MessageBox(0,SVI("Returned value for HOOKINFO: &1",envValue))

 

* Any other value should return the real value of the environment variable

envValue := GETENV("SystemRoot")

 

* Display what we received

MessageBox(0,SVI("Returned value for SystemRoot: &1",envValue))

 

* Now remove the Hook

RemoveDllImportHook(hookInfo)

MessageBox(0,"Hook removed.")

 

* Now let's see what GETENV returns

envValue := GETENV("HOOKINFO")

 

* Display what we received (should be empty this time but it isn't, hence

* Xbase++ caches environment variables!)

MessageBox(0,SVI("Returned value for HOOKINFO after unhooking: &1",envValue))

 

RETURN NIL

 

 

 

* This function is called instead of the original GetEnvironmentVariableA function

CALLBACK FUNCTION MyHookFunc(p1,p2,p3)

 

* Display the passed parameters

MessageBox(0,SVI("GetEnvironmentVariableA(&1,&2,&3)",CopyMemoryToString(p1),p2,p3),;

          "Hook function called")

 

* Handle our special case...

IF CopyMemoryToString(p1) == "HOOKINFO"

 

  * Size Request?

  IF p3 == 0

     RETURN 11

  ENDIF

 

  * Return our special result value

  WriteMemory(p2,"Holy Moly!"+CHR(0),11)

 

  * Return the size

  RETURN 10

 

ENDIF

 

* Pass the call on to the original function

RETURN CallAddress(originalAddress,p1,p2,p3)