Environment: C#, Framework 1.1, WinXP SP2
Okay, first off I admit I don't really know what I'm doing here.
I find that I must dip into the Windows API because MS, in its infinite wisdom, "bound" the File and
FileInfo objects in the Framework to the "short path" version of the file API functions (i.e., if
you use a path over MAX_PATH in a call to File object methods or in creating a FileInfo object
you'll get a PathTooLong exception).
Unfortunately, I must deal with some file names that are longer than MAX_PATH in my application. The
file names are specified via UNC.
I tried reading up on how to invoke GetFileAttributesEx, but I must be doing something wrong. Here's
some pseudocode that fails:
[StructLayout(LayoutKind.Sequential)]
public struct WIN32_FILE_ATTRIBUTE_DATA
{
public uint fileAttributes;
public FILETIME creationTime;
public FILETIME lastAccessTime;
public FILETIME lastWriteTime;
public uint fileSizeHigh;
public uint fileSizeLow;
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetFileAttributesEx(string path, int level, out WIN32_FILE_ATTRIBUTE_DATA
dat );
private void Junk( string fileName )
{
WIN32_FILE_ATTRIBUTE_DATA atx;
// the following call always fails...but it generally succeeds if the "override"
// preamble (the \\?\UNC\) is left off
bool ok = GetFileAttributesExW( @"\\?\UNC\" + fileName, 0, out atx );
}
I tried changing CharSet to Unicode, marshaling the path variable as:
[MarshalAs(UnmanagedType.LPWStr)]
and changing the extern function reference to GetFileAttributesExW
None of this worked.
So... what do I do to make this work?
- Mark
Mattias Sj?gren - 14 Sep 2004 08:40 GMT
Mark,
>None of this worked.
>
>So... what do I do to make this work?
So what happens, does the function return false? If so, what happens
if you add SetLastError=true to the DllImport attribute and then check
Marshal.GetLastWin32Error() after the call?
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mark Olbert - 14 Sep 2004 15:14 GMT
>So what happens, does the function return false? If so, what happens
>if you add SetLastError=true to the DllImport attribute and then check
>Marshal.GetLastWin32Error() after the call?
Nice tip, thanx. I didn't realize there was a SetLastError attribute.
It turns out that the problem had nothing to do with interop. Instead, it was the format of the UNC
path. I was passing in something like:
\\server\share\file
and converting it to
\\?\UNC\\\server\share\file (i.e., I was just prepending the \\?\UNC\ override)
when I should've been chopping off the leading \\ first:
\\?\UNC\server\share\file
Making that fix solved the problem.
- Mark