GetLongPathName API -- getting an error...

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

GetLongPathName API -- getting an error...

Post by jassing »

Could be I've been working way too long on this project....
Getting a memory access error when the api is called...

Invalid Memory Access, read error at address 0

Code: Select all

Prototype.i ptGetLongPathName(*cShort.s, *cLong.s, nBuffLen.l)
Procedure.s GetLongFromShort( cShort.s ) 
	Protected nHnd = OpenLibrary(#PB_Any,"Kernel32.dll")
	Protected cLong.s = cShort
	If nHnd
		Protected GetLongPathName_.ptGetLongPathName = GetFunction(nHnd, "GetLongPathName")
		cLong = Space(#MAX_PATH)
		Debug " Long: (@"+Hex(@cLong)+") = "+cLong
		Debug "Short: (@"+Hex(@cShort)+") = "+cShort
		
		GetLongPathName_(@cShort,@cLong,#MAX_PATH) ; << Invalid Memory Access, read error at address 0
	
		CloseLibrary(nHnd)
	EndIf
  ProcedureReturn cLong
EndProcedure  
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

You have to test the functionpointer "GetLongPathName_" !

Code: Select all

Prototype.i ptGetLongPathName(*cShort, *cLong, nBuffLen.l)

Procedure.s GetLongFromShort(cShort.s)
   Protected nHnd = OpenLibrary(#PB_Any,"Kernel32.dll")
   Protected cLong.s
   If nHnd
      CompilerIf #PB_Compiler_Unicode
        Protected GetLongPathName_.ptGetLongPathName = GetFunction(nHnd, "GetLongPathNameW") ; <---
      CompilerElse
        Protected GetLongPathName_.ptGetLongPathName = GetFunction(nHnd, "GetLongPathNameA") ; <---
      CompilerEndIf
      cLong = Space(#MAX_PATH)
      Debug " Long: (@"+Hex(@cLong)+") = "+cLong
      Debug "Short: (@"+Hex(@cShort)+") = "+cShort
      
      If GetLongPathName_ ; <---
        GetLongPathName_(@cShort,@cLong,#MAX_PATH) ; << Invalid Memory Access, read error at address 0
      EndIf
      CloseLibrary(nHnd)
   EndIf
  ProcedureReturn cLong
EndProcedure  
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: GetLongPathName API -- getting an error...

Post by jassing »

THANK YOU!!!
I had been working all day on this; and then this requirement got added in; and I slapped that procedure together -- I just couldn't see the error of my ways.
A clear headed 2nd pair of eyes always spots that stuff.

-j
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

Here my Version:

Code: Select all

Import "Kernel32.lib"
  GetLongPathNameW(cShort.p-Unicode, *cLong, nBuffLen.l)
EndImport

Procedure.s GetLongFromShort(cShort.s)
  Protected cLong.s = Space(#MAX_PATH)
  
  If GetLongPathNameW(cShort, @cLong, #MAX_PATH)
    ProcedureReturn PeekS(@cLong, -1, #PB_Unicode)
  EndIf
EndProcedure
greetings
Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: GetLongPathName API -- getting an error...

Post by jassing »

now that's nice and clean...thanks for sharing.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

jassing wrote:now that's nice and clean...thanks for sharing.
thx, but is not correct on unicode systems with very long paths!

Code: Select all

Import "Kernel32.lib"
  GetLongPathNameW(cShort.p-Unicode, *cLong, nBuffLen.l)
EndImport

Procedure.s GetLongFromShort(cShort.s)
  Protected cLong.s, length.l
  
  length = GetLongPathNameW(cShort, @cLong, 0)
  If length
    cLong = Space(length)
    If GetLongPathNameW(cShort, @cLong, length * SizeOf(Character))
      ProcedureReturn PeekS(@cLong, -1, #PB_Unicode)
    EndIf    
  EndIf
EndProcedure
This one should always correct!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetLongPathName API -- getting an error...

Post by Little John »

What about GetLongPathNameA? Shouldn't the code contain something like

Code: Select all

Import "Kernel32.lib"
   CompilerIf #PB_Compiler_Unicode
      GetLongPathNameW(cShort.p-Unicode, *cLong, nBuffLen.l)
   CompilerElse
      GetLongPathNameA(cShort.p-ascii, *cLong, nBuffLen.l)
   CompilerEndIf
EndImport
:?:

Regards, Little John
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

Little John wrote:What about GetLongPathNameA? Shouldn't the code contain something like
...
The pseudotypes only required if you use only one version for both, unicode and ascii!

Greetings
Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetLongPathName API -- getting an error...

Post by Little John »

Yes, I want to have a function which I can use in each and any progam.

Regards, Little John
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

Little John wrote:Yes, I want to have a function which I can use in each and any progam.

Regards, Little John
For this is my snippet!
Unicode- and Ascii, x86- and x64-Support :wink:

Requires Win2K and higher!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetLongPathName API -- getting an error...

Post by Little John »

ts-soft wrote:
Little John wrote:Yes, I want to have a function which I can use in each and any progam.

Regards, Little John
For this is my snippet!
Unicode- and Ascii, x86- and x64-Support :wink:
Sorry, then I don't understand what you mean with
The pseudotypes only required if you use only one version for both, unicode and ascii!
And how can GetLongPathNameW work in ASCII mode?

Regards, Little John
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetLongPathName API -- getting an error...

Post by ts-soft »

Little John wrote:And how can GetLongPathNameW work in ASCII mode?
The first parameter is changed to unicode with pseudotype and the result by peeks with #PB_Unicode!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetLongPathName API -- getting an error...

Post by Little John »

Oh, I see. Thanks for the explanation.

Regards, Little John
Post Reply