Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:27:19+00:00 2026-06-06T23:27:19+00:00

Trying to P/Invoke the SetFileTime function from my C# program, I am using the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:27:20+00:00Added an answer on June 6, 2026 at 11:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to invoke the CreateFile method from Kernel32.dll in a C# application,
I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){
I'm trying to invoke a native Windows command from Cygwin using run , but
I am trying to invoke sendmail via PHP's mail function by the following code:
I'm trying to Invoke a Powershell function dynamically using a string. Example $functionToInvoke =
I was trying to invoke a webservice from phonegap android app using jQuery .ajax
I am trying to invoke external program (in my case IECapt.exe) in php function
I am trying to invoke Invoke on a Dispatcher using the following overload: public
I am trying to invoke the method typeOfInstance() in the following (simplest) code: import
I am trying to invoke webservice using jquery ajax call. I am using jasonp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.