【提问】MS Detour3.0 注入Direct sound 截获的API只一次

Gary@Tokyo 2011-10-11 10:46:06

用MS Detours ,注入是成功的,recv可以多次截获,但是timeSetEvent和waveOutWrite就只一次

代码如下


// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <stdlib.h>
#include <tchar.h>
#include <unknwn.h>
#include <cstdio>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <detours.h>
#include <Mmsystem.h>

#pragma comment(lib,"../lib/detours.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Winmm.lib")


//Prototypes
int (WINAPI *pRecv)(SOCKET s, char* buf, int len, int flags) = recv;
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);

MMRESULT (WINAPI *pWaveOutWrite)( HWAVEOUT hwo, LPWAVEHDR pwh, UINT cbwh ) = waveOutWrite;
MMRESULT WINAPI MyWaveOutWrite( HWAVEOUT hwo, LPWAVEHDR pwh, UINT cbwh );

MMRESULT (WINAPI *pTimeSetEvent)(UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent)=timeSetEvent;
MMRESULT WINAPI MyTimeSetEvent(UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent);




//Log files
FILE* pRecvLogFile;
FILE* pWaveOut;




//Open file, write contents, close it
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
//OutputDebugString( _T("#_MyRecv_invoke successfully!"));
char* pFileName = "C:\\RecvLog.txt";
char* pType = "a+";
char* pFormat = "%s\n";
fopen_s(&pRecvLogFile, pFileName, pType);
fprintf(pRecvLogFile, pFormat, buf);
fclose(pRecvLogFile);
return pRecv(s, buf, len, flags);
}


MMRESULT WINAPI MyWaveOutWrite( HWAVEOUT hwo, LPWAVEHDR pwh, UINT cbwh )
{
//MessageBox(NULL, "MyWabveOutWrite"), "API"), MB_OK);
OutputDebugString( _T("#_MyWaveOutWrite_invoke successfully!"));

char* pFileName = "C:\\WaveOut.txt";
char* pType = "a+";
char* pFormat = "%s\n";
fopen_s(&pWaveOut, pFileName, pType);
fprintf(pWaveOut, pFormat, pwh->lpData);
fclose(pWaveOut);

return pWaveOutWrite(hwo, pwh, cbwh);
}


MMRESULT WINAPI MyTimeSetEvent(UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent)
{
OutputDebugString( _T("#_MyTimeSetEvent_invoke successfully!"));

return pTimeSetEvent(uDelay, uResolution, lpTimeProc, dwUser,fuEvent);
}









BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{

switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
OutputDebugString( _T("#_DllMain function_DLL_PROCESS_ATTACH!"));

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pRecv, MyRecv);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString( _T("#_recv() detoured"));

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pWaveOutWrite, MyWaveOutWrite);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString( _T("#_MyWaveOutWrite() detoured Attach"));


DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pTimeSetEvent, MyTimeSetEvent);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString( _T("#_MyTimeSetEvent() detoured Attach"));

}
break;
case DLL_PROCESS_DETACH:

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)pRecv, MyRecv);
DetourTransactionCommit();

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)pWaveOutWrite, MyWaveOutWrite);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString( _T("#_MyWaveOutWrite() detoured Detach"));


DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)pTimeSetEvent, MyTimeSetEvent);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString( _T("#_MyTimeSetEvent() detoured Attach"));

break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}


...全文
119 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljn398431 2011-10-11
  • 打赏
  • 举报
回复
帮顶下

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧