You cannot model that with the ref keyword in C#, that always produces a non-null pointer at runtime. You'll actually have to declare it long*
, which requires using the unsafe keyword. Pass the argument with the &local-var
syntax or use null
.
One possible trick to avoid using unsafe is to declare 3 versions of this method with different names, using the DllImport's EntryPoint property to map them all to SetFileTime(). Specifying the ones you don't want to set as IntPtr so you can pass IntPtr.Zero, the one you do want to set as ref long. For example:
[DllImport("kernel32.dll", SetLastError = true, EntryPoint="SetFileTime", ExactSpelling=true)]
internal static extern bool SetFileCreateTime(
IntPtr hFile,
ref long lpCreationTime,
IntPtr lpLastAccessTimeUnused,
IntPtr lpLastWriteTimeUnused);
Repeat for SetFileAccessTime and SetFileWriteTime.