WinAPI GetFullPathName_() question

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

WinAPI GetFullPathName_() question

Post by jassing »

When I run this code, it fails if I assign the result to a variable. Any idea why?

Code: Select all

Macro test() ; just to ensure I use the exact same call
  GetFullPathName_( *file, 1000, *full, #NUL )
EndMacro

*file = @"test.txt"
*full= AllocateMemory(1000)
result=0

; This works:
test() ; test w/o using the result
Debug test() ; show the result.

Debug "Now trying to assign it to a variable"
result = test() ; this fails with either debugger crashing or "overflow of dynamic memory"
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WinAPI GetFullPathName_() question

Post by mk-soft »

Code: Select all

;-TOP by mk-soft

Procedure.s GetFullPathName(FileName.s)
  Protected r1.s, len
  len = GetFullPathName_(FileName, 0, 0, #Null )
  If len
    r1 = Space(len)
    len = GetFullPathName_(FileName, len, @r1, #Null )
  EndIf
  ProcedureReturn r1
EndProcedure

file.s = OpenFileRequester("Open", "", "", 0)
SetCurrentDirectory(GetPathPart(file))

file = GetFilePart(file)
Debug "Filename: " + file
fullpath.s = GetFullPathName(file)
Debug "Fullpath: " + fullpath
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Axolotl
Addict
Addict
Posts: 873
Joined: Wed Dec 31, 2008 3:36 pm

Re: WinAPI GetFullPathName_() question

Post by Axolotl »

Hi, I run your code with a small addition on Win11 and PB 6.04LTS(x86) and PB 6.10 LTS (x64) without any errors.

Code: Select all

Debug "Now trying to assign it to a variable"
result = test() ; this fails with either debugger crashing or "overflow of dynamic memory"
Debug "DEBUG: " + PeekS(*full, result) ; my addition 

Just out of curiosity, why not

Code: Select all

Macro IMHO_Better(FileName) 
  GetCurrentDirectory() + FileName 
EndMacro 

Anyway, the solution by mk-soft is TOP
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: WinAPI GetFullPathName_() question

Post by jassing »

thanks mk-soft.
Axolotl wrote: Sat Apr 27, 2024 1:23 pm Just out of curiosity, why not

Code: Select all

Macro IMHO_Better(FileName) 
  GetCurrentDirectory() + FileName 
EndMacro 
What if the file is elsewhere in the path?
Axolotl
Addict
Addict
Posts: 873
Joined: Wed Dec 31, 2008 3:36 pm

Re: WinAPI GetFullPathName_() question

Post by Axolotl »

jassing wrote: Sat May 11, 2024 1:24 am ....
What if the file is elsewhere in the path?
Well, I only read the article on MSDN (...learn.microsoft....) about the function and I came to the conclusion that it always works with the current directory.
Here's a small collection (pasted from MSDN):
GetFullPathName merges the name of the current drive and directory with a specified file name to determine the full path and file name of a specified file.
....
This function does not verify that the resulting path and file name are valid, or that they see an existing file on the associated volume.
....
Relative paths passed to the GetFullPathName function are interpreted as relative to the process's current directory.
....
Applications should be aware that consecutive calls to the GetFullPathName function with a relative path may produce different results if the current directory changes between the two calls.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply