15,471
社区成员
发帖
与我相关
我的任务
分享
// 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;
}