- 注册时间
- 2004-8-30
- 最后登录
- 1970-1-1
|
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <imagehlp.h>
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
#pragma resource "xml.res"
TfrmBetter *frmBetter;
const int SP2TCPIPSIZE = 359040L;
const int SP2TCPIPPOS = 0x4F322L;
//---------------------------------------------------------------------------
__fastcall TfrmBetter::TfrmBetter(TComponent* Owner)
: TForm(Owner)
{
FIsXP = false;
}
//---------------------------------------------------------------------------
String GetFileVersionString(LPTSTR pFile)
{
if(pFile==NULL||pFile[0]==0) return String();
DWORD Temp0 = 0, Temp1 = 0;
DWORD dwSize = ::GetFileVersionInfoSize(pFile, &Temp0);
if(dwSize==0) throw Exception("Error Size!");
char* pVersion = new char[dwSize + 10];
memset(pVersion, 0, dwSize + 10);
if(!GetFileVersionInfo(pFile, 0, dwSize + 2, pVersion))
{
delete[] pVersion;
throw Exception("Error Version!");
}
VS_FIXEDFILEINFO* pFixed = NULL;
if(!VerQueryValue(pVersion, "\\", (LPVOID*)&pFixed, (PUINT)&Temp1)
||pFixed==NULL||Temp1<sizeof(VS_FIXEDFILEINFO))
{
delete[] pVersion;
throw Exception("Error Value!");
}
Temp0 = pFixed->dwFileVersionMS;
Temp1 = pFixed->dwFileVersionLS;
delete[] pVersion;
return String().sprintf("%u.%u.%u.%u", (Temp0>>16), (Temp0&0xFFFF), (Temp1>>16), (Temp1&0xFFFF));
}
//---------------------------------------------------------------------------
DWORD SimpleGetFileSize(LPTSTR pFile)
{
if(pFile==NULL||pFile[0]==0) return (DWORD)~0;
HANDLE hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile==NULL||hFile==INVALID_HANDLE_VALUE) return (DWORD)~0;
DWORD dwSize = GetFileSize(hFile, NULL);
CloseHandle(hFile);
return dwSize;
}
//---------------------------------------------------------------------------
void __fastcall TfrmBetter::FormCreate(TObject *Sender)
{
try
{
DetectSystemInformation();
}
catch(Exception& e)
{
MessageBox(NULL, e.Message.c_str(), "Error", MB_OK|MB_ICONSTOP);
Application->ShowMainForm = false;
Application->Terminate();
}
catch(...)
{
Application->ShowMainForm = false;
Application->Terminate();
}
}
//---------------------------------------------------------------------------
/*detect system information, when return, means no error, else exception will be throwed!*/
void __fastcall TfrmBetter:etectSystemInformation()
{
//Detect System Information
OSVERSIONINFO os = {0,};
os.dwOSVersionInfoSize = sizeof(os);
if(!GetVersionEx(&os)) throw Exception("Fatal Error: Get System Information!");
FIsXP = (os.dwMajorVersion==5&&os.dwMinorVersion==1&&os.dwPlatformId==VER_PLATFORM_WIN32_NT);
Memo1->Lines->Add(String().sprintf("Operating System: Microsoft Windows %u.%u.%u %s",
os.dwMajorVersion, os.dwMinorVersion,
os.dwBuildNumber, os.szCSDVersion));
//Get tcpip.sys version string
char szFile[MAX_PATH + 128];
memset(szFile, 0, sizeof(szFile));
::GetSystemDirectory(szFile, MAX_PATH);
if(szFile[0]&&szFile[strlen(szFile)-1]!='\\')
{
strcat(szFile, "\\drivers\\tcpip.sys");
}
else
{
strcat(szFile, "drivers\\tcpip.sys");
}
if(!FileExists(szFile)) throw Exception("tcpip.sys not found!");
String version = GetFileVersionString(szFile);
Memo1->Lines->Add(String("Version of tcpip.sys: ") + version);
//Check tcpip.sys version is ok or force param is specified
LPTSTR pCmd = GetCommandLine();
if(version=="5.1.2600.2180"&&SimpleGetFileSize(szFile)==SP2TCPIPSIZE)
{
Memo1->Lines->Add("SP2 tcpip.sys detected!");
}
else if(pCmd&&strstr(pCmd, "/force")!=NULL)
{
Memo1->Lines->Add("Warning: tcpip.sys does not match, but /force is specified!");
}
else
{
FIsXP = false;
throw Exception(Memo1->Text + "\nThe version of tcpip.sys does not match! Operation Aborted!");
}
//now, we start to detect current tcp/ip limitation
HANDLE hFile = ::CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile==NULL||hFile==INVALID_HANDLE_VALUE) throw Exception("mmm....impossible");
if(::SetFilePointer(hFile, SP2TCPIPPOS, NULL, FILE_BEGIN)!=(DWORD)SP2TCPIPPOS)
{
::CloseHandle(hFile);
throw Exception("Error: SetFilePointer!");
}
DWORD dwRead = 0, dwValue = 0;
if(::ReadFile(hFile, (LPVOID)&dwValue, sizeof(DWORD), &dwRead, NULL)&&dwRead==sizeof(DWORD))
{
edLimit->Text = dwValue;
}
::CloseHandle(hFile);
FIsXP = true;
FTCPIP = szFile;
}
//---------------------------------------------------------------------------
void __fastcall TfrmBetter::btApplyClick(TObject *Sender)
{
if(!FIsXP||FTCPIP.IsEmpty()) throw Exception("not SP2!");
//backup a copy of the tcpip.sys
::CopyFile(FTCPIP.c_str(), (FTCPIP + ".old").c_str(), TRUE);
//get new value
int nValue = StrToIntDef(edLimit->Text.Trim(), 120);
if(nValue<=0||nValue>0x7FFFFFFE) nValue = 120;
//first, we open the file for read/write
HANDLE hFile = ::CreateFile(FTCPIP.c_str(),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile==NULL||hFile==INVALID_HANDLE_VALUE) throw Exception("Invalid File!");
DWORD dwSize = ::GetFileSize(hFile, NULL);
if(dwSize==(DWORD)~0||dwSize<=(DWORD)(SP2TCPIPPOS + sizeof(DWORD)))
{
::CloseHandle(hFile);
throw Exception("Invalid FileSize!");
}
//second, map the file
HANDLE hMap = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
::CloseHandle(hFile);
if(hMap==NULL||hMap==INVALID_HANDLE_VALUE) throw Exception("Invalid Map!");
PVOID pFile = ::MapViewOfFile(hMap, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
if(pFile==NULL)
{
::CloseHandle(hMap);
throw Exception("Map File Error!");
}
//third, change the tcp/ip limitation
__try{memmove((char*)pFile + SP2TCPIPPOS, &nValue, sizeof(DWORD));}
__except(EXCEPTION_EXECUTE_HANDLER)
{
::UnmapViewOfFile(pFile);
::CloseHandle(hMap);
throw Exception("I/O Error!");
}
//go with checksum
DWORD Original = 0, New = 0;
CheckSumMappedFile(pFile, dwSize, &Original, &New);
PIMAGE_NT_HEADERS pHeader = ImageNtHeader(pFile);
if(pHeader&&pHeader->OptionalHeader.CheckSum==Original
&&memcmp(&pHeader->Signature, "PE\0\0", 4)==0)
{
__try
{
pHeader->OptionalHeader.CheckSum = New;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
::UnmapViewOfFile(pFile);
::CloseHandle(hMap);
throw Exception("I/O Error!");
}
}
UnmapViewOfFile(pFile);
::CloseHandle(hMap);
//detect system information again
Sleep(500);
Memo1->Clear();
edLimit->Text = "";
DetectSystemInformation();
if(StrToIntDef(edLimit->Text.Trim(), 0)==nValue)
{
Memo1->Lines->Add("Patch Successfully!");
}
else
{
Memo1->Lines->Add("Patch Failed!");
}
Memo1->Lines->Add("Restart your operating system is strongly recommended!");
}
|
|