VB2005 and API "GetShortPathName Problem

Hi All,

I found yet another problem. I am wondering if VB2005 Express has some "built-in" way to get the short name of a file I have a procedure that I have been using for a long time in VB4, VB5, and VB6 and it has worked fine. However, when I try to upgrade the code in VB2005 Express "Update VB6 code", the procedure totally stops working! Here is how I have it setup:

In My Module, I have this:

Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer

Public Function ShortFileName(ByVal LongFileName As String) As String

Dim GetShortPathName As Object

Dim strBuffer As New VB6.FixedLengthString(255)

Dim lngResult As Integer

'UPGRADE_WARNING: Couldn't resolve default property of object GetShortPathName(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'

lngResult = GetShortPathName(LongFileName, strBuffer.Value, Len(strBuffer.Value))

ShortFileName = Left(strBuffer.Value, lngResult)

End Function

I call it from a form like this:

Dim MyFile, MyInFile AS String

MyOutFile=ShortFileName(MyInFile)

Now, before the conversion...it works perfect in VB6. After the conversion, lngResult returns 0 and, of course, a null string is returned to MyOutFile...

Thanks in advance,

-Ron



Answer this question

VB2005 and API "GetShortPathName Problem

  • ih8usc1014

    Ok, I have been playing with this a bit more & found a solution...this is VB2005 Express.

    I was trying to put the "GetShortName" into a module and calling it from my Form (as I am used to doing). What I found, and it works for some reason, was to put this in my module:

    Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer

    ...And I had to put this actually in the form in my event (Me.ComboBox1.DblClick):

    Dim RetVal As Integer

    Dim ShortFileName As String

    Dim GetShortFileName As String

    ShortFileName = Microsoft.VisualBasic.Space(260)

    RetVal = GetShortPathName(FindMedia, ShortFileName, Microsoft.VisualBasic.Len(ShortFileName))

    GetShortFileName = Microsoft.VisualBasic.Left(ShortFileName, RetVal)

    MsgBox(GetShortFileName)

    Spaghetti code, yes, but at least it works :)

    -Ron


  • rhallet

    Neither of these works in VB2005 Express.

    Thanks just the same :)

    -Ron


  • theManMyth

    It looks like that function can still be called and this link provides details on creating a nice function that you can call in VB.Net without using the VB6 namespace.

    http://vsdntips.com/Tips/VS.NET/VB.NET/439.aspx

    http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName

     

    Give these a try and see if they resolve your problems.   I tested the first and it run just fine under VB.Net 2005

     


  • VB2005 and API "GetShortPathName Problem