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

  • Home
  • SEARCH
  • 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 7650659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:18:37+00:00 2026-05-31T11:18:37+00:00

I trying to marshaling C# code from WinApi functions.. But i understand.. WHYYY WHYY

  • 0

I trying to marshaling C# code from WinApi functions..
But i understand.. WHYYY WHYY she dont work! Filepath – is correct, handle is taken.
Can anyone help me?

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;

namespace ChangeFileTime
{

    class Program
    {

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetFileTime(IntPtr hFile, ref long lpCreationTime,
                                                            ref long lpLastAccessTime, 
                                                            ref long lpLastWriteTime);

        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateFile(string lpFilename,
                                              uint dwDesiredAccess,
                                              uint dwShareMode,
                                              IntPtr SecurityAttributes,
                                              uint dwCreationDisposition,
                                              uint dwFlagsAndAttributes,
                                              IntPtr hTemplateFile);


        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);

        static void Main(string[] args)
        {
            const uint GENERIC_READ = 0x80000000;
            const uint OPEN_EXISTING = 3;
            const uint FILE_SHARE_WRITE = 0x00000002;
            const uint FILE_ATTRIBUTE_NORMAL = 128;

            IntPtr ptr = CreateFile("C:\\file.txt", GENERIC_READ,
                                                    FILE_SHARE_WRITE,
                                                    IntPtr.Zero,
                                                    OPEN_EXISTING,
                                                    FILE_ATTRIBUTE_NORMAL,
                                                    IntPtr.Zero);

            DateTime creation_time = new DateTime(1990, 12, 14);

            long file_time = creation_time.ToFileTime();
            DateTime time = DateTime.FromFileTime(file_time);

            SetFileTime(ptr, ref file_time, ref file_time, ref file_time);
            int a = 20;
        }
    }
}

I think i got mistake.. i try to write C++ code, and she work fine.. but why in C# dont work?

  • 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-05-31T11:18:38+00:00Added an answer on May 31, 2026 at 11:18 am

    From MSDN:

    The handle must have been created using the CreateFile function with
    the FILE_WRITE_ATTRIBUTES

    This should work:

    const uint FILE_WRITE_ATTRIBUTES = 0x0100;
    IntPtr ptr = CreateFile("C:\\file.txt", FILE_WRITE_ATTRIBUTES, //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to use managed/unmanaged code interop, but I've hit a
I'm trying to marshal a struct from a byte[]. Each part works except marshaling
I'm trying to send an image from C++ to C# with an interop (marshaling)
trying to work out how to install zipArchive extension with MAMP, but not having
I am trying to call out to a legacy dll compiled from FORTRAN code.
I'm trying to abstract marshaling IntPtr to and from structs (actually class, so it's
Trying to get my css / C# functions to look like this: body {
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to honor a feature request from our customers, I'd like that my application,
I'm trying to access some Ghostscript functions like so: [DllImport(@C:\Program Files\GPLGS\gsdll32.dll, EntryPoint = gsapi_revision)]

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.