Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this PInvoke code correct and reliable?

Tags:

In this question I have searched for a simple solution to unblock files. Thanks to all the comments and answer, I have found a simple solution by PInvoking DeleteFile.

It works, but because I've never used file-operations through PInvoke (Win32), I don't know if there are some pitfalls or if there is another method of calling DeleteFile to delete the alternate stream of a file.

What I also don't know is if I have to wrap the call in a try/catch or if it is enough just to look the at the boolean result. In my tests, no exceptions were raised, but I don't know what will happen in the real world.

public class FileUnblocker {      [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]     [return: MarshalAs(UnmanagedType.Bool)]     public static extern bool DeleteFile(string name );      public bool Unblock(string fileName) {         return DeleteFile(fileName+ ":Zone.Identifier");     } } 

Does this code look reliable?

Update
I had posted an incomplete method (the unblock method didn't concatenate the "Zone.Identifier" literal to the file name). I have corrected this now, sorry.

like image 315
HCL Avatar asked Jun 16 '11 16:06

HCL


People also ask

What does PInvoke do?

P/Invoke is a technology that allows you to access structs, callbacks, and functions in unmanaged libraries from your managed code. Most of the P/Invoke API is contained in two namespaces: System and System.

What is PInvoke signature?

* The term PInvoke is derived from the phrase "Platform Invoke". PInvoke signatures are native method signatures, also known as Declare statements in VB.

How does DllImport work?

DllImport attribute uses the InteropServices of the CLR, which executes the call from managed code to unmanaged code. It also informs the compiler about the location of the implementation of the function used.


2 Answers

Calling the native method will never raise an exception. If the file deletion fails, for whatever reason, the call to DeleteFile returns false.

Your P/Invoke code is good. You are correctly using Unicode characters, setting SetLastError to true and the parameter marshalling is correct. To check for errors look for the value of the boolean return from DeleteFile. If it is false (i.e. the call failed) then call Marshal.GetLastWin32Error to find out the Win32 error code.

The most obvious causes for the function to fail are:

  1. The file does not exist.
  2. The alternate stream is not present.
  3. The process does not have sufficient rights to delete the alternate stream.

For 1 and 2 an error code of ERROR_FILE_NOT_FOUND will be returned. For 3 you will be given an error code of ERROR_ACCESS_DENIED.

like image 167
David Heffernan Avatar answered Oct 04 '22 01:10

David Heffernan


I made a small refinement to the code. You can now just pass your startup path to the UnblockPath() function and it will automatically unblock all of the files and sub directory files for your executable. It could be refined further to only search for .exe, .dll, etc.

[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeleteFile(string name);  public static void UnblockPath(string path) {     string[] files = System.IO.Directory.GetFiles(path);     string[] dirs = System.IO.Directory.GetDirectories(path);      foreach (string file in files)     {         UnblockFile(file);     }      foreach (string dir in dirs)     {         UnblockPath(dir);     }  }  public static bool UnblockFile(string fileName) {     return DeleteFile(fileName + ":Zone.Identifier"); } 
like image 32
John Goad Avatar answered Oct 04 '22 02:10

John Goad