#include <windows.h>
#include "resource.h"
#include "TpcbExample.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100
HINSTANCE hInst; HWND hWndDlgMain; HWND hWndFrame; TpcbExample *tpcb;
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MainDialog (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK InitDialog (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK AdvancedDialog (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK RunDialog (HWND, UINT, WPARAM, LPARAM);
BOOL GetHomeDirectory(HWND, BOOL);
BOOL RecursiveDirRemove(wchar_t *);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
hWndDlgMain = NULL;
tpcb = new TpcbExample();
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WCE_TPCB);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WCE_TPCB));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
TCHAR szTitle[MAX_LOADSTRING]; TCHAR szWindowClass[MAX_LOADSTRING];
hInst = hInstance; LoadString(hInstance, IDC_WCE_TPCB, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWndFrame = CreateWindow(szWindowClass, szTitle,
WS_VISIBLE | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWndFrame)
{
return FALSE;
}
ShowWindow(hWndDlgMain, nCmdShow);
UpdateWindow(hWndDlgMain);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hDlg;
switch (message)
{
case WM_COMMAND:
DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_CREATE:
hDlg = CreateDialog(hInst,
MAKEINTRESOURCE(IDD_MAINDIALOG), hWnd,
(DLGPROC)MainDialog);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK MainDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt;
HWND hCurrentRadioButton;
wchar_t wdirname[MAX_PATH], msg[1024], *title;
int ret, valid, ntxns, written;
switch (message)
{
case WM_INITDIALOG:
GetClientRect(GetParent(hDlg), &rt);
SetWindowPos(hDlg, HWND_TOP, 0, 0, rt.right, rt.bottom,
SWP_SHOWWINDOW);
CheckRadioButton(hDlg, IDC_MEDIUM_RADIO,
IDC_SMALL_RADIO, IDC_SMALL_RADIO);
SetDlgItemText(hDlg, IDC_HOME_EDIT,
tpcb->getHomeDirW(wdirname, MAX_PATH));
SetDlgItemInt(hDlg, IDC_TXN_EDIT, 1000, 0);
SetWindowText(hDlg, L"BDB TPCB Example app");
ShowWindow(hDlg, SW_SHOWNORMAL);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_INIT_BUTTON ||
LOWORD(wParam) == IDC_RUN_BUTTON ) {
hCurrentRadioButton = GetDlgItem(hDlg,
IDC_SMALL_RADIO);
if(BST_CHECKED ==
SendMessage(hCurrentRadioButton,
BM_GETCHECK, NULL, NULL)) {
tpcb->accounts = 500;
tpcb->branches = 10;
tpcb->tellers = 50;
tpcb->history = 5000;
}
hCurrentRadioButton = GetDlgItem(hDlg,
IDC_MEDIUM_RADIO);
if(BST_CHECKED ==
SendMessage(hCurrentRadioButton,
BM_GETCHECK, NULL, NULL)) {
tpcb->accounts = 1000;
tpcb->branches = 10;
tpcb->tellers = 100;
tpcb->history = 10000;
}
hCurrentRadioButton = GetDlgItem(hDlg,
IDC_LARGE_RADIO);
if(BST_CHECKED ==
SendMessage(hCurrentRadioButton,
BM_GETCHECK, NULL, NULL)) {
tpcb->accounts = 100000;
tpcb->branches = 10;
tpcb->tellers = 100;
tpcb->history = 259200;
}
EnableWindow(GetDlgItem(hDlg, IDC_INIT_BUTTON),
FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_RUN_BUTTON),
FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_ADV_BUTTON),
FALSE);
}
if (LOWORD(wParam) == IDC_ADV_BUTTON) {
CreateDialog(hInst,
MAKEINTRESOURCE(IDD_ADVANCEDDIALOG), hDlg,
(DLGPROC)AdvancedDialog);
} else if (LOWORD(wParam) == IDC_INIT_BUTTON) {
tpcb->closeEnv();
GetHomeDirectory(hDlg, TRUE);
tpcb->createEnv(0);
ret = tpcb->populate();
} else if (LOWORD(wParam) == IDC_RUN_BUTTON) {
GetHomeDirectory(hDlg, FALSE);
if (GetFileAttributes(
tpcb->getHomeDirW(wdirname, MAX_PATH)) !=
FILE_ATTRIBUTE_DIRECTORY) {
_snwprintf(msg, 1024,
L"Target directory: %s does not exist, or is not a directory.\nMake sure the "
L"directory name is correct, and that you ran the initialization phase.",
wdirname);
MessageBox(hDlg, msg, L"Error", MB_OK);
EnableWindow(GetDlgItem(hDlg,
IDC_INIT_BUTTON), TRUE);
EnableWindow(GetDlgItem(hDlg,
IDC_RUN_BUTTON), TRUE);
EnableWindow(GetDlgItem(hDlg,
IDC_ADV_BUTTON), TRUE);
return FALSE;
}
ntxns = GetDlgItemInt(hDlg, IDC_TXN_EDIT,
&valid, FALSE);
if (valid == FALSE) {
MessageBox(hDlg,
L"Invalid number in transaction field.",
L"Error", MB_OK);
EnableWindow(GetDlgItem(hDlg,
IDC_INIT_BUTTON), TRUE);
EnableWindow(GetDlgItem(hDlg,
IDC_RUN_BUTTON), TRUE);
EnableWindow(GetDlgItem(hDlg,
IDC_ADV_BUTTON), TRUE);
return FALSE;
}
tpcb->createEnv(0);
ret = tpcb->run(ntxns);
} else if (LOWORD(wParam) == IDC_EXIT_BUTTON) {
tpcb->closeEnv();
EndDialog(hDlg, LOWORD(wParam));
DestroyWindow(hDlg);
DestroyWindow(hWndFrame);
return FALSE;
}
if (LOWORD(wParam) == IDC_INIT_BUTTON ||
LOWORD(wParam) == IDC_RUN_BUTTON ) {
if (ret == 0)
title = L"Results";
else
title = L"Error message";
written = MultiByteToWideChar(CP_ACP, NULL,
tpcb->msgString, strlen(tpcb->msgString),
msg, sizeof(msg)/sizeof(msg[0]));
msg[written] = L'\0';
MessageBox(hDlg, msg, title, MB_OK);
EnableWindow(GetDlgItem(hDlg, IDC_INIT_BUTTON), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_RUN_BUTTON),
TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_ADV_BUTTON),
TRUE);
}
break;
case WM_DESTROY:
tpcb->closeEnv();
EndDialog(hDlg, LOWORD(wParam));
DestroyWindow(hDlg);
DestroyWindow(hWndFrame);
return FALSE;
default:
return DefWindowProc(hDlg, message, wParam, lParam);
}
return TRUE;
}
LRESULT CALLBACK AdvancedDialog(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
RECT rt;
HWND hCurrentCheckBox;
int currentInt, valid;
switch (message)
{
case WM_INITDIALOG:
GetClientRect(GetParent(hDlg), &rt);
SetWindowPos(hDlg, HWND_TOP, 0, 0, rt.right, rt.bottom,
SWP_SHOWWINDOW);
if (tpcb->fast_mode == 0) {
hCurrentCheckBox =
GetDlgItem(hDlg, IDC_FASTMODE_CHECK);
SendMessage(hCurrentCheckBox, BM_SETCHECK,
BST_CHECKED, 0);
}
if (tpcb->verbose == 1) {
hCurrentCheckBox =
GetDlgItem(hDlg, IDC_VERBOSE_CHECK);
SendMessage(hCurrentCheckBox, BM_SETCHECK,
BST_CHECKED, 0);
}
if (tpcb->cachesize != 0) {
SetDlgItemInt(hDlg, IDC_CACHE_EDIT,
tpcb->cachesize/1024, FALSE);
}
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_DONE_BUTTON) {
hCurrentCheckBox =
GetDlgItem(hDlg, IDC_FASTMODE_CHECK);
if(BST_CHECKED == SendMessage(hCurrentCheckBox,
BM_GETCHECK, NULL, NULL))
tpcb->fast_mode = 0;
else
tpcb->fast_mode = 1;
hCurrentCheckBox =
GetDlgItem(hDlg, IDC_VERBOSE_CHECK);
if(BST_CHECKED == SendMessage(hCurrentCheckBox,
BM_GETCHECK, NULL, NULL))
tpcb->verbose = 1;
else
tpcb->verbose = 0;
currentInt = GetDlgItemInt(hDlg,
IDC_RANDOM_EDIT, &valid, FALSE);
if (valid != FALSE)
tpcb->rand_seed = currentInt;
currentInt = GetDlgItemInt(hDlg,
IDC_CACHE_EDIT, &valid, FALSE);
if (valid != FALSE) {
if (currentInt < 20) {
MessageBox(hDlg,
L"Min cache size is 20kb.",
L"Error", MB_OK);
return FALSE;
}
tpcb->cachesize = currentInt*1024;
}
EndDialog(hDlg, LOWORD(wParam));
DestroyWindow(hDlg);
}
break;
default:
return DefWindowProc(hDlg, message, wParam, lParam);
}
return TRUE;
}
BOOL
GetHomeDirectory(HWND hDlg, BOOL init)
{
wchar_t wdirname[MAX_PATH];
DWORD attrs;
if (GetDlgItemText(hDlg, IDC_HOME_EDIT, wdirname, MAX_PATH) == 0)
tpcb->setHomeDir(TESTDIR);
else
tpcb->setHomeDirW(wdirname);
if (init == TRUE) {
tpcb->getHomeDirW(wdirname, MAX_PATH);
attrs = GetFileAttributes(wdirname);
if (attrs == FILE_ATTRIBUTE_DIRECTORY)
RecursiveDirRemove(wdirname);
else if (attrs == FILE_ATTRIBUTE_NORMAL)
DeleteFile(wdirname);
else if (attrs != 0xFFFFFFFF) {
return FALSE;
}
return CreateDirectory(wdirname, NULL);
}
return TRUE;
}
BOOL
RecursiveDirRemove(wchar_t *dirname)
{
HANDLE hFind; WIN32_FIND_DATA findFileData;
wchar_t DirPath[MAX_PATH];
wchar_t FileName[MAX_PATH];
wcscpy(DirPath, dirname);
wcscat(DirPath, L"\\*"); wcscpy(FileName, dirname);
wcscat(FileName, L"\\");
MessageBox(hWndDlgMain, L"Cleaning directory.", L"Message", MB_OK);
if ((hFind = FindFirstFile(DirPath,&findFileData)) ==
INVALID_HANDLE_VALUE)
return FALSE;
wcscpy(DirPath,FileName);
MessageBox(hWndDlgMain, L"Found files in directory.",
L"Message", MB_OK);
bool bSearch = true;
do {
wcscpy(FileName + wcslen(DirPath), findFileData.cFileName);
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!RecursiveDirRemove(FileName))
break; } else {
if (findFileData.dwFileAttributes &
FILE_ATTRIBUTE_READONLY)
SetFileAttributes(findFileData.cFileName,
FILE_ATTRIBUTE_NORMAL);
if (!DeleteFile(FileName)) {
MessageBox(hWndDlgMain, L"Delete failed.",
L"Message", MB_OK);
break; }
}
} while (FindNextFile(hFind,&findFileData));
FindClose(hFind); return RemoveDirectory(dirname); }
extern "C" {
void tpcb_errcallback(const DB_ENV *, const char *errpfx, const char *errstr)
{
wchar_t wstr[ERR_STRING_MAX];
memset(wstr, 0, sizeof(wstr));
MultiByteToWideChar(CP_ACP, 0, errstr, strlen(errstr),
wstr, ERR_STRING_MAX-1);
MessageBox(hWndDlgMain, wstr, L"Error Message", MB_OK);
exit(1);
}
}