📄 repairdb.cpp
字号:
#include <windows.h>
#include <math.h>
#include "idapi.h"
pDbiGetErrorInfo DbiGetErrorInfo;
pDbiGetErrorString DbiGetErrorString;
pDbiInitFn DbiInitFn;
pDbiExit DbiExit;
pDbiOpenDatabase DbiOpenDatabase;
pDbiCloseDatabase DbiCloseDatabase;
pDbiSetDirectory DbiSetDirectory;
pDbiDoRestructure DbiDoRestructure;
pDbiAddPassword DbiAddPassword;
HWND hMainWnd;
HINSTANCE hInst;
char szPassword[DBIMAXNAMELEN + 1];
BOOL LoadDLL(HINSTANCE hIDAPI)
{
if (hIDAPI)
{
SetLastError(0);
DbiInitFn = (pDbiInitFn)GetProcAddress(hIDAPI, "DbiInitFn");
DbiExit = (pDbiExit)GetProcAddress(hIDAPI, "DbiExit");
DbiOpenDatabase = (pDbiOpenDatabase)GetProcAddress(hIDAPI, "DbiOpenDatabase");
DbiCloseDatabase = (pDbiCloseDatabase)GetProcAddress(hIDAPI, "DbiCloseDatabase");
DbiSetDirectory = (pDbiSetDirectory)GetProcAddress(hIDAPI, "DbiSetDirectory");
DbiGetErrorInfo = (pDbiGetErrorInfo)GetProcAddress(hIDAPI, "DbiGetErrorInfo");
DbiAddPassword = (pDbiAddPassword)GetProcAddress(hIDAPI, "DbiAddPassword");
DbiDoRestructure = (pDbiDoRestructure)GetProcAddress(hIDAPI, "DbiDoRestructure");
DbiGetErrorString = (pDbiGetErrorString)GetProcAddress(hIDAPI, "DbiGetErrorString");
return GetLastError() == 0;
}
else
{
MessageBox(hMainWnd, "Can not load library \'idapi32.dll\'.", "Error", MB_ICONERROR);
return FALSE;
}
}
BOOL Check(DBIResult ErrorValue)
{
char dbi_status[DBIMAXMSGLEN * 5] = {'\0'};
DBIMSG dbi_string = {'\0'};
DBIErrInfo ErrInfo;
if (ErrorValue != DBIERR_NONE)
{
DbiGetErrorInfo(TRUE, &ErrInfo);
if (ErrInfo.iError == ErrorValue)
{
wsprintf(dbi_status, " ERROR %s", ErrInfo.szErrCode);
if (lstrcmp(ErrInfo.szContext1, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext1);
if (lstrcmp(ErrInfo.szContext2, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext2);
if (lstrcmp(ErrInfo.szContext3, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext3);
if (lstrcmp(ErrInfo.szContext4, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext4);
}
else
{
DbiGetErrorString(ErrorValue, dbi_string);
wsprintf(dbi_status, " ERROR %s", dbi_string);
}
MessageBox(hMainWnd, dbi_status, "BDE Error", MB_ICONERROR);
}
return ErrorValue == DBIERR_NONE;
}
BOOL CALLBACK GetPasswordDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
SendDlgItemMessage(hwndDlg, 100, EM_LIMITTEXT, DBIMAXNAMELEN + 1, 0);
SetFocus(GetDlgItem(hwndDlg, 100));
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
if (GetWindowText(GetDlgItem(hwndDlg, 100), szPassword, DBIMAXNAMELEN + 1) == 0)
{
SetFocus(GetDlgItem(hwndDlg, 100));
break;
}
case IDCANCEL:
EndDialog(hwndDlg, wParam);
}
}
return FALSE;
}
BOOL GetPassword()
{
return LOWORD(DialogBox(hInst, "GETPWDDLG", hMainWnd, GetPasswordDlgProc)) == IDOK;
}
BOOL TablePassword(char *szDBPath, char *szTableName)
{
hDBIDb hDB;
CRTblDesc TblDesc;
DBIResult Res;
HINSTANCE hIDAPI = LoadLibrary("IDAPI32.DLL");
if (!LoadDLL(hIDAPI))
return FALSE;
__try
{
if (!Check(DbiInitFn(DBIINTFVER, NULL)))
return FALSE;
__try
{
if (!Check(DbiOpenDatabase(NULL, NULL, dbiREADONLY, dbiOPENSHARED, NULL, 0, NULL, NULL, &hDB)))
return FALSE;
__try
{
if (!Check(DbiSetDirectory(hDB, szDBPath)))
return FALSE;
ZeroMemory(&TblDesc, sizeof(CRTblDesc));
ZeroMemory(&szPassword, sizeof(szPassword));
lstrcpy(TblDesc.szTblName, szTableName);
lstrcpy(TblDesc.szTblType, szPARADOX);
TblDesc.bPack = TRUE;
pass:
Res = DbiDoRestructure(hDB, 1, &TblDesc, NULL, NULL, NULL, FALSE);
switch (Res)
{
case DBIERR_NOTSUFFTABLERIGHTS:
if (!GetPassword())
return FALSE;
lstrcpy(TblDesc.szPassword, szPassword);
TblDesc.bProtected = TRUE;
Check(DbiAddPassword(szPassword));
goto pass;
default:
return Check(Res);
}
}
__finally
{
Check(DbiCloseDatabase(&hDB));
}
}
__finally
{
DbiExit();
}
}
__finally
{
FreeLibrary(hIDAPI);
}
return FALSE;
}
void RepairTable()
{
char fn[MAX_PATH];
unsigned char fd[65];
HANDLE hFile;
unsigned long sof;
DWORD dwRead;
int rl, lhb, dbs, nr, tb;
char *pFile;
char szFile[MAX_PATH];
OPENFILENAME ofn;
ZeroMemory(fn, MAX_PATH);
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hMainWnd;
ofn.lpstrFile = fn;
ofn.nMaxFile = sizeof(fn);
ofn.lpstrFilter = "Paradox Tables (*.DB)\0*.DB\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
lstrcpy(szFile, fn);
szFile[lstrlen(szFile) - 2] = 0;
lstrcat(szFile, "BAK\0");
CopyFile(fn, szFile, TRUE);
hFile = CreateFile(fn,
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile)
{
sof = GetFileSize(hFile, NULL);
ReadFile(hFile, fd, 64, &dwRead, NULL);
dbs = fd[5] * 1024;
lhb = fd[2] + fd[3] * 256;
tb = (int)ceil((sof - lhb) / dbs);
rl = fd[0] + fd[1] * 256;
nr = dbs / rl;
nr = nr * tb;
fd[6] = nr % 256;
fd[7] = nr / 256;
fd[8] = nr / (256 * 256);
fd[9] = nr / (256 * 256 * 256);
fd[10] = fd[12] = fd[16] = fd[58] = tb % 256;
fd[11] = fd[13] = fd[17] = fd[59] = tb / 256;
fd[14] = 1;
fd[15] = 0;
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
WriteFile(hFile, fd, 64, &dwRead, NULL);
CloseHandle(hFile);
pFile = strrchr(fn, '\\');
*pFile++;
lstrcpy(szFile, pFile);
fn[lstrlen(fn) - lstrlen(pFile)] = 0;
if (TablePassword(fn, szFile))
MessageBox(hMainWnd, "Repair complete.", "Information", MB_ICONINFORMATION);
}
else
MessageBox(hMainWnd, "Can not open file.", "Error", MB_ICONERROR);
}
}
BOOL CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hSys;
MENUITEMINFO mii;
MSGBOXPARAMS mbp;
switch (uMsg)
{
case WM_INITDIALOG:
hMainWnd = hwndDlg;
SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(13579)));
hSys = GetSystemMenu(hwndDlg, FALSE);
ZeroMemory(&mii, sizeof(MENUITEMINFO));
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_TYPE;
mii.fType = MFT_SEPARATOR;
InsertMenuItem(hSys, 1, TRUE, &mii);
InsertMenuItem(hSys, 1, TRUE, &mii);
mii.fMask = MIIM_ID | MIIM_DATA | MIIM_TYPE;
mii.wID = 200;
mii.fType = MFT_STRING;
mii.cch = 12;
mii.dwTypeData = "Send mail...";
InsertMenuItem(hSys, 2, TRUE, &mii);
mii.wID = 300;
mii.fType = MFT_STRING;
mii.cch = 17;
mii.dwTypeData = "About RepairDB...";
InsertMenuItem(hSys, 3, TRUE, &mii);
return TRUE;
case WM_SYSCOMMAND:
switch (LOWORD(wParam))
{
case 200:
ShellExecute(hMainWnd, NULL, "mailto:xzylzh@163.net", NULL, NULL, SW_SHOW);
break;
case 300:
ZeroMemory(&mbp, sizeof(MSGBOXPARAMS));
mbp.cbSize = sizeof(MSGBOXPARAMS);
mbp.hwndOwner = hMainWnd;
mbp.hInstance = hInst;
mbp.lpszText = "RepairDB Version 1.0.0\r\rCopyright (C) 2003-2004 XZY. All rights reserved.";
mbp.lpszCaption = "About RepairDB";
mbp.dwStyle = MB_USERICON;
mbp.lpszIcon = MAKEINTRESOURCE(1357);
MessageBoxIndirect(&mbp);
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 100:
RepairTable();
break;
case IDCANCEL:
EndDialog(hwndDlg, wParam);
return TRUE;
}
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
DialogBox(hInstance, "MAINDLG", GetDesktopWindow(), MainDlgProc);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -