Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileListBox Shows Short Filenames

Status
Not open for further replies.

DQR

Technical User
Dec 18, 2002
30
GB

I'm running VB5 under Windows NT. I've got a FileListBox which allows me to select one of the files with a given extension from within a directory that I've specified. What it's actually showing are the correct files, but expressed as short filenames, i.e. the names as they would have appeared under DOS. This is causing the program to crash when I try to select that file - because it doesn't exist. How do I get the FileListBox to show the long file name? If that's not possible under Windows NT, can I convert the filename from a short name into a long one?

Cheers,
DQR
 

Yes, the API is ...
[tt]
GetLongPathName
The GetLongPathName function converts the specified path to its long form. If no long path is found, this function simply returns the specified name.

DWORD GetLongPathName(
LPCTSTR lpszShortPath, // file name
LPTSTR lpszLongPath, // path buffer
DWORD cchBuffer // size of path buffer
);
Parameters
lpszShortPath
[in] Pointer to a null-terminated path to be converted.
Windows 2000 and later: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions.

Windows 98 and later: This string must not exceed MAX_PATH characters.

lpszLongPath
[out] Pointer to the buffer to receive the long path. You can use the same buffer you used for the lpszShortPath parameter.
cchBuffer
[in] Specifies the size of the buffer, in TCHARs.
Return Values
If the function succeeds, the return value is the length of the string copied to the lpszLongPath parameter, in TCHARs. This length does not include the terminating null character.

If the lpszLongPath buffer is too small to contain the path, the return value is the size of the buffer, in TCHARs, required to hold the path. Therefore, if the return value is greater than cchBuffer, call the function again with a buffer that is large enough to hold the path.

If the function fails for any other reason, the return value is zero. To get extended error information, call GetLastError.

Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Requires Windows 98 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows 2000.

See Also
File I/O Overview, File I/O Functions, GetFullPathName, GetShortPathName

[/tt]

Good Luck

 

Thanks for your help on this; however I'm afraid I'm struggling with the solution, as I'm not primarily a programmer. Though I can understand in principle what you're recommending, I can't see how to implement it, as I'm not familiar with APIs or their usage.

I understand that I have to create a function called GetLongPathName. Where does this function go, and what should it contain? I've tried reproducing the block of code beginning with "DWORD GetLongPathName" in my function definitions, and got back an assortment of syntax errors, so I'm assuming that this isn't the correct place for it. Whereabouts in the program do I set the MAX_PATH parameter, and what should the value of it be? I can't find any reference to GetFullPathName or GetShortPathName in the online help: would I expect to find them there? So many questions...

Cheers,
DQR
 

Do you have the Visual Basic Design Environment? If so you should be able to find under addins the API declaration dialog, or if you do not you can goto MS's site and search for API. You will get a couple of results at the top. What you are looking for is the API home page. Oops! just searched for the API in the API viewer and its not there??? but MAX_PATH is so here is a test for you. This works on win2kSP2 with V-Studio6.0SP5+.NET

[tt]
Option Explicit

Private Const MAX_PATH = 260

Private Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long


Private Sub Form_Load()

'declare prodedural variables
Dim Result As Long, MyShortFileName As String, MyFullFileName As String

'initialize output string
MyFullFileName = String(MAX_PATH, Chr(0))

'set input short file name
MyShortFileName = "Your Path File Name Here" & Chr(0)

'execute API
Result = GetLongPathName(MyShortFileName, MyFullFileName, MAX_PATH)

'Test Result
If Result = 0 Then

'error has occured, use get last error or
MsgBox Err.LastDllError

Else

If Result > MAX_PATH Then

'it needs a larger buffer, this should never happen
DoEvents

Else

'clean up output and display
MyFullFileName = Left(MyFullFileName, InStr(1, MyFullFileName, Chr(0)) - 1)
MsgBox MyFullFileName

End If

End If

End Sub
[/tt]

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top