Solved

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

Posted on 2004-04-01
4
890 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

772 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