2013-08-21 5 views
1

Я пытался подключить несколько функций winapi, например. DrawText, TextOut, ExtTextOut с использованием обходных путей. В случае инъекции моей dll в калькулятор окон, функции зацепления работают нормально, пока я не нажму кнопку на калькуляторе, а затем вызовет сбой. Включение моей DLL в другие процессы вызывает подобное поведение. Все перехватчики работают нормально, пока я не запускаю определенные действия, такие как открытие диалогового окна нового файла в блокноте.Сбой приложения при использовании подключенных функций

Я протестировал свою dll на 32-разрядных и 64-битных системах Windows 7, оба показывают одинаковое поведение.

Любые идеи о том, что может быть причиной проблемы?

Мой длл:

main.cpp

#include <Windows.h> 
#include <detours.h> 
#include "hookedFunctions.h" 


BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 
{ 
    switch (ul_reason_for_call){  
     case DLL_PROCESS_ATTACH: 
      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 
      DetourAttach(&(PVOID&)pDrawTextW, myDrawTextW); 
      DetourAttach(&(PVOID&)pDrawTextA, myDrawTextA); 
      DetourAttach(&(PVOID&)pExtTextOutW, myExtTextOutW); 
      DetourAttach(&(PVOID&)pExtTextOutA, myExtTextOutA); 
      DetourAttach(&(PVOID&)pTextOutW, myTextOutW); 
      DetourAttach(&(PVOID&)pTextOutA, myTextOutA); 
      DetourAttach(&(PVOID&)myPolyTextOutW, myPolyTextOutW); 
      DetourAttach(&(PVOID&)myPolyTextOutA, myPolyTextOutA); 
      if(DetourTransactionCommit() == NO_ERROR) 
       OutputDebugStringA("Detoured successfully"); 

      break; 
     case DLL_PROCESS_DETACH: 

      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 

      DetourDetach(&(PVOID&)pDrawTextW, myDrawTextW); 
      DetourDetach(&(PVOID&)pDrawTextA, myDrawTextA); 
      DetourDetach(&(PVOID&)pExtTextOutW, myExtTextOutW); 
      DetourDetach(&(PVOID&)pExtTextOutA, myExtTextOutA); 
      DetourDetach(&(PVOID&)pTextOutW, myTextOutW); 
      DetourDetach(&(PVOID&)pTextOutA, myTextOutA); 
      DetourDetach(&(PVOID&)myPolyTextOutW, myPolyTextOutW); 
      DetourDetach(&(PVOID&)myPolyTextOutA, myPolyTextOutA); 
      if(DetourTransactionCommit() == NO_ERROR) 
       OutputDebugStringA("Detoured successfully"); 

      break; 
    } 
    return TRUE; 
} 

hookedFunctions.h

#pragma once 

#include <Windows.h> 

// Declare function pointers to original Windows API functions 
extern int (WINAPI *pDrawTextW)(HDC, LPCTSTR, int, LPRECT, UINT); 
extern int (WINAPI *pDrawTextA)(HDC, LPCSTR, int, LPRECT, UINT); 
extern BOOL (WINAPI *pTextOutW)(HDC, int, int, LPCTSTR, int); 
extern BOOL (WINAPI *pTextOutA)(HDC, int, int, LPCSTR, int); 
extern BOOL (WINAPI *pExtTextOutW)(HDC, int, int, UINT, const RECT*, LPCTSTR, UINT, const INT*); 
extern BOOL (WINAPI *pExtTextOutA)(HDC, int, int, UINT, const RECT*, LPCSTR, UINT, const INT*); 
extern BOOL (WINAPI *pPolyTextOutW)(HDC, const POLYTEXTW* , int); 
extern BOOL (WINAPI *pPolyTextOutA)(HDC, const POLYTEXTA* , int); 


// Declare our custom functions which are used to override the original Windows API functions 
int myDrawTextW(HDC, LPCTSTR, int, LPRECT, UINT); 
int myDrawTextA(HDC, LPCSTR, int, LPRECT, UINT); 
BOOL myTextOutW(HDC, int, int, LPCTSTR, int); 
BOOL myTextOutA(HDC, int, int, LPCSTR, int); 
BOOL myExtTextOutW(HDC, int, int, UINT, const RECT*, LPCTSTR , UINT, const INT*); 
BOOL myExtTextOutA(HDC, int, int, UINT, const RECT*, LPCSTR , UINT, const INT*); 
BOOL myPolyTextOutW(HDC, const POLYTEXTW*, int); 
BOOL myPolyTextOutA(HDC, const POLYTEXTA*, int); 

hookedFunctions.cpp

#include "hookedFunctions.h" 


// Create and initialize function pointers to original Windows API functions 
int (WINAPI *pDrawTextW)(HDC, LPCTSTR, int, LPRECT, UINT) = DrawTextW; 
int (WINAPI *pDrawTextA)(HDC, LPCSTR, int, LPRECT, UINT) = DrawTextA; 
BOOL (WINAPI *pTextOutW)(HDC, int, int, LPCTSTR, int) = TextOutW; 
BOOL (WINAPI *pTextOutA)(HDC, int, int, LPCSTR, int) = TextOutA; 
BOOL (WINAPI *pExtTextOutW)(HDC, int, int, UINT, const RECT*, LPCTSTR, UINT, const INT*) = ExtTextOutW; 
BOOL (WINAPI *pExtTextOutA)(HDC, int, int, UINT, const RECT*, LPCSTR, UINT, const INT*) = ExtTextOutA; 
BOOL (WINAPI *pPolyTextOutW)(HDC, const POLYTEXTW* , int) = PolyTextOutW; 
BOOL (WINAPI *pPolyTextOutA)(HDC, const POLYTEXTA* , int) = PolyTextOutA; 

// Custom versions of the Windows API functions, having the same parameters, 
// return type, and calling convention as the versions provided by the OS. 
int myDrawTextW(HDC hDC, LPCTSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat) 
{ 
    OutputDebugString(lpchText); 
    return pDrawTextW(hDC, lpchText, nCount, lpRect, uFormat); 
} 

int myDrawTextA(HDC hDC, LPCSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat) 
{ 
    OutputDebugStringA(lpchText); 
    return pDrawTextA(hDC, lpchText, nCount, lpRect, uFormat); 
} 


BOOL myTextOutW(HDC hdc, int nXStart, int nYStart, LPCTSTR lpString, int cchString) 
{ 
    OutputDebugString(lpString); 
    return pTextOutW(hdc, nXStart, nYStart, lpString, cchString); 
} 

BOOL myTextOutA(HDC hdc, int nXStart, int nYStart, LPCSTR lpString, int cchString) 
{ 
    OutputDebugStringA(lpString); 
    return pTextOutA(hdc, nXStart, nYStart, lpString, cchString); 
} 

BOOL myExtTextOutW(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, 
        LPCTSTR lpString, UINT cbCount, const INT *lpDx) 
{ 
    OutputDebugString(lpString); 
    return pExtTextOutW(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx); 
} 

BOOL myExtTextOutA(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, 
        LPCSTR lpString, UINT cbCount, const INT *lpDx) 
{ 
    OutputDebugStringA(lpString); 
    return pExtTextOutA(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx); 
} 

BOOL myPolyTextOutW(HDC hdc, const POLYTEXTW *pptxt, int cStrings) 
{ 
    OutputDebugString(pptxt->lpstr); 
    return pPolyTextOutW(hdc, pptxt, cStrings); 
} 

BOOL myPolyTextOutA(HDC hdc, const POLYTEXTA *pptxt, int cStrings) 
{ 
    OutputDebugStringA(pptxt->lpstr); 
    return pPolyTextOutA(hdc, pptxt, cStrings); 
} 
+0

Вы должны добавить свое решение в качестве ответа и принять его, чтобы отметить этот вопрос как решение. – Spooky

ответ

2

Проблема решена - я забыл добавить WINAPI к своим функциям. Заголовки должны выглядеть так: int WINAPI myDrawTextW (HDC, LPCTSTR, int, LPRECT, UINT);

+0

спас меня от бесполезной отладки :) была ли эта проблема, забыли добавить WINAPI. Благодаря :) – Darxis

Смежные вопросы