none
Is there a way to marshal a System.DateTime as a COM FILETIME structure? RRS feed

  • General discussion

  • I see several COM classes that take a FILETIME structure.  It would help if there was a way to represent that that as a System.DateTime.  Is it possible?

    Will Pittenger


    • Edited by Will Pittenger Friday, September 12, 2014 6:03 PM type-o
    • Changed type Fred BaoModerator Monday, September 29, 2014 7:42 AM The discussion type is more suitable for this case
    Friday, September 12, 2014 6:02 PM

All replies

  • See webpage below.  Please read everything including comments.

    http://www.pinvoke.net/default.aspx/Structures/FILETIME.html


    jdweng

    Friday, September 12, 2014 6:32 PM
  • See webpage below.  Please read everything including comments.

    http://www.pinvoke.net/default.aspx/Structures/FILETIME.html


    jdweng

    Well, I didn't know about the System.Runtime.InteropServices.ComTypes namespace.  But it doesn't do what I asked for.  Nor does everything on that link.  Try changing the source code below so the comment is replace with an option that marshals the parameter as a FILETIME.

    [System.Runtime.InteropServices.MarshalAs(/* What goes here */)]
    public static HResult function GetTime(System.DateTime dtMarshaledAsFileTime);


    Will Pittenger

    Friday, September 12, 2014 6:57 PM
  • You don't like the code below?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices.ComTypes;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
            static FILETIME DateTimeToFileTime(DateTime dt)
            {
                FILETIME ft = new FILETIME();
                //From System.DateTime to System.Runtime.InteropServices.ComTypes.FILETIME
                long hFT1 = dt.ToFileTime();
                ft.dwLowDateTime = (int)(hFT1 & 0xFFFFFFFFL);
                ft.dwHighDateTime = (int)((hFT1 >> 32) & 0xFFFFFFFFL);
    
                return ft;
            }
            static DateTime DateTimeFromFileTime(FILETIME ft)
            {
                long time = (ft.dwHighDateTime << 32) + ft.dwLowDateTime;
                return DateTime.FromFileTime(time);
            }
    
        }
    }
    


    jdweng

    Friday, September 12, 2014 11:27 PM
  • Not really.  I'm working on importing functions as part of an API that might be called by others (which might not be me).  I would prefer that my callers don't need to worry about how to convert.  I want to hide the details.  It's called encapsulation.  I suppose I could make the import private and do the convert in a public method.  But the marshaller would be better.

    Will Pittenger


    Saturday, September 13, 2014 12:14 AM