Click to See Complete Forum and Search --> : Quick/Fast/Urgent: How to get DOS path


BrewGuru99
October 18th, 1999, 01:09 PM
How do I get the ms dos path for any given path? It would be perfect if I could pass a function a string for the long path, and it would return the dos path.

I swear I saw this before, but couldn't find it. I hope I wasn't dreaming!

Lothar Haensler
October 19th, 1999, 02:15 AM
there is a GetShortPathname API

Lothar Haensler
October 19th, 1999, 03:00 AM
here is a complete sample with a callable function:


option Explicit
private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (byval lpszLongPath as string, byval lpszShortPath as string, byval cchBuffer as Long) as Long
private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
(byval dwFlags as Long, lpSource as Any, _
byval dwMessageId as Long, byval dwLanguageId as Long, _
byval lpBuffer as string, byval nSize as Long, _
Arguments as Long) as Long
private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

private Sub Command1_Click()
MsgBox gsp("c:\programme\microsoft visual Studio")
End Sub

Function gsp(byval longpath as string) as string
Dim lResult as Long
Dim sp as string * 255
lResult = GetShortPathName(longpath, sp, len(sp))
If lResult > 0 then
gsp = Left(sp, lResult)
else
Dim strbuffer as string * 255
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, byval 0, Err.LastDllError, _
0, strbuffer, len(strbuffer), 0
Err.Raise Number:=vbObjectError + 1001, _
Description:=strbuffer
End If
End Function

Crazy D @ Work
October 19th, 1999, 07:00 AM
Lothar, just a little hint for your format message:
add FORMAT_MESSAGE_IGNORE_INSERTS as constant, else VB will crash with certain errors (eg. error 129 will cause one) (error that accept inserts (%1, %2, etc, as in sprinf in C)

Crazy D @ Work :-)