Loging System Time & Date Error
-
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC. -
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.Suresh H wrote:
::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.
Suresh H wrote:
new TCHAR[21];
this buffer is too small. try a value of 50
nave
-
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC. -
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.SYSTEMTime is for use with GetSystemTime() function. Example : 1. SYSTEMTIME stSystime; GetSystemTime(&stSystime); This will load UTC time into stSystime variable. 2. SYSTEMTIME stSystime; GetLocalTime(&stSystime); 3. Your Code with Local time Logging: outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; // Add this line GetLocalTime(&stSystime); //Remove this line //::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
-
Suresh H wrote:
::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.
Suresh H wrote:
new TCHAR[21];
this buffer is too small. try a value of 50
nave
-
SYSTEMTime is for use with GetSystemTime() function. Example : 1. SYSTEMTIME stSystime; GetSystemTime(&stSystime); This will load UTC time into stSystime variable. 2. SYSTEMTIME stSystime; GetLocalTime(&stSystime); 3. Your Code with Local time Logging: outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; // Add this line GetLocalTime(&stSystime); //Remove this line //::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
-
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.Suresh H wrote:
_stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);
Why not put a breakpoint on this statement to verify the values of
st
before bothering to write them to the file? The "crash" is happening because you are writing 36 bytes into a buffer that can only hold 21.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Suresh H wrote:
::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.
Suresh H wrote:
new TCHAR[21];
this buffer is too small. try a value of 50
nave
Naveen R wrote:
this buffer is too small.
How so?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Naveen R wrote:
this buffer is too small.
How so?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
sorry i dont understand your question. What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); So i said him to increase the buffer. I found you too suggested the same to him. Then why a question like this?
nave
-
Suresh H wrote:
_stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);
Why not put a breakpoint on this statement to verify the values of
st
before bothering to write them to the file? The "crash" is happening because you are writing 36 bytes into a buffer that can only hold 21.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
Hi David, Thank you very much for the explaining the cause of crash. I had actually used break points to check the crash , I found it was cashing at this statement delete []lpDateTime; and also I was not getting the proper out put I had commented the delete statement and I was concentrating on the GetLocalTime function once I got the proper time and date later I checked the cause and I found that I had used 21 bytes instead of 36 bytes. Now I got the solution for the crash. Thanking you.,:) Suresh HC
-
Hi David, Thank you very much for the explaining the cause of crash. I had actually used break points to check the crash , I found it was cashing at this statement delete []lpDateTime; and also I was not getting the proper out put I had commented the delete statement and I was concentrating on the GetLocalTime function once I got the proper time and date later I checked the cause and I found that I had used 21 bytes instead of 36 bytes. Now I got the solution for the crash. Thanking you.,:) Suresh HC
Suresh H wrote:
I found it was cashing at this statement delete []lpDateTime;
And rightly so since
lpDateTime
had been stepped all over. The array is indeed large enough; you just need to verify that you're not trying to stuff too much into it.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
sorry i dont understand your question. What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); So i said him to increase the buffer. I found you too suggested the same to him. Then why a question like this?
nave
Naveen R wrote:
What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);
Sure it is. Actually, only 20 bytes are needed, assuming that the values in
st
are correct (which they weren't in his example).Naveen R wrote:
I found you too suggested the same to him.
I never indicated the buffer was too small. I told him to fix the problem itself, not something unrelated further downstream.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code.
outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();
and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.