#include #include #include

GetDateFormatEx returns a non-zero value but the buffer returned by it is faulty

50 views Asked by At

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.
                           [01]: Intel64 Family 6 Model 44 Stepping 2 GenuineIntel ~177 Mhz
                           [02]: Intel64 Family 6 Model 44 Stepping 2 GenuineIntel ~177 Mhz
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.
                           [01]: KB5033911
                           [02]: KB4486153
                           [03]: KB4535680
                           [04]: KB4549947
                           [05]: KB4561600
                           [06]: KB4562562
                           [07]: KB4589208
                           [08]: KB4598480
                           [09]: KB4601393
                           [10]: KB5000859
                           [11]: KB5003243
                           [12]: KB5003711
                           [13]: KB5005112
                           [14]: KB5034127
                           [15]: KB5006754
                           [16]: KB5008287
                           [17]: KB5009642
                           [18]: KB5011574
                           [19]: KB5012675
                           [20]: KB5014031
                           [21]: KB5014797
                           [22]: KB5015896
                           [23]: KB5017400
                           [24]: KB5020374
                           [25]: KB5023789
                           [26]: KB5028316
                           [27]: KB5030505
                           [28]: KB5031589
                           [29]: KB5032306
                           [30]: KB5034863
                           [31]: KB5005701
Network Card(s):           4 NIC(s) Installed.
                           [01]: HP NC382i DP Multifunction Gigabit Server Adapter
                                 Connection Name: Ethernet 3
                                 Status:          Media disconnected
                           [02]: HP NC382i DP Multifunction Gigabit Server Adapter
                                 Connection Name: Ethernet 4
                                 DHCP Enabled:    No
                                 IP address(es)
                                 [01]: 10.229.8.6
                           [03]: HP NC382i DP Multifunction Gigabit Server Adapter
                                 Connection Name: Ethernet 2
                                 Status:          Media disconnected
                           [04]: HP NC382i DP Multifunction Gigabit Server Adapter
                                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?

0

There are 0 answers