Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

OpenFile (kernel32)
 
.
Summary
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr OpenFileMapping(
     uint dwDesiredAccess,
     bool bInheritHandle,
     string lpName);

User-Defined Types:

None.

[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]
static extern int OpenFile([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]string lpFileName, out OFSTRUCT lpReOpenBuff,
   [Enum.OpenFile] uStyle);

Notes:

None.

User-Defined Types:

[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public struct OFSTRUCT
{
  public byte cBytes;
  public byte fFixedDisc;
  public UInt16 nErrCode;
  public UInt16 Reserved1;
  public UInt16 Reserved2;
  [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 128)]
  public string szPathName;
}

Tips & Tricks:

Please add some!

VB.NET Signature:

Private Declare Function OpenFile Lib "kernel32.dll" (ByVal lpFileName As String, ByRef lpReOpenBuff As OFSTRUCT, ByVal uStyle As Long) As Long

User-Defined Types:

<System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)> _
Public Structure OFSTRUCT
    Public cBytes As Byte
    Public fFixedDisc As Byte
    Public nErrCode As UInt16
    Public Reserved1 As UInt16
    Public Reserved2 As UInt16
    <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=128)> _
    Public szPathName As String
End Structure

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
        const UInt32 SECTION_QUERY = 0x0001;
        const UInt32 SECTION_MAP_WRITE = 0x0002;
        const UInt32 SECTION_MAP_READ = 0x0004;
        const UInt32 SECTION_MAP_EXECUTE = 0x0008;
        const UInt32 SECTION_EXTEND_SIZE = 0x0010;
        const UInt32 SECTION_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED|SECTION_QUERY|
            SECTION_MAP_WRITE |      
            SECTION_MAP_READ |      
            SECTION_MAP_EXECUTE |    
            SECTION_EXTEND_SIZE);
        const UInt32 FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;
        private IntPtr hHandle;
        private IntPtr pBuffer;
        unsafe public void Attach(string SharedMemoryName, UInt32 NumBytes)
        {
            if (IntPtr.Zero != hHandle) return;
            hHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, SharedMemoryName);
            if (IntPtr.Zero == hHandle) return;
            pBuffer = MapViewOfFile(hHandle, FILE_MAP_ALL_ACCESS, 0, 0, &NumBytes);
        }
        public void Detach()
        {
            if (IntPtr.Zero != hHandle)
            {
                CloseHandle(hHandle); //fair to leak if can't close
                hHandle = IntPtr.Zero;
            }
            pBuffer = IntPtr.Zero;
            lBufferSize = 0;
        }
Private Const OF_EXIST As Long = &H4000
Private Const OF_ERROR As Long = -1
Private Const OFS_MAXPATHNAME As Long = 128

Public Function FileExists(ByVal sFile As String) As Boolean
    Dim lRetVal As Long
    Dim of As OFSTRUCT

Alternative Managed API:

Do you know one? Please contribute it!

    lRetVal = OpenFile(sFile, of, OF_EXIST)
    If lRetVal <> OF_ERROR Then
        FileExists = True
    Else
        FileExists = False
    End If
End Function

Documentation

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
OpenFile on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions