bool _WriteValue(CTSTRING &tsValName, ValueType Type, const void *pVal, DWORD dwBytes) { bool r = false; DWORD dwType = 0UL; switch (Type) { case VT_DWORD: dwType = REG_DWORD; break; case VT_STRING: dwType = REG_SZ; break; case VT_BINARY: dwType = REG_BINARY; break; } if (ERROR_SUCCESS == RegSetValueEx(m_hKey, (0 < tsValName.size()) ? tsValName.c_str() : NULL, 0UL, dwType, static_cast<const BYTE *>(pVal), dwBytes)) { r = true; } return r; }
bool CHelpDB::NewItem(CTSTRING& _sAlias, CTSTRING& sText, CTSTRING& sCreator) { MSXML::IXMLDOMElementPtr El; bool r = false; if(m_doc == 0) return false; TSTRING sAlias; { TCHAR* tsz = _tcsdup(_sAlias.c_str()); _tcsupr(tsz); sAlias = tsz; SAFE_FREE(tsz); } // Just determine if it exists. if(GetItemElement(sAlias, El, false)) { // it already exists; fail. sprintf(m_err, _T("%s already exists. If you want to change the existing entry, use !msgchange."), sAlias.c_str()); g_pLog->msg(_T("%s"), m_err.c_str()); } else { // doesn't already exist... we're golden like the shower. This time // just call GetItemElement() with bCreate set to true. if(!GetItemElement(sAlias, El, true)) { sprintf(m_err, _T("%s could not be added."), sAlias.c_str()); g_pLog->msg(_T("%s"), m_err.c_str()); } else { El->setAttribute(TAG_ALIAS, sAlias.c_str()); El->setAttribute(TAG_CREATOR, sCreator.c_str()); El->setAttribute(TAG_TEXT, sText.c_str()); El->setAttribute(TAG_USAGE, _T("0")); SYSTEMTIME st; TSTRING sTime; GetLocalTime(&st); Bin2Str(sTime, (BYTE*)&st, sizeof(st)); El->setAttribute(TAG_TIME, sTime.c_str()); g_pLog->msg(_T("New db item: %s = %s"), _sAlias.c_str(), sText.c_str()); r = true; } } return r; }
bool _OpenKey(HKEY hKey, CTSTRING &tsPath, bool bCreate) { bool r = false; DWORD dwDisp = 0UL; if (bCreate) { if (ERROR_SUCCESS == RegCreateKeyEx(hKey, tsPath.c_str(), 0UL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisp)) { r = true; } else { m_hKey = NULL; } } else { if (ERROR_SUCCESS == RegOpenKeyEx(hKey, tsPath.c_str(), 0UL, KEY_ALL_ACCESS, &m_hKey)) { r = true; } else { m_hKey = NULL; } } return r; }
TSTRING GetPathFromFile(CTSTRING &tsFile) { TSTRING::size_type r = tsFile.rfind(_T('\\')); if (TSTRING::npos != r) { return tsFile.substr(0, r + 1); } else if (TSTRING::npos != (r = tsFile.rfind(_T('/')))) { return tsFile.substr(0, r + 1); } return _T(""); }
TSTRING GetFileFromPath(CTSTRING &tsPath) { TSTRING::size_type r = tsPath.rfind(_T('\\')); if (TSTRING::npos != r) { return tsPath.substr(r + 1); } else if (TSTRING::npos != (r = tsPath.rfind(_T('/')))) { return tsPath.substr(r + 1); } return _T(""); }
bool CHelpDB::GetItemElement(CTSTRING& _sAlias, MSXML::IXMLDOMElementPtr& Out, bool bCreate) { bool r = false; TSTRING sXPath; MSXML::IXMLDOMNodePtr Node; TSTRING sAlias; { TCHAR* tsz = _tcsdup(_sAlias.c_str()); _tcsupr(tsz); sAlias = tsz; SAFE_FREE(tsz); } // this will look something like MSG[@ALIAS="hithere"] sprintf(sXPath, TAG_MSG _T("[@") TAG_ALIAS _T("=\"%s\"]"), sAlias.c_str()); try { Node = m_root->selectSingleNode(_bstr_t(sXPath.c_str())); if((Node == NULL) && (bCreate == true)) { MSXML::IXMLDOMElementPtr NewEl; MSXML::IXMLDOMNodePtr NewNode; NewEl = m_doc->createElement("MSG"); NewEl.QueryInterface(__uuidof(MSXML::IXMLDOMNode), &NewNode); Node = m_root->appendChild(NewNode); } if(Node != NULL) { Node.QueryInterface(__uuidof(MSXML::IXMLDOMElement), &Out); r = true; } } catch(_com_error& e) { sprintf(m_err, _T("COM Error: %08x"), e.Error()); g_pLog->msg(_T("CHelpDB::GetItemElement(%s)"), _sAlias.c_str()); } return r; }
bool _ReadValue(CTSTRING &tsValName, ValueType Type, void *pVal, DWORD_PTR *pdwBytes) { bool r = false; if (ERROR_SUCCESS == RegQueryValueEx(m_hKey, (0 < tsValName.size()) ? tsValName.c_str() : NULL, 0UL, NULL, static_cast<BYTE *>(pVal), pdwBytes)) { r = true; } return r; }
void GetShortFileName(CTSTRING &tsFile, TSTRING &tsOut) { if (_T("") != tsFile) { TSTRING::size_type n = tsFile.rfind(_T('\\')); if (TSTRING::npos != n) { tsOut = tsFile.substr(n + 1); } else { tsOut = tsFile; } } }
bool Toolbar::SetButtonText(INT_PTR iCmd, CTSTRING &tsText) { TBBUTTONINFO bi = {0}; bi.cbSize = sizeof(TBBUTTONINFO); bi.dwMask = TBIF_TEXT; bi.pszText = const_cast<PTSTR>(tsText.c_str()); return SetButtonInfo(iCmd, &bi); }
bool GetFileExtension(CTSTRING &tsFile, TSTRING &tsOut) { bool r = false; if (_T("") != tsFile) { TSTRING::size_type n = tsFile.rfind(_T('.')); if (TSTRING::npos != n) { tsOut = tsFile.substr(n + 1); r = true; } } return r; }
INT_PTR ErrorPrompt(HWND hWnd, CTSTRING &tsSrcFile, CTSTRING &tsDestFile, bool bContinue, bool bCopy) { INT_PTR r = -1; PVOID pErrFromSystem = NULL; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0x0409, reinterpret_cast<TCHAR *>(&pErrFromSystem), 256, NULL); if (NULL != pErrFromSystem) { PVOID pText = LocalLock(pErrFromSystem); TCHAR *szQuestion = NULL; UINT_PTR uFlags = 0U; if (bContinue) { szQuestion = _T("\nWould you like extFileCopy to") _T(" continue with the next file?"); uFlags = MB_YESNO | MB_ICONSTOP; } else { szQuestion = _T(""); uFlags = MB_OK | MB_ICONSTOP; } r = PrintMsgBox(hWnd, _T("extFileCopy : ERROR"), uFlags, _T("While attempting to %s:\n\n%s\n\nto\n\n%s\n\nThe following") _T(" error occurred:\n\n%s%s"), bCopy ? _T("copy") : _T("move"), tsSrcFile.c_str(), tsDestFile.c_str(), reinterpret_cast<TCHAR *>(pText), szQuestion); LocalFree(pErrFromSystem); } return -1; }
bool CHelpDB::ChangeItem(CTSTRING& sAlias, CTSTRING& sText) { MSXML::IXMLDOMElementPtr El; HRESULT hr = 0; bool r = false; if(m_doc == 0) return false; if(GetItemElement(sAlias, El, false)) { if(SUCCEEDED(hr = El->setAttribute(TAG_TEXT, sText.c_str()))) { g_pLog->msg(_T("Item changed: %s = %s"), sAlias.c_str(), sText.c_str()); r = true; } else { g_pLog->msg(_T("SetAttribute(%s=%s) failed, hr=0x%08x"), TAG_TEXT, sText.c_str(), hr); } } else { sprintf(m_err, _T("%s was not found in the database."), sAlias.c_str()); g_pLog->msg(_T("CHelpDB::ChangeItem(%s) - Item wasn't found."), sAlias.c_str()); } return r; }
LONG_PTR ListBox::AddString(CTSTRING &tsString) { return AddString(tsString.c_str()); }
/* * Deletes an open key */ bool DeleteKey(HKEY hKey, CTSTRING &tsPath) { return (ERROR_SUCCESS == SHDeleteKey(hKey, tsPath.c_str())); }
bool FileGetTime(CTSTRING &tsFile, FILETIMES &ftOut) { bool r = false; /* * Make sure the file exists */ if (-1 != GetFileAttributes(tsFile.c_str())) { HANDLE hFile = NULL; /* * Open the file for query access */ hFile = CreateFile(tsFile.c_str(), /* File Name */ GENERIC_READ, /* Open for read access */ FILE_SHARE_READ | FILE_SHARE_WRITE, /* Don't want to hog the file */ NULL, /* Default security attributes */ OPEN_EXISTING, /* Don't actually create a file */ FILE_ATTRIBUTE_NORMAL, /* Normal attributes */ NULL); /* Not using a template */ if (INVALID_HANDLE_VALUE != hFile) { /* * Acquire the FILETIMEs for the file, * and convert to SYTEMTIME. */ FILETIME ftCreatedTmp = {0}; FILETIME ftModifiedTmp = {0}; if (0 != GetFileTime(hFile, &ftCreatedTmp, NULL, &ftModifiedTmp)) { FILETIME ftCreated = {0}; if (0 != FileTimeToLocalFileTime(&ftCreatedTmp, &ftCreated)) { FILETIME ftModified = {0}; if (0 != FileTimeToLocalFileTime(&ftModifiedTmp, &ftModified)) { if (0 != FileTimeToSystemTime(&ftCreated, &ftOut.stCreated)) { if (0 != FileTimeToSystemTime(&ftModified, &ftOut.stModified)) { /* * SYSTEMTIMEs acquired successfully, * format string outputs. * * Short = MM/DD/YYYY + time * Long = DayOfWeek, Month Day, Year + time */ TCHAR *szSuffix = NULL; WORD w12Hour = ftOut.stCreated.wHour; if (w12Hour > 12) { w12Hour -= 12; szSuffix = _T("PM"); } else { szSuffix = _T("AM"); } /* * Created */ sprintf(ftOut.tsShortCreated, _T("%02d/%02d/%02d %02d:%02d:%02d %s"), ftOut.stCreated.wMonth, ftOut.stCreated.wDay, ftOut.stCreated.wYear, w12Hour, ftOut.stCreated.wMinute, ftOut.stCreated.wSecond, szSuffix); sprintf(ftOut.tsLongCreated, _T("%s, %s %d, %d %02d:%02d:%02d %s"), NumericDayToString(ftOut.stCreated.wDayOfWeek).c_str(), NumericMonthToString(ftOut.stCreated.wMonth).c_str(), ftOut.stCreated.wDay, ftOut.stCreated.wYear, w12Hour, ftOut.stCreated.wMinute, ftOut.stCreated.wSecond, szSuffix); w12Hour = ftOut.stModified.wHour; if (w12Hour > 12) { w12Hour -= 12; szSuffix = _T("PM"); } else { szSuffix = _T("AM"); } /* * Modified */ sprintf(ftOut.tsShortModified, _T("%02d/%02d/%02d %02d:%02d:%02d %s"), ftOut.stModified.wMonth, ftOut.stModified.wDay, ftOut.stModified.wYear, w12Hour, ftOut.stModified.wMinute, ftOut.stModified.wSecond, szSuffix); sprintf(ftOut.tsLongModified, _T("%s, %s %d, %d %02d:%02d:%02d %s"), NumericDayToString(ftOut.stModified.wDayOfWeek).c_str(), NumericMonthToString(ftOut.stModified.wMonth).c_str(), ftOut.stModified.wDay, ftOut.stModified.wYear, w12Hour, ftOut.stModified.wMinute, ftOut.stModified.wSecond, szSuffix); r = true; } } } } } CloseHandle(hFile); } } return r; }
/* * Deletes an existing value */ bool DeleteValue(CTSTRING &tsValue) { return (ERROR_SUCCESS == RegDeleteValue(m_hKey, tsValue.c_str())); }
LONG_PTR ListBox::FindString(CTSTRING &tsFind, INT_PTR iStart) { return SendMsg(LB_FINDSTRING, (WPARAM)iStart, (LPARAM)tsFind.c_str()); }
LONG_PTR ListBox::SelectString(CTSTRING &tsFind, INT_PTR iStart) { return SelectString(tsFind.c_str(), iStart); }
bool WriteString(CTSTRING &tsValName, CTSTRING &tsVal) { return _WriteValue(tsValName, VT_STRING, tsVal.c_str(), static_cast<DWORD>(tsVal.size() * sizeof(TCHAR))); }
bool SetDefaultValue(CTSTRING &tsVal) { return _WriteValue(_T(""), VT_STRING, tsVal.c_str(), static_cast<DWORD>(tsVal.size() * sizeof (TCHAR))); }
LONG_PTR ListBox::InsertString(INT_PTR iIndex, CTSTRING &tsString) { return InsertString(iIndex, tsString.c_str()); }
INT_PTR MsgBox(HWND hWnd, CTSTRING &tsMsg, CTSTRING &tsCap, UINT_PTR uiFlags) { return MessageBox(hWnd, tsMsg.c_str(), tsCap.c_str(), uiFlags); }
LONG_PTR ListBox::FindStringExact(CTSTRING &tsFind, INT_PTR iStart) { return SendMsg(LB_FINDSTRINGEXACT, iStart, reinterpret_cast<LPARAM>(tsFind.c_str())); }
bool FileGetSize(CTSTRING &tsFile, FILESIZE &fsOut) { bool r = false; /* * Make sure the file exists */ if (-1 != GetFileAttributes(tsFile.c_str())) { HANDLE hFile = NULL; /* * Open the file for query access */ hFile = CreateFile(tsFile.c_str(), /* File Name */ FILE_READ_ATTRIBUTES, /* Open for query access */ FILE_SHARE_READ | FILE_SHARE_WRITE, /* Don't want to hog the file */ NULL, /* Default security attributes */ OPEN_EXISTING, /* Don't actually create a file */ FILE_ATTRIBUTE_NORMAL, /* Normal attributes */ NULL); /* Not using a template */ if (INVALID_HANDLE_VALUE != hFile) { /* * File successfully opened; * query size and calculate (b/KB/MB/GB/TB) */ DWORD dwFileHigh = 0UL; DWORD dwFileLow = GetFileSize(hFile, &dwFileHigh); UINT64 ui64Bytes = static_cast<UINT64>(((static_cast<UINT64>(dwFileHigh) << 32) & 0xFFFFFFFF00000000) | (static_cast<UINT64>(dwFileLow) & 0x00000000FFFFFFFF)); /* * Fill out the structure */ fsOut.ui64Bytes = ui64Bytes; fsOut.dKiloBytes = static_cast<double>(ui64Bytes / 1024.0f); fsOut.dMegaBytes = static_cast<double>(fsOut.dKiloBytes / 1024.0f); fsOut.dGigaBytes = static_cast<double>(fsOut.dMegaBytes / 1024.0f); fsOut.dTeraBytes = static_cast<double>(fsOut.dGigaBytes / 1024.0f); /* * Figure out which format is the 'most likely' match * to be displayed to the user. */ TCHAR *szSuffix = NULL; double *pdMatch = NULL; if (fsOut.ui64Bytes < 1024U) { fsOut.dwMatch = FS_BYTES; szSuffix = _T("Bytes"); } else if (fsOut.dKiloBytes < 1024.0f) { fsOut.dwMatch = FS_KBYTES; szSuffix = _T("KB"); pdMatch = &fsOut.dKiloBytes; } else if (fsOut.dMegaBytes < 1024.0f) { fsOut.dwMatch = FS_MBYTES; szSuffix = _T("MB"); pdMatch = &fsOut.dMegaBytes; } else if (fsOut.dGigaBytes < 1024.0f) { fsOut.dwMatch = FS_GBYTES; szSuffix = _T("GB"); pdMatch = &fsOut.dGigaBytes; } else if (fsOut.dTeraBytes < 1024.0f) { fsOut.dwMatch = FS_TBYTES; szSuffix = _T("TB"); pdMatch = &fsOut.dTeraBytes; } if (FS_BYTES == fsOut.dwMatch) { sprintf(fsOut.tsStrFmt, _T("%I64u %s"), fsOut.ui64Bytes, szSuffix); } else { sprintf(fsOut.tsStrFmt, _T("%.02f %s"), *pdMatch, szSuffix); } CloseHandle(hFile); r = true; } } return r; }