-1

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
540
  • 71
  • 7

1 Answers1

1

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);
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Hello! Thanks! Its hard to get to know how this work. If i follow the examples from http://msdn.microsoft.com/en-us/library/windows/apps/ms724950(v=vs.85).aspx i still get the problem with conversion. Ive tried yours, but am unsure of how to declare _countof. – 540 Aug 14 '13 at 22:21
  • `_countof` is a vendor-specific compiler extension. If your compiler does not support `_countof`, you can use `sizeof(wchBuf)/sizeof(wchBuf[0])` instead. – Remy Lebeau Aug 14 '13 at 22:25
  • I did this to resemble the msdn example:SYSTEMTIME st, lt; GetSystemTime(&st); GetLocalTime(&lt); SetDlgItemText(hWnd, 1003, st.wHour); Seemed simple enough. But wont work. – 540 Aug 14 '13 at 22:27
  • Thanks Remy. Did that, then it tells me that "GetDateFormat" dont take 5 arguments. – 540 Aug 14 '13 at 22:30
  • 1
    I edited the example to remove the `_countof` and add the missing NULL argument (sorry about that) - you should be able to get it to compile now. `wHour` is an integer, not a string, so again you can't pass it to `SetDlgItemText` without converting it. `SetDlgItemInt`, however, can take an integer argument. – Jonathan Potter Aug 14 '13 at 22:40
  • Thank you very much for your help! Did manage to get it to work now! Also with the shorter verson. when using SetDlgItemInt! – 540 Aug 14 '13 at 22:56