microsoft / Detours

Detours is a software package for monitoring and instrumenting API calls on Windows. It is distributed in source code form.
MIT License
5.18k stars 1.01k forks source link

detours crash on DetourAttach #145

Open luca2125 opened 4 years ago

luca2125 commented 4 years ago

Hi,

I have used detours on 2 projects. In the first project work fine.

The second project crash always when I use DetourAttach:

// This row cause crash DetourAttach(&(LPVOID&)AddressOfHookDamageFunction, &HookDamageSub);

The second project is a 90% copy of the first project.

AddressOfHookDamageFunction = 0x72DD81 (this is stored in (ffback.ini)

Becouse is very hard understand why this happen. I attach the entere project: BattleZone CC.zip

just open:

dllmain.cpp

goto "Init_Detour" and you find the line with DetourAttach.

To be honest I have no idea about this situation and I not sure if this is a bug,

Can you please verify ?

luca2125 commented 4 years ago

HI,

I have solved the problem.

I have added:

Sleep(1000);

before call: DetourTransactionBegin();

However I suggest to manage the error (ex.. "Target Application not ready") becouse when a application crash is very hard to find the cause becouse can depend by too much factors.

sonyps5201314 commented 3 years ago

This problem will be automatically fixed, when PR 144 is merged, because it provides a new extension API: DetourTransactionBeginEx (BOOL fWait), it supports waiting for other Detour transactions to complete, but currently in order to maintain compatibility, DetourTransactionBegin only calls DetourTransactionBeginEx with the FALSE parameter , as below code:

LONG WINAPI DetourTransactionBegin()
{
     return DetourTransactionBeginEx(FALSE);
}

Perhaps the official can consider adding a fWait parameter to DetourTransactionBegin directly, as shown below: LONG WINAPI DetourTransactionBegin(_In_opt_ BOOL fWait) . Due to the suggestion in pull_request_template.md:

These changes introduce no known API breaks (changing the public facing functions return types, function parameters, renaming functions, etc.).

I didn't do that, but the official can really consider adding a parameter for stability.

luca2125 commented 3 years ago

Thank you !

I have another question:

Sometime when detour call the funciton crash and now always is easy to find the cause.

I suspect that IDA not always return the correct number of parameters or types, but the crash can also depend by other factiors.

My question is there is a way to add some managaed error exception ? For example eneble some log file that write the error ?

If no, can be a good idea if is possible do it in the future.

Thanks !

sonyps5201314 commented 3 years ago

You can define the DETOUR_DEBUG macro to enable DETOUR_TRACE and DETOUR_BREAK to indicate exceptions of Detours Internals. In the future, DETOUR_ASSERT/Detour_Assert can indicate more exceptions of Detours Internals, regardless of whether the DETOUR_DEBUG macro is defined.

luca2125 commented 3 years ago

thank you ! there is a sample sorcce code that use DETOUR_DEBUG / DETOUR_TRACE ? I like to see it.

sonyps5201314 commented 3 years ago

create a c++ source file named DTRS.cpp with below code:

#define DETOUR_DEBUG

#undef _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE
typedef long NTSTATUS;
#include <ntstatus.h>
#define WIN32_NO_STATUS
#define DETOURS_INTERNAL
#include "detours.h"
#include "detours.cpp"
#include "disasm.cpp"
#include "image.cpp"
#include "modules.cpp"
#include "creatwth.cpp"

include this file to your Detours sample project without precompiled header (.pch) file for this source file, then you will see your want.

luca2125 commented 3 years ago

thanks ! if I do it what happen when there is an detour error ?

sonyps5201314 commented 3 years ago

Output a message to visual studio debug output window or break at the place of fatal error.