Complete error code definitions and handling guide for date_error.h
date_error.h defines all error codes, facility codes, and event categories used by the MDTS Date Log library. These codes follow the Windows HRESULT format and provide detailed error information for all date log operations.
All error codes in this library follow the Windows HRESULT format:
// HRESULT format (32-bit value):
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// Sev (2 bits): Severity code (0=Success, 1=Info, 2=Warning, 3=Error)
// C (1 bit): Customer code flag (1 = library-defined error)
// R (1 bit): Reserved
// Facility (11): Facility code (0x333 = FACILITY_THISLIB)
// Code (16): Specific error code
Each error code is a 32-bit value that contains specific information about the error. You can extract these components using bitwise operations:
Determines the type of result
0x0 = Success (S_OK, S_FALSE)0x1 = Informational0x2 = Warning0x3 = ErrorIndicates if the error is defined by the customer/library (1) or system (0)
Identifies the subsystem that generated the error
0xFFF = FACILITY_APPLICATION0x333 = FACILITY_THISLIB (Date Log Library)0x0 = FACILITY_SYSTEM (Windows system)Specific error code within the facility
// Recommended macros from winnt.h
#define SUCCEEDED(hr) (((LONG)(hr)) >= 0)
#define FAILED(hr) (((LONG)(hr)) < 0)
// Extract components
#define HRESULT_SEVERITY(hr) (((hr) >> 30) & 0x3)
#define HRESULT_CUSTOMER(hr) (((hr) >> 29) & 0x1)
#define HRESULT_FACILITY(hr) (((hr) >> 16) & 0x7FF)
#define HRESULT_CODE(hr) ((hr) & 0xFFFF)
Facility codes identify the subsystem or component that generated the error. The Date Log library uses multiple facilities for different subsystems:
Application-level events and categories
Windows system errors and standard error codes
Runtime environment errors
Date Log library-specific errors
File I/O and disk operation errors
Event categories are used for categorizing and filtering logged events. The following categories are defined for the Date Log library:
| Category Name | Value | Description |
|---|---|---|
| NETWORK_CATEGORY | 0x0FFF0001 | Network-related events and operations |
| DATABASE_CATEGORY | 0x0FFF0002 | Database and SQL operations |
| INFO_CATEGORY | 0x0FFF0003 | UI and informational events |
| IO_CATEGORY | 0x0FFF0004 | File I/O and disk operations |
| CONFIG_CATEGORY | 0x0FFF0005 | Configuration and settings events |
| ENCRYPTION_CATEGORY | 0x0FFF0006 | Encryption and security operations |
| SYSTEM_CATEGORY | 0x0FFF0007 | System-level events |
| SECURITY_CATEGORY | 0x0FFF0008 | Security and access control events |
Informational codes indicate successful operations or non-error conditions that may be logged or tracked:
The following error codes are returned by Date Log library functions when operations fail.
All error codes have the format 0xC333XXXX (Error severity + Library facility).
// Most common usage pattern
HRESULT hr = InitializeDateItem(hDateLog, &fileTime, NULL, &hDateItem);
if (SUCCEEDED(hr)) {
// Operation succeeded, use hDateItem
printf("Date item created successfully\n");
} else {
// Operation failed, log error code
printf("Failed to create date item: 0x%08X\n", hr);
}
// Clean up
if (hDateItem != NULL) {
FreeDateItem(hDateItem);
}
HRESULT hr = MergeDateLogHandle(hDest, hSource, dwFlags);
if (FAILED(hr)) {
// Extract components
DWORD dwSeverity = HRESULT_SEVERITY(hr);
DWORD dwFacility = HRESULT_FACILITY(hr);
DWORD dwCode = HRESULT_CODE(hr);
DWORD dwCustomer = HRESULT_CUSTOMER(hr);
printf("Error 0x%08X:\n", hr);
printf(" Severity: 0x%X\n", dwSeverity);
printf(" Customer: %s\n", dwCustomer ? "Library-defined" : "System-defined");
printf(" Facility: 0x%X\n", dwFacility);
printf(" Code: 0x%X\n", dwCode);
// Handle specific errors
switch (hr) {
case ERR_DATE_LOG_PENDING_IO:
printf("Date log is busy, retrying...\n");
Sleep(100);
// Retry operation
break;
case ERR_DATELOG_ZERO_GAP:
printf("Invalid date range (zero or negative gap)\n");
break;
case ERR_INVALID_DATELOG_HANDLE:
printf("Invalid handle provided\n");
break;
default:
printf("Unknown error occurred\n");
break;
}
}
HRESULT hr = InitializeDateItem(hDateLog, &ft, NULL, &hItem);
if (SUCCEEDED(hr)) {
printf("Date item created\n");
} else if (hr == ERR_DATE_IS_HOLIDAY) {
// Special handling for holidays
printf("Selected date is a holiday/weekend.\n");
printf("Please select a market trading day.\n");
// Find next valid market day
FILETIME ftNext = ft;
// Add logic to increment to next business day
} else if (hr == ERR_DATE_IS_FUTURE_DATE) {
printf("ERROR: Cannot use future dates\n");
} else if (hr == ERR_INVALID_DATELOG_HANDLE) {
printf("ERROR: Invalid date log handle\n");
printf("Reinitialize handle and retry\n");
} else {
printf("Unexpected error: 0x%08X\n", hr);
}
Error codes defined in this library support the Windows FormatMessage API when
the library is properly registered with the system's message resource files. This allows
human-readable error messages to be retrieved dynamically.
For FormatMessage integration to work, the library must:
#include
#include
// Custom error message mapping function
const char* GetDateLogErrorMessage(HRESULT hr) {
switch (hr) {
case S_OK:
return "Operation completed successfully";
case ERR_DATE_IS_HOLIDAY:
return "The selected date is a holiday or weekend";
case ERR_DATE_EARLIER_BASE:
return "The date is earlier than the start date";
case ERR_INVALID_DATELOG_HANDLE:
return "Invalid date log handle";
case ERR_DATELOG_ZERO_GAP:
return "Zero or negative gap between dates";
case ERR_MAX_DATELOG_REACHED:
return "Date log exceeds maximum size";
case ERR_DATE_LOG_PENDING_IO:
return "Date log is busy; operation pending";
default:
return "Unknown error occurred";
}
}
// Usage example
int main() {
HRESULT hr = InitializeDateItem(hDateLog, &ft, NULL, &hItem);
if (FAILED(hr)) {
const char* pszMsg = GetDateLogErrorMessage(hr);
printf("ERROR (0x%08X): %s\n", hr, pszMsg);
return 1;
}
return 0;
}
// Combine library and system error messages
void PrintDetailedError(HRESULT hr) {
LPVOID lpMsgBuf = NULL;
DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM;
DWORD dwFacility = HRESULT_FACILITY(hr);
printf("===== Error Report =====\n");
printf("Error Code: 0x%08X\n", hr);
printf("Severity: %s\n", HRESULT_SEVERITY(hr) >= 2 ? "ERROR" : "INFO");
printf("Facility: 0x%X (%s)\n", dwFacility,
(dwFacility == 0x333) ? "FACILITY_THISLIB" :
(dwFacility == 0xFFF) ? "FACILITY_APPLICATION" :
"FACILITY_SYSTEM");
printf("Error Code: 0x%X\n", HRESULT_CODE(hr));
// Try system error translation first
if (FormatMessageA(dwFlags, NULL, hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf, 0, NULL)) {
printf("System Message: %s\n", (const char*)lpMsgBuf);
LocalFree(lpMsgBuf);
} else {
// Fall back to custom messages
printf("Message: %s\n", GetDateLogErrorMessage(hr));
}
printf("=======================\n");
}
// Example call
int main() {
HRESULT hr = MergeDateLogHandle(hDest, hSource, dwFlags);
if (FAILED(hr)) {
PrintDetailedError(hr);
}
return 0;
}
// Production-quality error logging
BOOL LogDateLogError(HRESULT hr, const char* pszOperation) {
FILE* pFile = fopen("datelog_errors.log", "a");
if (pFile == NULL) return FALSE;
SYSTEMTIME st;
GetLocalTime(&st);
fprintf(pFile, "[%04d-%02d-%02d %02d:%02d:%02d] ",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond);
fprintf(pFile, "OPERATION: %s\n", pszOperation);
fprintf(pFile, "ERROR CODE: 0x%08X\n", hr);
fprintf(pFile, "SEVERITY: %d\n", HRESULT_SEVERITY(hr));
fprintf(pFile, "FACILITY: 0x%X\n", HRESULT_FACILITY(hr));
fprintf(pFile, "CODE: 0x%X\n", HRESULT_CODE(hr));
fprintf(pFile, "MESSAGE: %s\n", GetDateLogErrorMessage(hr));
fprintf(pFile, "---\n");
fclose(pFile);
return TRUE;
}
// Usage
HRESULT hr = LoadDateLogHandle(pFileInfo, NULL, NULL, hDateLog, NULL, dwFlags);
if (FAILED(hr)) {
LogDateLogError(hr, "LoadDateLogHandle");
}
// Windows Registry path for message DLL registration
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MDTS_DateLog
//
// Registry entries to add:
// EventMessageFile: C:\Program Files\MDTS\mdts_datelog.dll
// TypesSupported: 7 (Success, Error, Warning)
//
// PowerShell command to register:
// New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\MDTS_DateLog" -Force
// Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\MDTS_DateLog" `
// -Name "EventMessageFile" -Value "C:\Program Files\MDTS\mdts_datelog.dll"
// Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\MDTS_DateLog" `
// -Name "TypesSupported" -Value 7
// This will work only if the DLL is registered
DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER;
LPVOID lpMsgBuf = NULL;
HRESULT hr = InitializeDateItem(hDateLog, &ft, NULL, &hItem);
if (FAILED(hr)) {
// FormatMessage can now find the message in the registered DLL
if (FormatMessageA(dwFlags, NULL, hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf, 0, NULL)) {
fprintf(stderr, "ERROR: %s\n", (const char*)lpMsgBuf);
LocalFree(lpMsgBuf);
} else {
// Fallback to custom message
fprintf(stderr, "ERROR (0x%08X): %s\n", hr,
GetDateLogErrorMessage(hr));
}
}
| Error Code | Value | Category | Severity |
|---|---|---|---|
| INFO_MERGE_IS_COMPLETE | 0x43330100 | Informational | Success |
| ERR_DATE_EARLIER_BASE | 0xC3330400 | Date Validation | Error |
| ERR_DATE_IS_FUTURE_DATE | 0xC3330401 | Date Validation | Error |
| ERR_DATE_IS_HOLIDAY | 0xC3330402 | Date Validation | Error |
| ERR_DATELOG_MERGE_GAP | 0xC3330403 | Merge | Error |
| ERR_NOMERGE_BYFLAG | 0xC3330404 | Merge | Error |
| ERR_DATEITEM_NOT_FOUND | 0xC3330405 | Date Item | Error |
| ERR_INVALID_DATELOG_HANDLE | 0xC3330406 | Handle | Error |
| ERR_INVALID_HOLIDAY_HANDLE | 0xC3330407 | Handle | Error |
| ERR_DATELOG_ZERO_GAP | 0xC3330412 | Range | Error |
| ERR_MAX_DATELOG_REACHED | 0xC3330413 | Capacity | Error |
| ERR_DATE_LOG_PENDING_IO | 0xC3330430 | I/O | Error |