Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

interop CreateFileW : Access Denied

Posted on 2006-04-03
6
Medium Priority
?
859 Views
Last Modified: 2011-04-14
Can someone please help me with this, I try to use the following interop code, but I always get an error : Access is denied?

this is the code:

      public class LongPathIO
      {
            public static FileStream OpenLongPath(string path)
            {
                  IntPtr handle = NativeMethods.CreateFileW(
                        path,
                        NativeMethods.EFileAccess.GenericWrite,
                        NativeMethods.EFileShare.None,
                        IntPtr.Zero,
                        NativeMethods.ECreationDisposition.OpenAlways,
                        NativeMethods.EFileAttributes.Normal,
                        IntPtr.Zero);
                  if (handle == NativeMethods.INVALID_HANDLE_VALUE)
                  {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                  }
                  else
                  {
                        return new FileStream(handle, FileAccess.ReadWrite, true);
                  }
            }
      }

      internal class NativeMethods
      {
            public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
            [Flags]
                  public enum EFileAccess : uint
            {
                  GenericRead = 0x80000000,
                  GenericWrite = 0x40000000,
                  GenericExecute = 0x20000000,
                  GenericAll = 0x10000000,
            }

            [Flags]
                  public enum EFileShare : uint
            {
                  None = 0x00000000,
                  Read = 0x00000001,
                  Write = 0x00000002,
                  Delete = 0x00000004,
            }

            public enum ECreationDisposition : uint
            {
                  New = 1,
                  CreateAlways = 2,
                  OpenExisting = 3,
                  OpenAlways = 4,
                  TruncateExisting = 5,
            }

            [Flags]
                  public enum EFileAttributes : uint
            {
                  Readonly = 0x00000001,
                  Hidden = 0x00000002,
                  System = 0x00000004,
                  Directory = 0x00000010,
                  Archive = 0x00000020,
                  Device = 0x00000040,
                  Normal = 0x00000080,
                  Temporary = 0x00000100,
                  SparseFile = 0x00000200,
                  ReparsePoint = 0x00000400,
                  Compressed = 0x00000800,
                  Offline= 0x00001000,
                  NotContentIndexed = 0x00002000,
                  Encrypted = 0x00004000,
                  Write_Through = 0x80000000,
                  Overlapped = 0x40000000,
                  NoBuffering = 0x20000000,
                  RandomAccess = 0x10000000,
                  SequentialScan = 0x08000000,
                  DeleteOnClose = 0x04000000,
                  BackupSemantics = 0x02000000,
                  PosixSemantics = 0x01000000,
                  OpenReparsePoint = 0x00200000,
                  OpenNoRecall = 0x00100000,
                  FirstPipeInstance = 0x00080000
            }

            [DllImport("kernel32.dll", EntryPoint="CreateFileW", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true)]
            public static extern IntPtr CreateFileW(
                  string lpFileName,
                  EFileAccess dwDesiredAccess,
                  EFileShare dwShareMode,
                  IntPtr lpSecurityAttributes,
                  ECreationDisposition dwCreationDisposition,
                  EFileAttributes dwFlagsAndAttributes,
                  IntPtr hTemplateFile);
      }
0
Comment
Question by:wimthepimscake
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 16362963
did you add this dll in another project and its still open?
0
 
LVL 6

Author Comment

by:wimthepimscake
ID: 16362976
aha, I did, how do I close it?
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16363041
just close the project that you added it to
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:wimthepimscake
ID: 16363051
It is in the same project but in another dll
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 2000 total points
ID: 16363063
then recompile the whole project with the Dll included
0
 
LVL 4

Expert Comment

by:c_myers
ID: 16363437
FYI, not sure if you saw this already, but it may provide some useful hints in case you run into other problems:

http://pinvoke.net/default.aspx/kernel32.CreateFile
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question