OPENFILNAME

P

PD

I'm getting very frustrated

When I use GetOpenFileName standard dialog, I want to set up the OPENFILENAME structure member 'lpstrInitialDir
to a particular directory

If I use a constant string when setting this member, no problem, the dialog will always open with that directory
However, if I use a pointer to a string variable, then the dialog box opens with whatever directory was used in a previou
use of the Open File or Save As file dialog

If I set a breakpoint, and look at the structure memberprior to calling GetOpenFileName, it has the correct string in there,
but that seems to have no effect

So in my application, unless the user is careful, and notes which directory is showing, they can easily save file
in the wrong place
 
D

Douglas J. Steele

I've never had any problem using a variable to set the initial directory.

What's the actual code you're using? You might compare it to what's at
http://www.mvps.org/access/api/api0001.htm at "The Access Web"

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



PD said:
I'm getting very frustrated.

When I use GetOpenFileName standard dialog, I want to set up the
OPENFILENAME structure member 'lpstrInitialDir'
to a particular directory.

If I use a constant string when setting this member, no problem, the
dialog will always open with that directory.
However, if I use a pointer to a string variable, then the dialog box
opens with whatever directory was used in a previous
use of the Open File or Save As file dialog.

If I set a breakpoint, and look at the structure memberprior to calling
GetOpenFileName, it has the correct string in there,
but that seems to have no effect.

So in my application, unless the user is careful, and notes which
directory is showing, they can easily save files
 
P

Paul Daynes

Hi Douglas

below is the code I'm using

You will see the line that I have commented out...

ofn.lpstrInitialDir = "c:\\C++projects\\b2k5"

If I use this line, GetOpenFileName always opens with this directory, regardless of what directorie
have been selected with previous 'file open' in my own or any other application

However, if I use instead..
ofn.lpstrInitialDir = InitialDirectory
GetOpenFileName will open with directory that was used with previous 'file open'

I can check OPENFILENAME structure after setting members, and the string is identical ?????

This is driving me nuts. Hope you can spot something

Paul Daynes

void ConfigFileOpenDlg (HWND hWnd

OPENFILENAME ofn; //structure for 'Open' standard dialo
char ConfigFilter[] = "Setup Files (*.CFG)\0*.cfg\0"
char pFileName[_MAX_PATH] = "";
char pTitleName[_MAX_FNAME + _MAX_EXT] = "";
int j
HMENU hMenu
HANDLE hFileCfgSave; //file handl
char *pChar

//initialise 'Open' file structur
ofn.lStructSize = sizeof (OPENFILENAME)
ofn.hwndOwner = hWnd
ofn.hInstance = NULL
ofn.lpstrFilter = ConfigFilter
ofn.lpstrCustomFilter = NULL
ofn.nMaxCustFilter = 0
ofn.nFilterIndex = 0
ofn.lpstrFile = pFileName
ofn.nMaxFile = _MAX_PATH
ofn.lpstrFileTitle = pTitleName
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT
ofn.lpstrInitialDir = InitialDirectory
// ofn.lpstrInitialDir = "c:\\C++projects\\b2k5"
ofn.lpstrTitle = "Setup File Open"
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY
ofn.nFileOffset = 0
ofn.nFileExtension = 0
ofn.lpstrDefExt = "cfg"
ofn.lCustData = 0
ofn.lpfnHook = NULL
ofn.lpTemplateName = NULL

//open 'Save' dialog, and save file if returns with valid filenam
if (GetOpenFileName (&ofn)

//open the .CFG fil
hFileCfgSave = CreateFile(
pTitleName, // file name
GENERIC_WRITE | GENERIC_READ, // open for writing
(DWORD) 0,
(LPSECURITY_ATTRIBUTES) NULL, // no security
OPEN_EXISTING, // open if exists
FILE_ATTRIBUTE_NORMAL, // normal file
(HANDLE) NULL); // no attr. template

CloseHandle(hFileCfgSave)
 
D

Douglas J. Steele

What language is that? It looks like C or JavaScript. Access uses VBA. I
think you'll need to find a more appropriate newsgroup to ask your question.
 
P

Paul Daynes

Yeah, Hi Douglas, it is C language. How old are you ????

I think I spotted a couple of things in the link you sent me that I can try.

One thing the OFN_NOCHANGEDIR in my documentation is said to be unsupported, but maybe it is now.
Also, I'm going to play around a bit with the declaration of my string variables, and the zero limiting,
as I suspect the function is seeing a string that is too large or something.

Anyway, thanks, you've given me some more leads to follow.

By the way, I'm in Greece at present, doing some software for our asparagus grading machines.
Normally reside in New Zealand, get to the USA often as we have machines there also.
Paul Daynes.
 
D

Douglas J. Steele

Paul Daynes said:
Yeah, Hi Douglas, it is C language. How old are you ????

Old enough that I programmed in Lifeboat C under CP/M on TRS-80s... <g>

Hope you find an answer to your problem.
 
P

Paul Daynes

Hi Douglas

no luck yet

In my code I know this

lpstrInitialDir, only works with string constants, not string variables

Perhaps its something I'm doing, not sure

Having to work around the problem for now

Paul Daynes
 
P

Paul Daynes

Hi Douglas

I think I know what my problem is now

I am using VC++ 6.0, which I purchased a few years back
Now I suspect the standard include libraries such as windows.h etc on my copy are out of date
because when I try to use a particular flag, I get an error that the it is undefined, yet th
documentation I got from the internet says that this is defined in windows.h

So although I am using common dialogs etc, they are not working as per the latest documention

My question is...
how do I get hold of the latest include file ?????
I had a quick look at Microsoft.com, but couldn't see anything there

Any ideas ????
 
B

Brendan Reynolds

Here's the code I use, Paul. It's VBA code, not C++, but you may be able to
extract the info you need from it. It has all the flags, and works correctly
on Windows 2000 and Windows XP (thanks to some help from fellow Access MVP
Stephen Lebans). Excuse the copyright and disclaimer, I don't usually
include those in newsgroup postings, I added them to this code when I
published it on a web page.

The first two groups of constants are my own constants, used for error
handling. The API flags are those beginning with 'OFN_', 'CDERR_' and
'FNERR_'

'Copyright (C) 2002 Brendan Reynolds
'http://brenreyn.brinkster.net
'
'You have a royalty-free right to use, modify, reproduce and
'distribute this code (and/or any modified version) in any way
'you find useful, provided that you agree that Brendan Reynolds
'has no warranty, obligations or liability for this code.

Option Compare Database
Option Explicit

Private Const mlngcFirstError As Long = 19581

Private Const mlngcUserCancelled As Long = mlngcFirstError + vbObjectError +
0
Private Const mlngcDialogFailure As Long = mlngcFirstError + vbObjectError +
1
Private Const mlngcStructSize As Long = mlngcFirstError + vbObjectError + 2
Private Const mlngcInitialization As Long = mlngcFirstError + vbObjectError
+ 3
Private Const mlngcNoTemplate As Long = mlngcFirstError + vbObjectError + 4
Private Const mlngcNoHInstance As Long = mlngcFirstError + vbObjectError + 5
Private Const mlngcLoadStrFailure As Long = mlngcFirstError + vbObjectError
+ 6
Private Const mlngcFindResFailure As Long = mlngcFirstError + vbObjectError
+ 7
Private Const mlngcLoadResFailure As Long = mlngcFirstError + vbObjectError
+ 8
Private Const mlngcLockResFailure As Long = mlngcFirstError + vbObjectError
+ 9
Private Const mlngcMemAllocFailure As Long = mlngcFirstError + vbObjectError
+ 10
Private Const mlngcMemLockFailure As Long = mlngcFirstError + vbObjectError
+ 11
Private Const mlngcNoHook As Long = mlngcFirstError + vbObjectError + 12
Private Const mlngcRegisterMsgFail As Long = mlngcFirstError + vbObjectError
+ 13
Private Const mlngcSubClassFailure As Long = mlngcFirstError + vbObjectError
+ 14
Private Const mlngcInvalidFileName As Long = mlngcFirstError + vbObjectError
+ 15
Private Const mlngcBufferTooSmall As Long = mlngcFirstError + vbObjectError
+ 16

Private Const mstrcUserCancelled As String = "User cancelled."
Private Const mstrcDialogFailure As String = "Dialog failure."
Private Const mstrcStructSize As String = "Struct size."
Private Const mstrcInitialization As String = "Initialization."
Private Const mstrcNoTemplate As String = "No template."
Private Const mstrcNoHInstance As String = "No instance."
Private Const mstrcLoadStrFailure As String = "Load str failure."
Private Const mstrcFindResFailure As String = "Find res failure."
Private Const mstrcLoadResFailure As String = "Load res failure."
Private Const mstrcLockResFailure As String = "Lock res failure."
Private Const mstrcMemAllocFailure As String = "Mem alloc failure."
Private Const mstrcMemLockFailure As String = "Mem lock failure."
Private Const mstrcNoHook As String = "No hook."
Private Const mstrcRegisterMsgFail As String = "Register msg fail."
Private Const mstrcSubClassFailure As String = "Sub class failure."
Private Const mstrcInvalidFileName As String = "Invalid file name."
Private Const mstrcBufferTooSmall As String = "Buffer too small."

Private Const OFN_READONLY As Long = &H1&
Private Const OFN_OVERWRITEPROMPT As Long = &H2&
Private Const OFN_HIDEREADONLY As Long = &H4&
Private Const OFN_NOCHANGEDIR As Long = &H8&
Private Const OFN_SHOWHELP As Long = &H10&
Private Const OFN_ENABLEHOOK As Long = &H20&
Private Const OFN_ENABLETEMPLATE As Long = &H40&
Private Const OFN_ENABLETEMPLATEHANDLE As Long = &H80&
Private Const OFN_NOVALIDATE As Long = &H100&
Private Const OFN_ALLOWMULTISELECT As Long = &H200&
Private Const OFN_EXTENSIONDIFFERENT As Long = &H400&
Private Const OFN_PATHMUSTEXIST As Long = &H800&
Private Const OFN_FILEMUSTEXIST As Long = &H1000&
Private Const OFN_CREATEPROMPT As Long = &H2000&
Private Const OFN_SHAREAWARE As Long = &H4000&
Private Const OFN_NOREADONLYRETURN As Long = &H8000&
Private Const OFN_NOTESTFILECREATE As Long = &H10000
Private Const OFN_NONETWORKBUTTON As Long = &H20000
Private Const OFN_NOLONGNAMES As Long = &H40000
Private Const OFN_EXPLORER As Long = &H80000
Private Const OFN_NODEREFERENCELINKS As Long = &H100000
Private Const OFN_LONGNAMES As Long = &H200000
Private Const OFN_ENABLEINCLUDENOTIFY As Long = &H400000
Private Const OFN_ENABLESIZING As Long = &H800000

Private Const CDERR_DIALOGFAILURE As Long = &HFFFF&
Private Const CDERR_STRUCTSIZE As Long = &H1&
Private Const CDERR_INITIALIZATION As Long = &H2&
Private Const CDERR_NOTEMPLATE As Long = &H3&
Private Const CDERR_NOHINSTANCE As Long = &H4&
Private Const CDERR_LOADSTRFAILURE As Long = &H5&
Private Const CDERR_FINDRESFAILURE As Long = &H6&
Private Const CDERR_LOADRESFAILURE As Long = &H7&
Private Const CDERR_LOCKRESFAILURE As Long = &H8&
Private Const CDERR_MEMALLOCFAILURE As Long = &H9&
Private Const CDERR_MEMLOCKFAILURE As Long = &HA&
Private Const CDERR_NOHOOK As Long = &HB&
Private Const CDERR_REGISTERMSGFAIL As Long = &HC&
Private Const FNERR_SUBCLASSFAILURE As Long = &H3001&
Private Const FNERR_INVALIDFILENAME As Long = &H3002&
Private Const FNERR_BUFFERTOOSMALL As Long = &H3003&

Private Declare Function apiCommDlgExtendedError Lib "comdlg32.dll" Alias _
"CommDlgExtendedError" () As Long
Private Declare Function apiGetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Public Function GetOpenFileName(Owner As Long, File As String, Filter As
String, _
Title As String, FilterIndex As Long) As String

Const strcProcedure = "GetOpenFileName"

Dim ofn As OPENFILENAME
Dim lngExtendedError As Long

With ofn
.Flags = OFN_HIDEREADONLY + OFN_NOCHANGEDIR + OFN_FILEMUSTEXIST
.hwndOwner = Owner
.lpstrCustomFilter = String$(40, vbNullChar)
.lpstrDefExt = "MDB"
.lpstrFile = File & String$(2048 - Len(File), vbNullChar)
.lpstrFileTitle = String$(2048, vbNullChar)
.lpstrFilter = Filter
.lpstrInitialDir = CurrentProject.Path
.lpstrTitle = Title
.lStructSize = Len(ofn)
.nFilterIndex = FilterIndex
.nMaxCustFilter = Len(.lpstrCustomFilter)
.nMaxFile = Len(.lpstrFile)
.nMaxFileTitle = Len(.lpstrFileTitle)
End With

If apiGetOpenFileName(ofn) = 0 Then
lngExtendedError = apiCommDlgExtendedError()
Select Case lngExtendedError
Case 0
Err.Raise mlngcUserCancelled, strcProcedure, mstrcUserCancelled
Case CDERR_DIALOGFAILURE
Err.Raise mlngcDialogFailure, strcProcedure, mstrcDialogFailure
Case CDERR_STRUCTSIZE
Err.Raise mlngcStructSize, strcProcedure, mstrcStructSize
Case CDERR_INITIALIZATION
Err.Raise mlngcInitialization, strcProcedure,
mstrcInitialization
Case CDERR_NOTEMPLATE
Err.Raise mlngcNoTemplate, strcProcedure, mstrcNoTemplate
Case CDERR_NOHINSTANCE
Err.Raise mlngcNoHInstance, strcProcedure, mstrcNoHInstance
Case CDERR_LOADSTRFAILURE
Err.Raise mlngcLoadStrFailure, strcProcedure,
mstrcLoadStrFailure
Case CDERR_FINDRESFAILURE
Err.Raise mlngcFindResFailure, strcProcedure,
mstrcFindResFailure
Case CDERR_LOADRESFAILURE
Err.Raise mlngcLoadResFailure, strcProcedure,
mstrcLoadResFailure
Case CDERR_LOCKRESFAILURE
Err.Raise mlngcLockResFailure, strcProcedure,
mstrcLockResFailure
Case CDERR_MEMALLOCFAILURE
Err.Raise mlngcMemAllocFailure, strcProcedure,
mstrcMemAllocFailure
Case CDERR_MEMLOCKFAILURE
Err.Raise mlngcMemLockFailure, strcProcedure,
mstrcMemLockFailure
Case CDERR_NOHOOK
Err.Raise mlngcNoHook, strcProcedure, mstrcNoHook
Case CDERR_REGISTERMSGFAIL
Err.Raise mlngcRegisterMsgFail, strcProcedure,
mstrcRegisterMsgFail
Case FNERR_SUBCLASSFAILURE
Err.Raise mlngcSubClassFailure, strcProcedure,
mstrcSubClassFailure
Case FNERR_INVALIDFILENAME
Err.Raise mlngcInvalidFileName, strcProcedure,
mstrcInvalidFileName
Case FNERR_BUFFERTOOSMALL
Err.Raise mlngcBufferTooSmall, strcProcedure,
mstrcBufferTooSmall
Case Else
Debug.Assert False
End Select
Else
GetOpenFileName = TrimNull(ofn.lpstrFile)
End If

End Function

Private Function TrimNull(ByRef strTrimThis As String, _
Optional ByRef boolTwoNulls As Boolean) As String

Dim lngPos As Long
Dim strFindThis As String
Dim strTemp As String

If boolTwoNulls Then
strFindThis = vbNullChar & vbNullChar
Else
strFindThis = vbNullChar
End If
lngPos = InStr(1, strTrimThis, strFindThis)
If lngPos > 0 Then
strTemp = Left$(strTrimThis, lngPos - 1)
Else
strTemp = strTrimThis
End If
If Len(strTemp) > 0 Then
If Right$(strTemp, 1) = vbNullChar Then
strTemp = Left$(strTemp, Len(strTemp) - 1)
End If
End If
TrimNull = strTemp

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top