Run-Time Check Failure #0 - The value of ESP was not properl

c++ / delphi package - dll injection and api hooking
Post Reply
Protector
Posts: 3
Joined: Mon Nov 07, 2011 9:24 am

Run-Time Check Failure #0 - The value of ESP was not properl

Post by Protector »

Hi!

I am hooking CreateProcessA and CreateProcessW with madeCodeHook 3. The functions look like this:

Code: Select all

BOOL CreateProcessAHooked(
    __in_opt    LPCSTR lpApplicationName,
    __inout_opt LPSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOA lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
	)
{
	return CreateProcessAOriginal( lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation );
}
BOOL WINAPI CreateProcessWHooked(
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
	)
{
	return CreateProcessWOriginal( lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation );
}
Hooking looks like this:

Code: Select all

	HookAPI("kernel32.dll", "CreateProcessA", CreateProcessAHooked, (PVOID*) &CreateProcessAOriginal);
	HookAPI("kernel32.dll", "CreateProcessW", CreateProcessWHooked, (PVOID*) &CreateProcessWOriginal);
CreateProcessAOriginal and CreateProcessWOriginal look like this:

Code: Select all

BOOL (WINAPI *CreateProcessAOriginal)(
    __in_opt    LPCSTR lpApplicationName,
    __inout_opt LPSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOA lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
    );

BOOL (WINAPI *CreateProcessWOriginal)(
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
    );
As you can see the hooked functions just call the original functions and do nothing else. This is to keep it a very simple test case to avoid side effects from other code pieces. When I call CreateProcessW everything is fine. But when I call CreateProcessA, I get the following debug message followed by an appcrash:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Any ideas? Thanks!
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Run-Time Check Failure #0 - The value of ESP was not pro

Post by madshi »

You forgot the WINAPI in your CreateProcessAHooked declaration.
Protector
Posts: 3
Joined: Mon Nov 07, 2011 9:24 am

Re: Run-Time Check Failure #0 - The value of ESP was not pro

Post by Protector »

Oh my, that's embarrassing :O

Thanks for pointing it out, I guess it's true what people say about becoming blind when reading own code.
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Run-Time Check Failure #0 - The value of ESP was not pro

Post by madshi »

No worries, that's the single most often occuring madCodeHook programming error. Double checking the calling convention is always the first thing I do... :wink:
Post Reply