Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than comdlg32, prefix the name with the module name and a period.
<DllImport("comdlg32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetOpenFileName(<[In], Out> ByVal ofn As OpenFileName) As Boolean
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Class OpenFileName
Public structSize As Integer = 0
Public dlgOwner As IntPtr = IntPtr.Zero
Public instance As IntPtr = IntPtr.Zero
Public filter As String = Nothing
Public customFilter As String = Nothing
Public maxCustFilter As Integer = 0
Public filterIndex As Integer = 0
Public file As String = Nothing
Public maxFile As Integer = 0
Public fileTitle As String = Nothing
Public maxFileTitle As Integer = 0
Public initialDir As String = Nothing
Public title As String = Nothing
Public flags As Integer = 0
Public fileOffset As Short = 0
Public fileExtension As Short = 0
Public defExt As String = Nothing
Public custData As IntPtr = IntPtr.Zero
Public hook As IntPtr = IntPtr.Zero
Public templateName As String = Nothing
Public reservedPtr As IntPtr = IntPtr.Zero
Public reservedInt As Integer = 0
Public flagsEx As Integer = 0
End Class 'OpenFileName
Public Class LibWrap
'BOOL GetOpenFileName(LPOPENFILENAME lpofn);
Declare Auto Function GetOpenFileName Lib "Comdlg32.dll" ( _
<[In](), Out()> ByVal ofn As OpenFileName) As Boolean
End Class 'LibWrap
Private Function ShowOpen(Optional ByVal filter As String = ("All files" & ChrW(0) & "*.*" & ChrW(0)), _
Optional ByVal title As String = "Open File Dialog...", _
Optional ByVal defext As String = "*", _
Optional ByVal path As String = "C:\") As String
If LibWrap.GetOpenFileName(ofn) Then
Console.WriteLine("Selected file with full path: {0}", ofn.file)
Console.WriteLine("Selected file name: {0}", ofn.fileTitle)
Console.WriteLine("Offset from file name: {0}", ofn.fileOffset)
Console.WriteLine("Offset from file extension: {0}", ofn.fileExtension)
Return ofn.file
End If
Catch ex As Exception
Debug.Assert(False)
Return System.String.Empty
End Try
[<Struct>]
[<StructLayout(LayoutKind.Sequential)>]
type OpenFileName =
val mutable lStructSize: int
val mutable hwndOwner: nativeint
val mutable hInstance: nativeint
val mutable lpstrFilter: string
val mutable lpstrCustomFilter: string
val mutable nMaxCustFilter: int
val mutable nFilterIndex: int
val mutable lpstrFile: string
val mutable nMaxFile: int
val mutable lpstrFileTitle: string
val mutable nMaxFileTitle: int
val mutable lpstrInitialDir: string
val mutable lpstrTitle: string
val mutable Flags: Flags
val mutable nFileOffset: int16
val mutable nFileExtension: int16
val mutable lpstrDefExt: string
val mutable lCustData: nativeint
val mutable lpfnHook: nativeint
val mutable lpTemplateName: string
val mutable pvReserved: nativeint
val mutable dwReserved: int
val mutable FlagsEx: int
let mutable ofn = OpenFileName ()
ofn.lStructSize <- Marshal.SizeOf (ofn)
//The first character of this buffer must be NULL if initialization is not necessary.
ofn.lpstrFile <- Char.MinValue.ToString () + new string (' ', 256)
ofn.nMaxFile <- ofn.lpstrFile.Length
ofn.lpstrFileTitle <- new string (' ', 64)
ofn.nMaxFileTitle <- ofn.lpstrFileTitle.Length
ofn.Flags <- Flags.OFN_FILEMUSTEXIST ||| Flags.OFN_PATHMUSTEXIST
if (GetOpenFileName (&ofn)) then
Choice1Of2 ofn.lpstrFile
else
Choice2Of2 (sprintf "GetOpenFileName failed with the error code: %d" (Marshal.GetLastWin32Error ()))
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).