Uhrzeit...Exception
-
zuerst mal bissal code:
std::string getSystemTime() const { SYSTEMTIME st; LPSTR szTime; // Uhrzeit holen GetLocalTime (&st); // Die Uhrzeit formatiert in den String szTime schreiben ::GetTimeFormat( 0, 0, &st, "hh':'mm':'ss tt", reinterpret_cast<LPTSTR> (&szTime), 0 ); string time_(szTime); // hier tritt die exception auf!!!;-( ::LocalFree(szTime); return time_; }
so hatte ich das auch mal bei formatmessage...nur tritt hier ne exception bei string time(sz_time); auf....hat jemand ne ahnung warum?
cu
-
Hi Gerald.
Wie kommst du darauf das GetTimeFormat sich selbst den Speicher besorgt für szTime? In der Dokumentation konnte ich nichts finden.
-
gfff schrieb:
Hi Gerald.
Wie kommst du darauf das GetTimeFormat sich selbst den Speicher besorgt für szTime? In der Dokumentation konnte ich nichts finden.
aja *g*
bei FormatMessage steht:
lpBuffer
[out] Pointer to a buffer that receives the null-terminated string that specifies the formatted message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc function, and places the pointer to the buffer at the address specified in lpBuffer.danke cu
-
FormatMessage ist aber nicht GetTimeFormat!
I würde es so machen:
std::string getSystemTime() const { SYSTEMTIME st; // Uhrzeit holen GetLocalTime (&st); // Buffer anfordern vector<char>vBuf(GetTimeFormat( 0, 0, &st, "hh':'mm':'ss tt", NULL, 0 )); // Die Uhrzeit formatiert in vBuf schreiben GetTimeFormat( 0, 0, &st, "hh':'mm':'ss tt", &(vBuf[0]), vBuf.size()); // Buffer in string verwandeln return string(vBuf.begin(), vBuf.end()); }