Solved

How do I use CFIle with path lengths greater than MAX_PATH?

Posted on 2004-04-01
4
888 Views
Last Modified: 2013-11-20
I need to be able to save files into an absolute location whose full path is more than MAX_PATH characters.  My application is not (and can not easily be) in Unicode.  Could I use a relative path name of fewer than MAX_PATH characters and if so, what is the best way to set the desired current directory.  My appication is currently using CFile for file handling.  Please provide a few lines of code by way of an example if not very straightforward.
0
Comment
Question by:PhilipG
4 Comments
 
LVL 15

Assisted Solution

by:lakshman_ce
lakshman_ce earned 100 total points
ID: 10734754
Plz have a look at this for obtaining relative file names,
http://www.codeguru.com/Cpp/misc/misc/fileanddirectorynaming/print.php/c263/

-Lakshman
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10735729
You could try _chdir to set the current directory in stages
eg.
_chdir("C:\\temp");
_chdir(".\\subDir1");
_chdir(".\\SubDir2");
and so on where you split your path (recursive function) and move down the directory chain for each directory.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 total points
ID: 10736448
You'll still run into problems because CFile::Open() calls a function named AfxFullPath() that expands the file name to the full path with the limitation of _MAX_PATH. Look at FILECORE.CPP in the MFC\SRC directory.

What you might try is to derive a new class from CFile and override the Open method fixing the _MAX_PATH limitations. You'll want to look at the FILECORE.CPP to see what else you'd have to override/fix.

Then you could derive from your new class and attempt to open the long file name.

Good Luck,
Steve
0
 

Author Comment

by:PhilipG
ID: 10737161
Thanks all for the very quick response.  I have awarded the points although in the end I adopted a different solution.  The reason for the high point was that I wanted ideas very quickly - it worked.

The solution I used was to use CreateFileW and to convert my path to Unicode using the A2W macro.  I was also able to set the attributes by using SetFileAttributesW and the Unicode path.  The Microsoft info is:

HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object to create or open.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Thanks for the quick response.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 79
while loop over for loop 7 93
Hibernate methods 2 68
wordmultiple challenge 12 121
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now