Ask the C++ Pro 10-Minute Solutions

Convert Path to Long Path Name
By Jonathan Wood

In a recent 10-Minute Solution, I pointed out that when a user drops a filename on your application's icon, the system passes the short path name to your application's command line. The system does this, presumably, because it is not able to tell whether your application will correctly handle long filenames. Surprisingly, MFC does not convert the path name to the long path name. This can make your applications look rather unprofessional when they display the short version of a file's name.

At that time, I presented code to convert a filename on the command line to the long version. This month, I'd like to expand on this topic and address a limitation of the code I presented at that time. Although my previous code worked, the routine I presented only extracted the long version of the filename, and not the entire path. So I'm presenting a more generic routine that converts any existing pathname to its long version.

The technique I used was to call FindFirstFile with a given filename. FindFirstFile returns the long version of an existing filename or directory. Because FindFirstFile only returns the long version of the file or subdirectory name (not the entire path), the code below parses out each directory or file name, converting them to the long versions one at a time.

As I did before, I'll point out that the API does provide a GetLongPathName that does what the code below does. However, that routine is only available in Windows 98 or Windows NT 2000 or later. If you want your code to work on all Win32 operating systems, you'll need to avoid the API routine.

#define PATH_DELIMITER '\\'

//
BOOL MyGetLongPathName(LPTSTR lpszShortPath, LPTSTR lpszLongPath)
{
   TCHAR *p, *pStart; 
   TCHAR buff[MAX_PATH];
   WIN32_FIND_DATA wfd;
   HANDLE handle;
   int i;

   // Keep strings null-terminated
   *buff = '\0';
   *lpszLongPath = '\0';
   //
   p = lpszShortPath;
   while (p != NULL) {
      // Find next 
      p = strchr(pStart = p, PATH_DELIMITER);
      // See if a token was found
      if (p != pStart) {
         i = strlen(buff);
         // Append token to temp buffer
         if (p == NULL)
            strcpy(buff + i, pStart);
         else {
            *p = '\0';
            strcpy(buff + i, pStart);
            *p = PATH_DELIMITER;
         }
         // Copy token unmodified if drive specifier
         if (strchr(buff + i, ':') != NULL)
            strcat(lpszLongPath, buff + i);
         else {
            // Convert token to long name
            handle = FindFirstFile(buff, &wfd);
            if (handle == INVALID_HANDLE_VALUE)
               return FALSE;
            strcat(lpszLongPath, wfd.cFileName);
            FindClose(handle);
         }
      }
      // Copy terminator
      if (p != NULL) {
         buff[i = strlen(buff)] = *p;
         buff[i + 1] = '\0';
         lpszLongPath[i = strlen(lpszLongPath)] = *p;
         lpszLongPath[i + 1] = '\0';
      }
      // Bump pointer
      if (p) p++;
   }
   return TRUE;
}

 
Other 10-Minute Solutions
 How to Change the Mouse Pointer without Flicker
 Setting Full Row Selection in ListView Control
 Automating Type Conversions with stringstream Objects
 Improving Memory Reallocation with Vectors
 How to Use <fstream> Classes for File I/O
 Casting About for Safe Typecasting
 Overloading Operator + the Right Way
 How to Create Persistent Objects
 Making Linked Lists More User-Friendly
 Preventing Glitches in Signal Processing
 Forcing Object Allocation on the Free-store
 Using String-Based Data Validation
 Implementing the 'Resource Acquisition Is Initialization' Idiom
 Simple Locks for Data Files
 Template Specializations
 Exception Handling
 Using Bit Fields in Data Optimization
 Using the Transform() Algorithm to Change a String's Case
 Use RTTI for Dynamic Type Identification
 Choosing the Right swap () Implementation
 Take Charge and Initialize Your Own Data
 Share Data Among Objects Using the Monostate Design Pattern
 String Manipulation Made Easy with std::string Algorithms
 Using typedef to Curb Miscreant Code
 Managing Objects' Construction Order
 Bitwise Operators: Combining Efficiency and Ease of Use
 Use Function Adapters to Extend Generic Algorithms' Usage
 Simplify Callback Dispatching with Enumerated Indexes
 Streamline Your Bulk I/O Operations with Stream Iterators
 Optimize Your Member Layout
 Preserve Code Safety with Conversion Operators
 Modify Your Base Class Interface in Derived Classes
 Tackle Common Programming Tasks Using the New <tuple> Library
 Use Local Classes for Proper Cleanup in Exception-enabled Apps
 Use multimap to Create Associative Containers with Duplicate Keys
 Enforcing Compile-time Constraints
 Facilitate Directory Operations with the <dirent.h> and <dir.h> Libraries
 Spruce Up Your Built-in Arrays
 Target 32- and 64-bit Platforms Together with a Few Simple Datatype Changes
 Restrict Object Allocation to Specific Memory Types
 Use the Pimpl Idiom to Reduce Compilation Time and Enhance Encapsulation
 Automate Resource Management with shared_ptr
 The Quick and Dirty Way to Add
 Pointing to Class Members
 Detecting Keystrokes While Your Application is Busy
 Linked Lists
 Programming the System Tray
 Create a "Universal" DLL
 Convert Path to Long Path Name
 Constructing an Object at a Pre-Determined Memory Position
 Declaring Classes and Member Functions in a Namespace
 Using the auto_ptr Class Template to Facilitate Dynamic Memory Management
 Using the random_shuffle() Algorithm to Randomize a Sequence of Elements
 Defining a Function Object
 Implementing the Singleton Design Pattern
 Declaring Function Pointers and Implementing Callbacks
 Overloading Operator << for a User-Defined Type
 Implementing a Stopwatch Class for Performance Measurements
 Creating and Accessing Environment Variables
 Executing an Object's Member Function in a Separate Thread
 Creating Heterogeneous Containers
 Overriding New and Delete
 Time and Date Manipulation
  Defining Functions with a Variable Argument List
 Optimize Abstract Operations with Function Templates




Sponsored Links


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers