Coffeehouse Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C++ to C# question

Back to Forum: Coffeehouse
  • User profile image
    arunpv

    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

  • User profile image
    androidi

    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.

  • User profile image
    arunpv

    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.
    I tried changing it from Any CPU to x86 - no change.
           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...


  • User profile image
    blowdart

    arunpv said:
    androidi said:
    *snip*
    I tried changing it from Any CPU to x86 - no change.
           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 class

  • User profile image
    Bass

    After seeing your avatar, I have turned into blowdart's avatar.

  • User profile image
    arunpv

    blowdart said:
    arunpv said:
    *snip*
    Rather than convert what is it trying to do? To get the last modified time on a file you can use the FileInfo class
    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.

  • User profile image
    Lloyd_Humph

    Bass said:
    After seeing your avatar, I have turned into blowdart's avatar.
    Thank the lord for AdBlock

    If Blackberrys are addictive cellphones, Channel9 is the ultimate addictive website.
    Last modified
  • User profile image
    KeepITSimple​Boy

    arunpv said:
    androidi said:
    *snip*
    I tried changing it from Any CPU to x86 - no change.
           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);

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.