winapiunicodesystemtime

error C2664: 'SetDlgItemTextW' : cannot convert parameter 3 from 'SYSTEMTIME' to 'LPCWSTR' 208


I still have some trouble with understanding this with UNICODE and ANSI in win32 api..

For example, i have this code:

SYSTEMTIME LocalTime = { 0 };
GetSystemTime (&LocalTime);
SetDlgItemText(hWnd, 1003, LocalTime);'

That generates the error in the title.

Also, i should mention that it automatically adds a W after "setdlgitemtext" Some macro in VS probably.

Could someone clarify this for me?


Solution

  • In C or C++ you can't just take an arbitrary structure and pass it to a function that expects a string. You have to convert that structure to a string first.

    The Win32 functions GetDateFormat() and GetTimeFormat() can be used to convert a SYSTEMTIME to a string (the first one does the "date" part and the second one does the "time" part) according to the current system locale rules.

    For example,

    SYSTEMTIME LocalTime = { 0 };
    GetSystemTime (&LocalTime);
    wchar_t wchBuf[80];
    GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &LocalTime, NULL, wchBuf, sizeof(wchBuf) / sizeof(wchBuf[0]));
    SetDlgItemText(hWnd, 1003, wchBuf);