堆栈溢出,当我使用 Detours 拦截 CreateFileW

stack overflow, when i using Detours to intercept CreateFileW

我想拦截 win32 api CreateFileW,但遇到错误“堆栈溢出”。我不知道发生了什么,有人可以帮助我吗?

错误: 在 detoursExample.exe 中的 0x00007FFA76204170 (KernelBase.dll) 抛出异常:0xC00000FD:堆栈溢出(参数:0x0000000000000001、0x00000094A6A03FF0)。 detoursExample.exe 中 0x00007FFA76204170 (KernelBase.dll) 的未处理异常:0xC00000FD:堆栈溢出(参数:0x0000000000000001、0x00000094A6A03FF0)。

#include <Windows.h>
#include <string>
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

#include"detours.h"  
#pragma comment(lib,"detours.lib") 

void myCreateFile(LPCWSTR pathName) {
    // Open a handle to the file
    HANDLE hFile = CreateFileW(
        pathName, // L".\NewFile.txt",     // Filename
        GENERIC_WRITE,          // Desired access
        FILE_SHARE_READ,        // Share mode
        NULL,                   // Security attributes
        CREATE_NEW,             // Creates a new file, only if it doesn't already exist
        FILE_ATTRIBUTE_NORMAL,  // Flags and attributes
        NULL);                  // Template file handle

    if (hFile == INVALID_HANDLE_VALUE)
    {
        // Failed to open/create file
        throw("failed to open/create file\n");
        return ;
    }

    // Write data to the file
    std::string strText = "Hello World!"; // For C use LPSTR (char*) or LPWSTR (wchar_t*)
    DWORD bytesWritten;
    WriteFile(
        hFile,            // Handle to the file
        strText.c_str(),  // Buffer to write
        strText.size(),   // Buffer size
        &bytesWritten,    // Bytes written
        nullptr);         // Overlapped

     // Close the handle once we don't need it.
    CloseHandle(hFile);
}

                                      
HANDLE (*oldCreateFile)(LPCWSTR,    
     DWORD,
     DWORD,
     LPSECURITY_ATTRIBUTES,
     DWORD,
     DWORD,
     HANDLE) = CreateFileW;

HANDLE WINAPI newCreateFile(
    _In_ LPCWSTR lpFileName,
    _In_ DWORD dwDesiredAccess,
    _In_ DWORD dwShareMode,
    _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    _In_ DWORD dwCreationDisposition,
    _In_ DWORD dwFlagsAndAttributes,
    _In_opt_ HANDLE hTemplateFile
) {
    printf("hook success!\n");
    return CreateFileW(
        //L".\newFiles.txt", // L".\NewFile.txt",     // Filename
        lpFileName,
        dwDesiredAccess,          // Desired access
        dwShareMode,        // Share mode
        lpSecurityAttributes,                   // Security attributes
        dwCreationDisposition,             // Creates a new file, only if it doesn't already exist
        dwFlagsAndAttributes,  // Flags and attributes
        hTemplateFile);
}

void hook() {
    DetourRestoreAfterWith();                              
    DetourTransactionBegin();                             
    DetourUpdateThread(GetCurrentThread());                
    //DetourAttach((void**)&poldsystem, newsystem);         
    DetourAttach((void**)&oldCreateFile, newCreateFile);         
    DetourTransactionCommit();                             
}

int main() {
    hook();
    myCreateFile(L".\text.txt");
    getchar();
    return 0;

}

newCreateFile中,您需要调用oldCreateFile,而不是CreateFileW。你这样做的方式,你的钩子最终会永远调用自己。