Page 1 of 1

GetLongPathName API -- getting an error...

Posted: Sun Jan 02, 2011 6:31 am
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  

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

Posted: Sun Jan 02, 2011 7:14 am
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  

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

Posted: Sun Jan 02, 2011 7:22 am
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

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

Posted: Sun Jan 02, 2011 7:41 am
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

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

Posted: Sun Jan 02, 2011 7:50 am
by jassing
now that's nice and clean...thanks for sharing.

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

Posted: Sun Jan 02, 2011 8:01 am
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!

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

Posted: Sun Jan 02, 2011 12:04 pm
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

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

Posted: Sun Jan 02, 2011 12:19 pm
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

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

Posted: Sun Jan 02, 2011 12:25 pm
by Little John
Yes, I want to have a function which I can use in each and any progam.

Regards, Little John

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

Posted: Sun Jan 02, 2011 12:31 pm
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!

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

Posted: Sun Jan 02, 2011 12:42 pm
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

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

Posted: Sun Jan 02, 2011 1:10 pm
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!

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

Posted: Sun Jan 02, 2011 1:33 pm
by Little John
Oh, I see. Thanks for the explanation.

Regards, Little John