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 1014907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:19:21+00:00 2026-05-16T10:19:21+00:00

I open a file in C# with FileStream, and I got the file handle

  • 0

I open a file in C# with FileStream, and I got the file handle number with this line:

IntPtr file_handle = fs.SafeFileHandle.DangerousGetHandle();

Now I want to pass this handle to C++ code and use this handle value to access the file. Is this possible? How to open a file with merely a file handle in C++?

Thanks.

Update

I use C# to P/Invoke into a C++ Win32 DLL(not a COM DLL). I open the file in C# as FileStream, and pass the handle to the C++. Here is some of my code in the C++ DLL:

extern "C" __declspec(dllexport)void read_file(HANDLE file_handle)
{
    char buffer[64];
    ::printf("\nfile = %d\n",file_handle);

    if(::ReadFile(file_handle,buffer,32,NULL,NULL))
    {
        for(int i=0;i<32;i++)
            cout<<buffer[i]<<endl;
    }
    else
        cout<<"error"<<endl;
}

And here is my C# code:

    [DllImport("...",EntryPoint = "read_file", CharSet = CharSet.Auto)]
    public static extern void read_file(IntPtr file_handle_arg);

But I get this error:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.

Thanks.

  • 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-16T10:19:22+00:00Added an answer on May 16, 2026 at 10:19 am

    You can use win32 calls, the same way the filestream/file constructors do (via p/invoke).

    Cracking it open in .NET Reflector, it looks like it is using this function:

    [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern SafeFileHandle CreateFile(
      string lpFileName,
      int dwDesiredAccess,
      FileShare dwShareMode,
      SECURITY_ATTRIBUTES securityAttrs,
      FileMode dwCreationDisposition,
      int dwFlagsAndAttributes,
      IntPtr hTemplateFile);
    

    Here is an official reference:

    http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

    This is just to open the file, though, as you asked when you said:

    How to open a file with merely a file handle in C++

    If you want to read an already open file, you might have more trouble. I’m not sure. You might be able to use this function:

    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern unsafe int ReadFile(
      SafeFileHandle handle,
      byte* bytes,
      int numBytesToRead,
      IntPtr numBytesRead_mustBeZero,
      NativeOverlapped* overlapped
    );
    

    http://msdn.microsoft.com/en-us/library/aa365467(v=VS.85).aspx

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

Sidebar

Related Questions

I want to open file and read it that I pass from console. Like
FileStream f=new FileStream(c:\\file.xml,FileMode.Create); StreamWriter sf=new StreamWriter(f); sf.WriteLine(stroka); sf.Close(); sf.Dispose(); f.Close(); f.Dispose(); FileStream f1=new FileStream(c:\\file.xml,FileMode.Open);
I have a open file dialog with three filters: QString fileName = QFileDialog::getOpenFileName( this,
I want to open file for append on Windows 7 host using C#/.NET. I
I got this error Cannot access a closed file when I save more than
Suppose that I want my ASP.net web server to open a file and then
with-open-file will read from the beginning of a file. If the file is VERY
The Desktop.getDestop().open(File) launches the associated aplication to open the file. The Desktop class is
How do i open file and get its content as a ByteArray in AS3.
How can i open file in python and write to it multiple times? I

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.