# CreateDetourAtDLLEntry
{: .no_toc }

Detours a function of a loaded DLL, found by the names of the DLL and the function, and returns the address that calls the original function.

Syntax: **CreateDetourAtDLLEntry** ( *libName*, *funcName*, *newAddress* ) **As LongPtr**

*libName*
: *required* A **String**, the name of a DLL that the process has already loaded, such as `"user32"` or `"user32.dll"`. A name without an extension means a `.dll` file.

*funcName*
: *required* A **String**, the name under which the DLL exports the function, such as `"SetFocus"` or `"MessageBoxW"`. The name is case-sensitive.

*newAddress*
: *required* A **LongPtr**, the address of the detour, usually **AddressOf** and the name of a procedure in a module.

Returns the address of the trampoline, which calls the original function. It is not the function's own address: a call to the function goes to the detour, and a call through the returned address runs the original. Keep the address for as long as the detour is in place, convert it to a [**Delegate**](/twinBASIC-docs/tB/Core/Delegate.md) type to call the original, and pass it to [**RemoveDetour**](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/Detours/RemoveDetour.md) to remove the detour.

The function finds the DLL with the **GetModuleHandle** API and the function with **GetProcAddress**, then places the detour with [**SlimDetoursInlineHook**](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/DetoursMod/SlimDetoursInlineHook.md), as [**CreateDetourAtFuncAddress**](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/Detours/CreateDetourAtFuncAddress.md) does. It does not load the DLL.

> [!NOTE] 1.0.0.0
> The DLL must already be loaded in the process, or the function raises error 5, `CreateDetourAtDLLEntry libName: DLL library not found`. To detour a function of a DLL that may not be loaded yet, load the DLL first with the **LoadLibrary** API.

[Errors](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/Detours/index.md#errors) lists the errors the function raises.

## Example

This module detours **SetFocus** of `user32` so that the program can see which window each call gives the focus to.

```tb check_build
Private Delegate Function SetFocusFn (ByVal hWnd As LongPtr) As LongPtr
Private RealSetFocus As SetFocusFn

Private Function MySetFocus(ByVal hWnd As LongPtr) As LongPtr
    Debug.Print "SetFocus &H"; Hex(hWnd)
    Return RealSetFocus(hWnd)
End Function

Public Sub WatchFocus()
    RealSetFocus = CType(Of SetFocusFn)(CreateDetourAtDLLEntry("user32", "SetFocus", AddressOf MySetFocus))
End Sub

Public Sub StopWatchingFocus()
    RemoveDetour RealSetFocus, AddressOf MySetFocus
End Sub
```

## See Also

- [CreateDetourAtFuncAddress](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/Detours/CreateDetourAtFuncAddress.md) function -- detours the function at an address
- [RemoveDetour](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/Detours/RemoveDetour.md) procedure
- [Writing a detour](/twinBASIC-docs/tB/Packages/Contributed/DetoursLib/index.md#writing-a-detour)
