How to Fix ERROR_INVALID_BLOCK in Function Detours
Make sure that the function is large enough for the detour
3 min. read
Updated on
Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more
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
- 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.
- 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
- Make sure the variable pointed to by ppPointer is correctly initialized and not NULL.
- 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
- Begin a new transaction using DetourTransactionBegin().
- Attach the detour function using DetourAttach().
- 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
- Ensure there is enough memory available to complete the detour operation.
- 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
- Check the target function for any modifications or patches that might cause inconsistencies.
- 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)
- When dealing with COM or automation calls, errors like ERROR_INVALID_BLOCK might be due to an unexpected variable type.
- 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. You can also learn how to resolve a similar issue, CNSS_FILE_SYSTEM_FILTER.
Did you manage to fix the ERROR_INVALID_BLOCK issue? Let us know in the comments below.
User forum
0 messages