-1

The OpenFileByID line in test() is giving me System.AccessViolationException Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I am trying to replicate this code example (see the answer), which I'm running in Visual Studio Express 2013 for Windows Desktop. But this example doesn't seem to work for me. It is breaking on the OpenFileByID line in test().

In a nutshell, I am getting a file's ID, then trying to create a file handle from that ID. Later on I plan to use that handle to get information about the file. The reason I'm using IDs is so that I can repair broken links, since a target file's GUID is far more reliable than its presumed location. Help appreciated!

Edit: The file I'm trying to open is an ordinary text file on my Desktop, nothing special.

Wes
  • 1,183
  • 3
  • 23
  • 51
  • You seem to have neglected to post any code. – Jonathan Potter Mar 22 '15 at 00:55
  • The code is in the link. I tried to post it here but it complained that the body was too large. – Wes Mar 22 '15 at 00:56
  • 1
    Get a simple case working in C++ first, only after that is ok, worry about writing C# pinvoke declarations, get the same simple case working in C#, and finally you're ready to integrate into a larger program. – Ben Voigt Mar 22 '15 at 01:29
  • I wanted C# because it has better support for databases, but I am starting to agree, ready to raise the white flag and do it in C++. – Wes Mar 22 '15 at 01:32
  • The P/Invoke signature for `OpenFileById` is odd. `int lpSecurityAttributes` should probably be an `IntPtr` instead. The remaining parameters have unusual types as well. At the very least appropriate `MarshalAs` attributes should be added. – IInspectable Mar 22 '15 at 01:35
  • @Wes I ran your code and it did work for me (in Console application). Have you tried checking the file operations in your application in [process monitor](https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx)? Maybe you could share the filtered .pml file (File - Save As - Only filtered events)? – Sebastian Mar 23 '15 at 18:55

1 Answers1

0

You're not checking to see if you got a valid volume handle - which you might not be. Could be the source of your a/v.

When you're opening the root dir, the doc says you shouldn't use FILE_ATTRIBUTE_NORMAL with any other flags - but you're using it with FILE_FLAG_BACKUP_SEMANTICS. To use FILE_FLAG_BACKUP_SEMANTICS, you have to get privileges for SE_BACKUP_NAME. You'll have to be an admin or a backup operator to do so. I can't imagine that you need that flag.

You can get the volume handle by opening "\\.\C:" (for example)...which is different than that handle to the root folder. I usually open it with GenericRead, but if all you need it for is for OpenFileById, you can specify 0 for access.

Also - adding object IDs to files isn't necessary - the file reference number (FRN) is the master file table identifier for the file - it's the "other" kind of ID you can pass in a FILE_ID_DESCRIPTOR. You can get it from an open file handle calling GetFileInformationByHandle - it's the nFileindexHigh and nFileIndexLow made into a long int. When you move a file, the FRN stays (only it's parent FRN changes). Also, when you rename a file, the FRN doesn't change. The benefit of using this over ObjectID is that you're not altering the volume in order to track a file...and you don't have to use DeviceIOControl - which is a bit of an interop bad dream.

One more thought - OpenFileByID didn't show up until Vista and Windows Server 2008. You're there, right?

Clay
  • 4,999
  • 1
  • 28
  • 45