#include "stdafx.h"
#include "resource.h"
#define DBSETHOST 1
#define DBSETUSER 2
#define DBSETPWD 3
#define DBSETAPP 4
#define DBSETID 5
#define DBSETLANG 6
#define ID_SENDMAIL 13579
#define ID_ABOUT 24680
#define SUCCEED 1
#define FAIL 0
#define SUCCEED_ABORT 2
#define EXCOMM 9
#define DBNOERR -1
#define INT_EXIT 0
#define INT_CONTINUE 1
#define INT_CANCEL 2
#define DBERRHANDLE_PROC FARPROC
#define STRINGBIND 10
#define NO_MORE_ROWS -2
typedef char DBCHAR;
typedef long DBINT;
#define LOGINREC void
typedef LOGINREC * PLOGINREC;
#define DBPROCESS void
typedef DBPROCESS * PDBPROCESS;
#define RETCODE INT
#define STATUS INT
typedef PLOGINREC (DBLOGIN)(void);
typedef DBLOGIN *PDBLOGIN;
typedef PLOGINREC (DBFREELOGIN)(PLOGINREC);
typedef DBFREELOGIN *PDBFREELOGIN;
typedef RETCODE (DBSETLNAME)(PLOGINREC, LPCSTR, INT);
typedef DBSETLNAME *PDBSETLNAME;
typedef PDBPROCESS (DBOPEN)(PLOGINREC, LPCSTR);
typedef DBOPEN *PDBOPEN;
typedef RETCODE (DBCLOSE)(PDBPROCESS);
typedef DBCLOSE *PDBCLOSE;
typedef RETCODE (DBCMD)(PDBPROCESS, LPCSTR);
typedef DBCMD *PDBCMD;
typedef RETCODE (DBUSE)(PDBPROCESS, LPCSTR);
typedef DBUSE *PDBUSE;
typedef RETCODE (DBSQLEXEC)(PDBPROCESS);
typedef DBSQLEXEC *PDBSQLEXEC;
typedef RETCODE (DBRESULTS)(PDBPROCESS);
typedef DBRESULTS *PDBRESULTS;
typedef RETCODE (DBBIND)(PDBPROCESS, INT, INT, DBINT, LPBYTE);
typedef DBBIND *PDBBIND;
typedef STATUS (DBNEXTROW)(PDBPROCESS);
typedef DBNEXTROW *PDBNEXTROW;
typedef DBINT (DBDATLEN)(PDBPROCESS, INT);
typedef DBDATLEN *PDBDATLEN;
typedef DBERRHANDLE_PROC DBERRHANDLE(DBERRHANDLE_PROC);
typedef DBERRHANDLE *PDBERRHANDLE;
typedef BOOL DBDEAD(PDBPROCESS);
typedef DBDEAD *PDBDEAD;
ATOM MyRegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND g_hWnd, g_hwndServer, g_hwndUserName, g_hwndPassword,
g_hwndConnect, g_hwndClear, g_hwndSelAll, g_hwndUnselect,
g_hwndDatabase, g_hwndProgress;
HINSTANCE g_hInst, g_hDLL;
PDBLOGIN dblogin;
PDBFREELOGIN dbfreelogin;
PDBSETLNAME dbsetlname;
PDBOPEN dbopen;
PDBCLOSE dbclose;
PDBCMD dbcmd;
PDBUSE dbuse;
PDBSQLEXEC dbsqlexec;
PDBRESULTS dbresults;
PDBBIND dbbind;
PDBNEXTROW dbnextrow;
PDBDATLEN dbdatlen;
PDBERRHANDLE dberrhandle;
PDBDEAD dbdead;
char g_szServer[16], g_szUserName[128], g_szPassword[128];
char szError[] = "Can not load library 'ntwdblib.dll'.";
int Error(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
{
MessageBox(g_hWnd, dberrstr, "Error", MB_ICONERROR);
if (dbproc == NULL || dbdead(dbproc))
return INT_EXIT;
else
return INT_CANCEL;
}
BOOL LoadDLLProc()
{
BOOL bRet = FALSE;
g_hDLL = LoadLibrary("ntwdblib.dll");
if (g_hDLL)
{
dblogin = (PDBLOGIN)GetProcAddress(g_hDLL, "dblogin");
dbfreelogin = (PDBFREELOGIN)GetProcAddress(g_hDLL, "dbfreelogin");
dbsetlname = (PDBSETLNAME)GetProcAddress(g_hDLL, "dbsetlname");
dbopen = (PDBOPEN)GetProcAddress(g_hDLL, "dbopen");
dbclose = (PDBCLOSE)GetProcAddress(g_hDLL, "dbclose");
dbcmd = (PDBCMD)GetProcAddress(g_hDLL, "dbcmd");
dbuse = (PDBUSE)GetProcAddress(g_hDLL, "dbuse");
dbsqlexec = (PDBSQLEXEC)GetProcAddress(g_hDLL, "dbsqlexec");
dbresults = (PDBRESULTS)GetProcAddress(g_hDLL, "dbresults");
dbbind = (PDBBIND)GetProcAddress(g_hDLL, "dbbind");
dbnextrow = (PDBNEXTROW)GetProcAddress(g_hDLL, "dbnextrow");
dbdatlen = (PDBDATLEN)GetProcAddress(g_hDLL, "dbdatlen");
dberrhandle = (PDBERRHANDLE)GetProcAddress(g_hDLL, "dberrhandle");
dbdead = (PDBDEAD)GetProcAddress(g_hDLL, "dbdead");
dberrhandle((DBERRHANDLE_PROC)Error);
bRet = TRUE;
}
return bRet;
}
void FreeDLLProc()
{
if (g_hDLL)
{
FreeLibrary(g_hDLL);
g_hDLL = NULL;
}
}
void AddDatabase()
{
LOGINREC *logrec = dblogin();
if (logrec)
{
dbsetlname(logrec, g_szUserName, DBSETUSER);
dbsetlname(logrec, g_szPassword, DBSETPWD);
dbsetlname(logrec, "LZHXZY", DBSETAPP);
DBPROCESS *dbproc = dbopen(logrec, g_szServer);
if (dbproc)
{
dbuse(dbproc, "master");
dbcmd(dbproc, "SELECT name FROM sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb')");
dbsqlexec(dbproc);
if (dbresults(dbproc) == SUCCEED)
{
DBCHAR szName[128];
dbbind(dbproc, 1, STRINGBIND, (DBINT)128, (BYTE*)szName);
while (dbnextrow(dbproc) != NO_MORE_ROWS)
{
szName[dbdatlen(dbproc, 1)] = '\0';
SendMessage(g_hwndDatabase, LB_ADDSTRING, 0, (LPARAM)szName);
}
}
dbclose(dbproc);
}
dbfreelogin(logrec);
}
}
BOOL ClearLogFile(const char *szDBName, char *szDBFile, char *szLogFile)
{
BOOL bRet = FALSE;
LOGINREC *logrec = dblogin();
if (logrec)
{
dbsetlname(logrec, g_szUserName, DBSETUSER);
dbsetlname(logrec, g_szPassword, DBSETPWD);
dbsetlname(logrec, "LZHXZY", DBSETAPP);
DBPROCESS *dbproc = dbopen(logrec, g_szServer);
if (dbproc)
{
if (dbuse(dbproc, szDBName) == SUCCEED)
{
dbcmd(dbproc, "sp_helpfile");
dbsqlexec(dbproc);
dbresults(dbproc);
DBCHAR szName[MAX_PATH];
dbbind(dbproc, 3, STRINGBIND, (DBINT)MAX_PATH, (BYTE*)szName);
dbnextrow(dbproc);
szName[dbdatlen(dbproc, 3)] = '\0';
strcpy(szDBFile, szName);
dbnextrow(dbproc);
szName[dbdatlen(dbproc, 3)] = '\0';
strcpy(szLogFile, szName);
bRet = TRUE;
}
dbclose(dbproc);
if (bRet)
{
bRet = FALSE;
dbproc = dbopen(logrec, g_szServer);
if (dbproc)
{
if (dbuse(dbproc, "master") == SUCCEED)
{
char szCmd[MAX_PATH + 256];
sprintf(szCmd, "sp_detach_db '%s'", szDBName);
dbcmd(dbproc, szCmd);
dbsqlexec(dbproc);
ZeroMemory(szCmd, MAX_PATH + 256);
sprintf(szCmd, "xp_cmdshell 'del \"%s\"'", szLogFile);
dbcmd(dbproc, szCmd);
dbsqlexec(dbproc);
bRet = TRUE;
}
dbclose(dbproc);
if (bRet)
{
bRet = FALSE;
dbproc = dbopen(logrec, g_szServer);
if (dbproc)
{
if (dbuse(dbproc, "master") == SUCCEED)
{
char szCmd[MAX_PATH + 256];
sprintf(szCmd, "sp_attach_single_file_db '%s','%s'", szDBName, szDBFile);
dbcmd(dbproc, szCmd);
dbsqlexec(dbproc);
bRet = TRUE;
}
dbclose(dbproc);
}
}
}
}
}
dbfreelogin(logrec);
}
return bRet;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
UINT nWidth = 260;
UINT nHeight = 295;
MyRegisterClass(hInstance);
g_hInst = hInstance;
int iSX, iSY;
iSX = GetSystemMetrics(SM_CXSCREEN);
iSY = GetSystemMetrics(SM_CYSCREEN);
HWND hWnd = CreateWindowEx(0, "LZHXZY", "Clear Log File", WS_VISIBLE | WS_SYSMENU,
(iSX - nWidth) / 2, (iSY - nHeight) / 2, nWidth, nHeight, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
g_hWnd = hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_CHAR)
{
switch (msg.wParam)
{
case VK_TAB:
SetFocus(GetNextDlgTabItem(g_hWnd, GetFocus(), HIBYTE(GetKeyState(VK_SHIFT)) != 0));
break;
case VK_RETURN:
hWnd = GetFocus();
if (GetWindowLong(hWnd, GWL_ID) == 2468)
SendMessage(g_hWnd, WM_COMMAND, MAKELPARAM(BN_CLICKED, 0), (LPARAM)hWnd);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CLEARLOG);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground