How to Fix ERROR_INVALID_BLOCK in Function Detours

Make sure that the function is large enough for the detour

Reading time icon 3 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

How to fix ERROR_INVALID_BLOCK

Dealing with the ERROR_INVALID_BLOCK issue can be quite a headache for developers, especially when working with function detours. It usually comes accompanied by the 9 (0x9) The storage control block address is invalid error message.

How do I fix ERROR_INVALID_BLOCK?

1. Ensure the target function is large enough to be detoured

  1. Verify that the function you are trying to detour has enough memory space to accommodate the detour. The function should be sufficiently long, typically at least five bytes, because Detours needs to overwrite the start of the function with a jump instruction.
  2. If the function is too small, consider detouring a larger function that eventually calls the target function.

This ensures that the function is large enough for the detour to attach properly and prevents the ERROR_INVALID_BLOCK error from occurring.

2. Initialize the target function pointer correctly

  1. Make sure the variable pointed to byย ppPointerย is correctly initialized and notย NULL.
  2. Initialize the original function pointer only once before the hook to avoidย ERROR_INVALID_BLOCK.
BOOL(WINAPI* originFunc) (HDC, int, int, int, int, HDC, int, int, DWORD) = Bitblt;

This avoids the ERROR_INVALID_BLOCK by ensuring that the function pointer is set up correctly before attaching the detour.

3. Use DetourTransaction API correctly

  1. Begin a new transaction usingย DetourTransactionBegin().
  2. Attach the detour function usingย DetourAttach().
  3. Commit the transaction usingย DetourTransactionCommit().

Hereโ€™s an example of using DetourTransaction API:

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)originFunc, MyBitBlt);
DetourTransactionCommit();

This ensures that the detour is part of a transaction and helps in orderly attaching and detaching of detours, preventing errors.

4. Check for memory availability

  1. Ensure there is enough memory available to complete the detour operation.
  2. If memory is insufficient, you might need to optimize memory usage elsewhere in your application or allocate more resources.

Ensuring that there is enough memory available prevents the ERROR_INVALID_BLOCK error due to memory constraints.

5. Verify the integrity of the function being detoured

  1. Check the target function for any modifications or patches that might cause inconsistencies.
  2. Ensure that the function code is intact and not altered by other hooks or debugging tools.

Ensuring the integrity of the target function prevents errors related to function size or corruption.

6. Handle special cases in automation calls (VBA/COM)

  1. When dealing with COM or automation calls, errors likeย ERROR_INVALID_BLOCKย might be due to an unexpected variable type.
  2. Ensure that you are passing the correct types of arguments expected by the function.

For example, using the correct type in a VBA call:

Dim strArg As String
strArg = "Expected Argument"
Call someFunction(strArg)

This method ensures that the argument types match the functionโ€™s expectations, preventing block errors.

By following these solutions, you can effectively address the ERROR_INVALID_BLOCK error in your detour operations and ensure smooth and error-free function hooking. Always remember to check your function sizes and memory availability to prevent such issues in the future.

Did you manage to fix the ERROR_INVALID_BLOCK issue? Let us know in the comments below.

More about the topics: Error Code, Programming tools and tips