I am trying to convert a C++ code to C# and maybe someone with both knowledge can help me...
I am trying to convert this code to C#
BOOL CTestNotifyDlg::VariantI8ToSystemTime(VARIANT var, SYSTEMTIME * pst)
{
__int64 ft;
ft = (__int64)var.decVal.Lo64;
FileTimeToSystemTime((FILETIME *)&ft, pst);
return TRUE;
}
C# code:
SYSTEMTIME st = new SYSTEMTIME();
Int64 ft = (Int64)psa.GetValue(1);
byte[] bytes = BitConverter.GetBytes(ft);
System.Runtime.InteropServices.FILETIME ftime = new System.Runtime.InteropServices.FILETIME();
ftime.dwHighDateTime = BitConverter.ToInt32(bytes, 0);
ftime.dwLowDateTime = BitConverter.ToInt32(bytes, 4);
FileTimeToSystemTime(ref ftime, out st); -------- totally weird values.
long fileTime = ((long)(ftime.dwHighDateTime) << 32) + (ftime.dwLowDateTime);
DateTime dt = DateTime.FromFileTimeUtc(fileTime); --------
same here - totally weird values.
any ideas?
Thanks
-
-
No idea but the approach I take when I have no idea is to look more closely what the original does, view the bytes in memory for both codes while stepping through. I don't think it applies here but sometimes also changing the c# project from Any CPU to x86 may affect things. Or well depending on what the systemtime() is and if you're on vista 64 bit, there might be an effect, so worth trying.
-
I tried changing it from Any CPU to x86 - no change.androidi said:No idea but the approach I take when I have no idea is to look more closely what the original does, view the bytes in memory for both codes while stepping through. I don't think it applies here but sometimes also changing the c# project from Any CPU to x86 may affect things. Or well depending on what the systemtime() is and if you're on vista 64 bit, there might be an effect, so worth trying.
Int64 ft = (Int64)psa.GetValue(1);
also the variable value ft = 128654403224000000
converting this huge value is the problem.
First, after googling i found that i need to use BitConvert in order to get Low/HighDateTime values
BitConverter.GetBytes()
System.Runtime.InteropServices.FILETIME ftime = new System.Runtime.InteropServices.FILETIME();
ftime.dwHighDateTime = BitConverter.ToInt32(bytes, 0);
ftime.dwLowDateTime = BitConverter.ToInt32(bytes, 4);
But sometimes the value i get for dwHighDateTime are negative and when i convert using FileTimeToSystemTime i get all zeros...
I am not sure how to proceed here...
-
Rather than convert what is it trying to do? To get the last modified time on a file you can use the FileInfo classarunpv said:
I tried changing it from Any CPU to x86 - no change.androidi said:*snip*
Int64 ft = (Int64)psa.GetValue(1);
also the variable value ft = 128654403224000000
converting this huge value is the problem.
First, after googling i found that i need to use BitConvert in order to get Low/HighDateTime values
BitConverter.GetBytes()
System.Runtime.InteropServices.FILETIME ftime = new System.Runtime.InteropServices.FILETIME();
ftime.dwHighDateTime = BitConverter.ToInt32(bytes, 0);
ftime.dwLowDateTime = BitConverter.ToInt32(bytes, 4);
But sometimes the value i get for dwHighDateTime are negative and when i convert using FileTimeToSystemTime i get all zeros...
I am not sure how to proceed here...
-
After seeing your avatar, I have turned into blowdart's avatar.
-
I am not trying to get last modified time of a file. this data is coming from an ActiveX Control and it has the Event Time Stamp that i need to retrieve.blowdart said:
Rather than convert what is it trying to do? To get the last modified time on a file you can use the FileInfo classarunpv said:*snip*
-
Thank the lord for AdBlockBass said:After seeing your avatar, I have turned into blowdart's avatar.
-
arunpv said:
I tried changing it from Any CPU to x86 - no change.androidi said:*snip*
Int64 ft = (Int64)psa.GetValue(1);
also the variable value ft = 128654403224000000
converting this huge value is the problem.
First, after googling i found that i need to use BitConvert in order to get Low/HighDateTime values
BitConverter.GetBytes()
System.Runtime.InteropServices.FILETIME ftime = new System.Runtime.InteropServices.FILETIME();
ftime.dwHighDateTime = BitConverter.ToInt32(bytes, 0);
ftime.dwLowDateTime = BitConverter.ToInt32(bytes, 4);
But sometimes the value i get for dwHighDateTime are negative and when i convert using FileTimeToSystemTime i get all zeros...
I am not sure how to proceed here...
Okay, you are almost there... Just two things you require to do ...
1. ftime.dwHighDateTime = BitConverter.ToInt32(bytes, 4);
ftime.dwLowDateTime = BitConverter.ToInt32(bytes, 0);It should be reverse, since by default, BitConverter.LittleEndian = False. And next thing,
2. long fileT = (((long)ftime.dwHighDateTime) << 32) + ftime.dwLowDateTime;
DateTime dt =DateTime.FromFileTime(fileT);
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.