很早以前网上找的一段代码,因为历史悠久已经找不到vb端的代码了,只有dll动态库的源码,最近想学这段代码,发现vb程序没有了。有能力帮我解决恢复vb端代码的重谢。
#include "windows.h"
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
#pragma data_seg(".shared")
int g_nLimitSpeed=0; // byte/s
int g_nTotalBytes=0;
#pragma data_seg()
#pragma comment(linker,"/Section:.shared,rws")
#define ASSERT(a,s) \
if(!(a))MessageBox(NULL, s, "ASSERT ERROR", MB_ICONSTOP);
#define MAKEJMPCODE(byTmp, dwbase, dwJmp) \
*byTmp=(char)0xE9; \
*(DWORD*)(byTmp+1)=(DWORD)(dwJmp) - (DWORD)(dwbase) - 5;
typedef int (PASCAL FAR *RECVFUNC)(SOCKET s, char FAR * buf, int len, int flags);
typedef int (PASCAL FAR *RECVFROMFUNC)(SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR *from, int FAR * fromlen);
typedef int (PASCAL FAR *SENDFUNC)(SOCKET s, const char FAR * buf, int len, int flags);
typedef int (PASCAL FAR *SENDTOFUNC)(SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR *to, int tolen);
void InitHook();
void UnHook();
void SetLimitSpeed(int nLimit);
void ExchangeCode(LPVOID dwAddress, DWORD dwSize, char *byCodeNew, char *byCodeSave);
int PASCAL FAR hooked_recv (SOCKET s, char FAR * buf, int len, int flags);
int PASCAL FAR hooked_recvfrom (SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR *from, int FAR * fromlen);
int PASCAL FAR hooked_sendto (SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR *to, int tolen);
int PASCAL FAR hooked_send (SOCKET s, const char FAR * buf, int len, int flags);
RECVFUNC precv=NULL;
RECVFROMFUNC precvfrom=NULL;
SENDFUNC psend=NULL;
SENDTOFUNC psendto=NULL;
char byCodeSaveRecv[5];
char byCodeSaveRecvFrom[5];
char byCodeSaveSend[5];
char byCodeSaveSendTo[5];
char byCodeJmpRecv[5];
char byCodeJmpRecvFrom[5];
char byCodeJmpSend[5];
char byCodeJmpSendTo[5];
HMODULE hDLLws32=NULL;
HMODULE hDLLThis=NULL;
void ExchangeCode(LPVOID dwAddress, DWORD dwSize, char *byCodeNew, char *byCodeSave)
{
DWORD dwOldProtect;
DWORD dwSizeWrite;
ASSERT(VirtualProtect((LPVOID)dwAddress, dwSize, PAGE_EXECUTE_READWRITE, &dwOldProtect),"VirtualProtect");
if(byCodeSave)
{
ReadProcessMemory((void*)-1, dwAddress, byCodeSave, dwSize, &dwSizeWrite);
ASSERT(dwSize==dwSizeWrite,"ReadProcessMemory");
}
WriteProcessMemory((void*)-1, dwAddress, byCodeNew, dwSize, &dwSizeWrite);
ASSERT(dwSize==dwSizeWrite,"WriteProcessMemory");
VirtualProtect((LPVOID)dwAddress, dwSize, dwOldProtect, &dwOldProtect);
}
void InitHook()
{
if(!g_nLimitSpeed)
{
char sFile[MAX_PATH+4];
GetModuleFileName(hDLLThis, sFile, MAX_PATH);
sFile[strlen(sFile)-4]='\0';
strcat(sFile, ".ini");
g_nLimitSpeed=GetPrivateProfileInt("WebLimit", "SpeedLimit", 0, sFile);
#ifdef _DEBUG
char str[128];
wsprintf(str, "%s\ng_nLimitSpeed:%d\n", sFile, g_nLimitSpeed);
MessageBox(NULL, str, NULL, NULL);
#endif
}
hDLLws32=LoadLibrary("ws2_32.dll");
precv=(RECVFUNC)GetProcAddress(hDLLws32, "recv");
precvfrom=(RECVFROMFUNC)GetProcAddress(hDLLws32, "recvfrom");
psend=(SENDFUNC)GetProcAddress(hDLLws32, "send");
psendto=(SENDTOFUNC)GetProcAddress(hDLLws32, "sendto");
MAKEJMPCODE(byCodeJmpSend, psend, hooked_send);
ExchangeCode(psend, 5, byCodeJmpSend, byCodeSaveSend);
MAKEJMPCODE(byCodeJmpRecv, precv, hooked_recv);
ExchangeCode(precv, 5, byCodeJmpRecv, byCodeSaveRecv);
}
void UnHook()
{
ExchangeCode((void*)send, 5, byCodeSaveSend, NULL);
ExchangeCode((void*)recv, 5, byCodeSaveRecv, NULL);
}
void SetLimitSpeed(int nLimit)
{
g_nLimitSpeed=nLimit;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDLLThis=(HMODULE)hModule;
InitHook();
#ifdef _DEBUG
MessageBox(NULL, "hooked.\n", NULL, NULL);
#endif
break;
case DLL_PROCESS_DETACH:
UnHook();
char str[32];
wsprintf(str, "%d", g_nTotalBytes);
char sFile[MAX_PATH+4];
GetModuleFileName(hDLLThis, sFile, MAX_PATH);
sFile[strlen(sFile)-4]='\0';
strcat(sFile, ".ini");
WritePrivateProfileString("WebLimit", "TotalBytes", str, sFile);
break;
}
return TRUE;
}
#include "windows.h"
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
#pragma data_seg(".shared")
int g_nLimitSpeed=0; // byte/s
int g_nTotalBytes=0;
#pragma data_seg()
#pragma comment(linker,"/Section:.shared,rws")
#define ASSERT(a,s) \
if(!(a))MessageBox(NULL, s, "ASSERT ERROR", MB_ICONSTOP);
#define MAKEJMPCODE(byTmp, dwbase, dwJmp) \
*byTmp=(char)0xE9; \
*(DWORD*)(byTmp+1)=(DWORD)(dwJmp) - (DWORD)(dwbase) - 5;
typedef int (PASCAL FAR *RECVFUNC)(SOCKET s, char FAR * buf, int len, int flags);
typedef int (PASCAL FAR *RECVFROMFUNC)(SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR *from, int FAR * fromlen);
typedef int (PASCAL FAR *SENDFUNC)(SOCKET s, const char FAR * buf, int len, int flags);
typedef int (PASCAL FAR *SENDTOFUNC)(SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR *to, int tolen);
void InitHook();
void UnHook();
void SetLimitSpeed(int nLimit);
void ExchangeCode(LPVOID dwAddress, DWORD dwSize, char *byCodeNew, char *byCodeSave);
int PASCAL FAR hooked_recv (SOCKET s, char FAR * buf, int len, int flags);
int PASCAL FAR hooked_recvfrom (SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR *from, int FAR * fromlen);
int PASCAL FAR hooked_sendto (SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR *to, int tolen);
int PASCAL FAR hooked_send (SOCKET s, const char FAR * buf, int len, int flags);
RECVFUNC precv=NULL;
RECVFROMFUNC precvfrom=NULL;
SENDFUNC psend=NULL;
SENDTOFUNC psendto=NULL;
char byCodeSaveRecv[5];
char byCodeSaveRecvFrom[5];
char byCodeSaveSend[5];
char byCodeSaveSendTo[5];
char byCodeJmpRecv[5];
char byCodeJmpRecvFrom[5];
char byCodeJmpSend[5];
char byCodeJmpSendTo[5];
HMODULE hDLLws32=NULL;
HMODULE hDLLThis=NULL;
void ExchangeCode(LPVOID dwAddress, DWORD dwSize, char *byCodeNew, char *byCodeSave)
{
DWORD dwOldProtect;
DWORD dwSizeWrite;
ASSERT(VirtualProtect((LPVOID)dwAddress, dwSize, PAGE_EXECUTE_READWRITE, &dwOldProtect),"VirtualProtect");
if(byCodeSave)
{
ReadProcessMemory((void*)-1, dwAddress, byCodeSave, dwSize, &dwSizeWrite);
ASSERT(dwSize==dwSizeWrite,"ReadProcessMemory");
}
WriteProcessMemory((void*)-1, dwAddress, byCodeNew, dwSize, &dwSizeWrite);
ASSERT(dwSize==dwSizeWrite,"WriteProcessMemory");
VirtualProtect((LPVOID)dwAddress, dwSize, dwOldProtect, &dwOldProtect);
}
void InitHook()
{
if(!g_nLimitSpeed)
{
char sFile[MAX_PATH+4];
GetModuleFileName(hDLLThis, sFile, MAX_PATH);
sFile[strlen(sFile)-4]='\0';
strcat(sFile, ".ini");
g_nLimitSpeed=GetPrivateProfileInt("WebLimit", "SpeedLimit", 0, sFile);
#ifdef _DEBUG
char str[128];
wsprintf(str, "%s\ng_nLimitSpeed:%d\n", sFile, g_nLimitSpeed);
MessageBox(NULL, str, NULL, NULL);
#endif
}
hDLLws32=LoadLibrary("ws2_32.dll");
precv=(RECVFUNC)GetProcAddress(hDLLws32, "recv");
precvfrom=(RECVFROMFUNC)GetProcAddress(hDLLws32, "recvfrom");
psend=(SENDFUNC)GetProcAddress(hDLLws32, "send");
psendto=(SENDTOFUNC)GetProcAddress(hDLLws32, "sendto");
MAKEJMPCODE(byCodeJmpSend, psend, hooked_send);
ExchangeCode(psend, 5, byCodeJmpSend, byCodeSaveSend);
MAKEJMPCODE(byCodeJmpRecv, precv, hooked_recv);
ExchangeCode(precv, 5, byCodeJmpRecv, byCodeSaveRecv);
}
void UnHook()
{
ExchangeCode((void*)send, 5, byCodeSaveSend, NULL);
ExchangeCode((void*)recv, 5, byCodeSaveRecv, NULL);
}
void SetLimitSpeed(int nLimit)
{
g_nLimitSpeed=nLimit;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDLLThis=(HMODULE)hModule;
InitHook();
#ifdef _DEBUG
MessageBox(NULL, "hooked.\n", NULL, NULL);
#endif
break;
case DLL_PROCESS_DETACH:
UnHook();
char str[32];
wsprintf(str, "%d", g_nTotalBytes);
char sFile[MAX_PATH+4];
GetModuleFileName(hDLLThis, sFile, MAX_PATH);
sFile[strlen(sFile)-4]='\0';
strcat(sFile, ".ini");
WritePrivateProfileString("WebLimit", "TotalBytes", str, sFile);
break;
}
return TRUE;
}