Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

Calling Unicode Version of GetFileAttributesEx

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Olbert - 14 Sep 2004 05:48 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.