This is the sample program in which "GetDateFormatEx" returns an invalid string. It is failing in customer's setup.
#include <windows.h>
#include <iostream>
uint64_t
shimTime(void)
{
FILETIME ts;
GetSystemTimeAsFileTime(&ts);
ULONGLONG systime = ((ULARGE_INTEGER*)&ts)->QuadPart;
return systime;
}
int main() {
static const size_t sz = 64;
TCHAR currTimeStr[sz];
TCHAR currDateStr[sz];
FILETIME ts;
int ret = -1;
// Get current-time in pretty-format
GetSystemTimeAsFileTime(&ts);
uint64_t currTime = shimTime();
SYSTEMTIME stime, lstime;
if (currTime == 0) {
printf("currTime is 0\n");
swprintf_s(currTimeStr, sz, L"0");
swprintf_s(currDateStr, sz, L"0");
}
else {
if (!FileTimeToSystemTime((FILETIME*)&currTime, &stime)) {
printf("Unable to get FileTimeToSystemTime, err=%u\n", GetLastError());
}
if (!SystemTimeToTzSpecificLocalTime(NULL, &stime, &lstime)) {
printf("Unable to get SystemTimeToTzSpecificLocalTime, err=%u\n", GetLastError());
}
ret = GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &lstime, NULL, currTimeStr, _countof(currTimeStr));
printf("GetTimeFormatEx ret = %d\n", ret);
if (!ret) {
printf("GetTimeFormatEx failed, err=%u\n", GetLastError());
}
ret = GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &lstime, L"ddd',' MMM dd',' yyyy", currDateStr, _countof(currDateStr), NULL);
printf("GetDateFormatEx ret = %d\n", ret);
if (!ret) {
printf("GetDateFormatEx failed, err=%u\n", GetLastError());
}
}
currTimeStr[sz - 1] = '\0';
currDateStr[sz - 1] = '\0';
// Print time and date
printf("Current time is: %S\n", currTimeStr);
printf("Current date is: %S\n", currDateStr);
char cp[1024];
int cursize = sprintf_s(&cp[0], 1023, "ts=%S %S", currTimeStr, currDateStr);
if (cursize < 0 || cursize >= 1023) {
printf("Error occurred during sprintf_s, cursize: %u\n", cursize);
}
printf("%s\n", cp);
return 0;
}
All I can see from log is cursize from sprint_f returned is 429496729 and the last print returns "ts=06:21:20 " None of the functions have failed. All are returning non zero values. currDateStr seems to be faulty but GetDateFormatEx has returned a non-zero value.
Details of the system
C:>systeminfo
Host Name: MIGNT006
OS Name: Microsoft Windows Server 2019 Standard
OS Version: 10.0.17763 N/A Build 17763
OS Manufacturer: Microsoft Corporation
OS Configuration: Member Server
OS Build Type: Multiprocessor Free
Registered Owner: Flex
Registered Organization: Flex
Product ID: 00429-70000-00000-AA322
Original Install Date: 01/30/2021, 17:11:12
System Boot Time: 01/19/2024, 14:52:23
System Manufacturer: HP
System Model: ProLiant DL360 G7
System Type: x64-based PC
Processor(s): 2 Processor(s) Installed.
BIOS Version: HP P68, 05/21/2018
Windows Directory: C:\WINDOWS
System Directory: C:\WINDOWS\system32
Boot Device: \Device\HarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC+02:00) Jerusalem
Total Physical Memory: 16,374 MB
Available Physical Memory: 5,051 MB
Virtual Memory: Max Size: 18,806 MB
Virtual Memory: Available: 7,398 MB
Virtual Memory: In Use: 11,408 MB
Page File Location(s): C:\pagefile.sys
Domain: europe.ad.flextronics.com
Logon Server: \EUMIG002
Hotfix(s): 31 Hotfix(s) Installed.
Network Card(s): 4 NIC(s) Installed.
Connection Name: Ethernet 3
Status: Media disconnected
Connection Name: Ethernet 4
DHCP Enabled: No
IP address(es)
Connection Name: Ethernet 2
Status: Media disconnected
Connection Name: Ethernet
Status: Media disconnected
Hyper-V Requirements: VM Monitor Mode Extensions: Yes
Virtualization Enabled In Firmware: Yes
Second Level Address Translation: Yes
Data Execution Prevention Available: Yes
`
Can you please help in figuring out what could be possibly wrong here?