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
XINSTALL BY CLICKING THE DOWNLOAD FILE
A message from our partner

For fixing Windows errors, we recommend Fortect:

Fortect will identify and deploy the correct fix for your Windows errors. Follow the 3 easy steps to get rid of Windows errors:

  • Download Fortect and install it on your PC
  • Launch the tool and Start scanning your PC for Windows errors
  • Right-click on Start Repair to deploy the right fix for each error encountered during the scan
Download Now Fortect has been downloaded by 0 readers this month, rated 4.6 on TrustPilot

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. 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.

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

User forum

0 messages