1

In relation my other question, I've tried to write a small program that sets a files short name given it's long name. However I'm always getting an exception thrown:

Error: The parameter is incorrect on FAT32

Error: The handle is invalid on NTFS

Edit: I now appreciate that I cannot call the API under FAT32

FYI: I'm running under an Administrative Console.

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Win32.SafeHandles;
    using System.ComponentModel;

    namespace Test{
      public static class Program
      {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public extern static IntPtr CreateFile(
             string lpFileName, uint dwDesiredAccess, uint dwShareMode,
             IntPtr SecurityAttributes, uint dwCreationDisposition,
             uint dwFlagsAndAttributes, IntPtr hTemplateFile
             );

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetFileShortName(IntPtr hFile, string lpShortName);

        public static void Main(string[] args)
        {
            const uint GENERIC_ALL = 0x10000000;
            const uint FILE_SHARE_READ = 0x00000001;
            const uint FILE_SHARE_WRITE = 0x00000002;
            const uint FILE_SHARE_DELETE = 0x00000003;

            const uint OPEN_EXISTING = 3;
            const uint BACKUP_SEMANTICS = 0x02000000;

            var handle = CreateFile(args[0], GENERIC_ALL, FILE_SHARE_READ,
                IntPtr.Zero, OPEN_EXISTING, BACKUP_SEMANTICS, IntPtr.Zero);

            bool ret = SetFileShortName(handle, args[1]);

            if (!ret)
            {
                Console.WriteLine("Error:  {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message);
            }
        }
    }
}

It's the same if I use FILE_SHARE DELETE or WRITE

Community
  • 1
  • 1
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216

1 Answers1

2

Based on your linked question, it sounds like you're trying to do this on a FAT32 volume. That doesn't work, from the docs for SetShortFileName:

Sets the short name for the specified file. The file must be on an NTFS file system volume.

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setfileshortnamea

Emphasis mine.

Re edit:

Error: The handle is invalid on NTFS

Is the CreateFile call succeeding? You seem to opening it with the appropriate permissions for SetShortFileName which makes me wonder if the CreateFile is failing and returning INVALID_HANDLE_VALUE. I would check and make sure that the CreateFile call is succeeding.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • Thanks. I wonder what I need to do to get the NTFS version of the program to work. – Preet Sangha Nov 07 '10 at 00:05
  • 1
    Think I've got it - further down in the doc (RTFM!!!) SE_RESTORE_NAME is required. My code though admin is most likely missing this. Need to go to get set up first! – Preet Sangha Nov 07 '10 at 04:41