Groups | Blog | Home
all groups > dotnet interop > may 2005 >

dotnet interop : Getting time with Win32 API from C# returns a wrong creation date



Mattias Sjögren
5/27/2005 12:00:00 AM
Johan,

[quoted text, click to view]

I don't think I can help you with your question. I just wanted to
point out that the SizeConst setting specifies the number of
_characters_ in the string, not bytes. So the sizes should be 260 and
14 respectively.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Johan Delimon
5/27/2005 4:05:24 AM
Hello,

I want to access some files that have paths longer than MAX_PATH so I have
to use Win32 API Unicode functions.

I use these functions to getfiles and information.

[DllImport("kernel32.dll", EntryPoint="FindFirstFileW", SetLastError=true,
CharSet = CharSet.Unicode)]
public static extern IntPtr FindFirstFileW( string lpFileName, out
WIN32_FIND_DATAW lpFindFileData);

[DllImport("kernel32.dll", EntryPoint="FindNextFileW", SetLastError=true,
CharSet = CharSet.Unicode)]
public static extern bool FindNextFileW( IntPtr hFindFile, out
WIN32_FIND_DATAW lpFindFileData);

This is the structure I use.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WIN32_FIND_DATAW
{
public uint dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
// TCHAR array 260 (MAX_PATH) entries, 520 bytes in unicode
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=520)]
public string cFileName;
// TCHAR array 14 TCHAR's alternate filename 28 byes in unicode
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=28)]
public string cAlternateFileName;
}


This is the way I convert FILETIME to DateTime

long hFT2 = (((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
DateTime dte = DateTime.FromFileTimeUtc(hFT2);

Now my question is how Created Date/Time is very ofter wrong and differs 7-8
minutes from viewing with Windows Explorer?

Has anyone experienced this before? Or am I doing something wrong?

When I use the .NET implementation the dates are correct as in Explorer but
these give errors on long path names.

Example

Windows explorer shows
file1 10/01/2005 00:03 10/01/2005 00:03
file2 10/01/2005 00:04 10/01/2005 00:04

My application shows
file1 10/01/2005 00:03 10/01/2005 00:03
file2 9/01/2005 23:57 9/01/2005 23:57

So as you can see in the same folder I have correct values and wrong values.
All values are converted with the same function.

Any suggestions?
Johan Delimon
5/27/2005 9:58:05 AM
I found this on the internet somewhere, does calling this function for
Unicode characterset also use SizeConst=260?

Would changing this make any sense?

[quoted text, click to view]
AddThis Social Bookmark Button