// Vance Thrift and Biller File Utility 2 // // Douglas Thrift // // $Id: License.cxx,v 1.3 2003/09/12 04:26:54 douglas Exp $ #include "License.h" License::License() { number = count++; if (number == 0) library = LoadLibrary("riched20.dll"); licenses.insert(pair(number, this)); } License::~License() { licenses.erase(number); if (licenses.empty()) FreeLibrary(library); } void License::display(HWND parent) { if (license.str() == "") { HRSRC license = FindResource(gui.instance, LPCTSTR(IDR_LICENSE), LPCTSTR(256)); int length = SizeofResource(gui.instance, license); HANDLE handle = LoadResource(gui.instance, license); if (handle != NULL) { const char* license = (char*)(LockResource(handle)); this->license.write(license, length); } else { error(parent); } } DialogBoxParam(gui.instance, MAKEINTRESOURCE(IDD_LICENSE), parent, window, number); } void License::check(HWND parent) { HKEY key; if (LONG code = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\DouglasThrift\\VTBFileUtil2", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &key, NULL) == ERROR_SUCCESS) { DWORD type, data, size = sizeof(data); if (RegQueryValueEx(key, "LastLicenseDate", NULL, &type, LPBYTE(&data), &size) == ERROR_SUCCESS) { if (type != REG_DWORD) { data = 0; } } else { data = 0; } if (debug) cerr << "data = {\n" << " month = " << LOWORD(data) << "\n" << " year = " << HIWORD(data) << "\n" << "}\n"; SYSTEMTIME time; GetLocalTime(&time); if (debug) cerr << "time = {\n" << " month = " << time.wMonth << "\n" << " year = " << time.wYear << "\n" << "}\n"; if (LOWORD(data) != time.wMonth || HIWORD(data) != time.wYear) { display(parent); data = MAKELONG(time.wMonth, time.wYear); if (code = RegSetValueEx(key, "LastLicenseDate", 0, REG_DWORD, LPBYTE(&data), sizeof(data)) != ERROR_SUCCESS) { error(parent, code); } } RegCloseKey(key); } else { error(parent, code); } } unsigned License::count = 0; HMODULE License::library = NULL; map License::licenses; map License::windows; INT_PTR License::window(HWND dialog, UINT msg, WPARAM w, LPARAM l) { map::iterator itor = windows.find(dialog); License* data = itor->second; switch (msg) { case WM_INITDIALOG: center(dialog); SendMessage(dialog, WM_SETICON, ICON_BIG, LPARAM(gui.icon)); SendMessage(dialog, WM_SETTEXT, 0, LPARAM(programName.c_str())); { map::iterator itor = licenses.find(l); windows.insert(pair(dialog, itor->second)); data = itor->second; } { HMENU menu = GetSystemMenu(dialog, FALSE); MENUITEMINFO separator, save; int count = GetMenuItemCount(menu); separator.cbSize = sizeof(separator); separator.fMask = MIIM_TYPE; separator.fType = MFT_SEPARATOR; save.cbSize = sizeof(save); save.fMask = MIIM_ID | MIIM_TYPE; save.fType = MFT_STRING; save.wID = 1; save.dwTypeData = "&Save..."; save.cch = 8; InsertMenuItem(menu, count++, TRUE, &separator); InsertMenuItem(menu, count++, TRUE, &save); } if (library != NULL) { HWND license = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_DISABLENOSCROLL | ES_READONLY | ES_WANTRETURN, 30, 33, 460, 242, dialog, NULL, gui.instance, 0); SETTEXTEX text; text.flags = ST_DEFAULT; text.codepage = CP_ACP; SendMessage(license, EM_SETTEXTEX, WPARAM(&text), LPARAM(data->license.str().c_str())); } break; case WM_SYSCOMMAND: switch (w) { case 1: { OPENFILENAME save; char file[MAX_PATH]; StringCchPrintf(file, MAX_PATH, "License.rtf"); save.lStructSize = sizeof(save); save.hwndOwner = dialog; save.lpstrFilter = "Rich Text Format (RTF)\0*.rtf\0"; save.lpstrCustomFilter = NULL; save.nFilterIndex = 1; save.lpstrFile = file; save.nMaxFile = MAX_PATH; save.lpstrFileTitle = NULL; save.lpstrInitialDir = NULL; save.lpstrTitle = NULL; save.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; save.lpstrDefExt = "rtf"; if (GetSaveFileName(&save)) { if (debug) cerr << "file = " << file << "\n"; ofstream fout(file); fout << data->license.str(); fout.close(); } } break; } break; case WM_COMMAND: switch (LOWORD(w)) { case IDCANCEL: case IDOK: EndDialog(dialog, w); return TRUE; break; } break; } return FALSE; }