Trying to P/Invoke the SetFileTime
function from my C# program, I am using the following signature:
[DllImport(@"kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetFileTime(
IntPtr hFile,
ref long lpCreationTime,
ref long lpLastAccessTime,
ref long lpLastWriteTime);
The documentation states the following unmanaged signature:
BOOL WINAPI SetFileTime(
__in HANDLE hFile,
__in_opt const FILETIME *lpCreationTime,
__in_opt const FILETIME *lpLastAccessTime,
__in_opt const FILETIME *lpLastWriteTime
);
It says for the 2nd, 3rd and 4th parameter that they can be optional:
This parameter can be NULL if the application does not need to change this information.
And this is exactly what I want to do: Pass only one of the three date time values and have the others set to null
.
I'm completely lost on how to express this in terms of P/Invoke signature.
Therefore my question is:
Which is the correct P/Invoke signature (or the correct way of calling) to be able to pass null
to the SetFileTime
function parameters?
Edit 1:
Since the FileInfo
class already provides a writable property for setting the file times, you may ask, why I'm doing it on my own instead of using the class.
The reason is that I'm planning to use this function in my Long Paths library which is used to overcome the MAX_PATH
limit and therefore cannot use the standard .NET functions.
Therefore using the FileInfo
class is not an option, unfortunately.
Edit 2:
I've solved it by using the solution suggested by Hans. After that, I found a user-contributed comment below the function that says:
In addition, specifying a zero value for one of the parameters has the same effect as specifying NULL.
Although I did not check whether this is true, it might still be an option for others having a similar requirement to mine.