Page 1 of 1
WinAPI GetFullPathName_() question
Posted: Sat Apr 27, 2024 11:45 am
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"
Re: WinAPI GetFullPathName_() question
Posted: Sat Apr 27, 2024 12:09 pm
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
Re: WinAPI GetFullPathName_() question
Posted: Sat Apr 27, 2024 1:23 pm
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
Re: WinAPI GetFullPathName_() question
Posted: Sat May 11, 2024 1:24 am
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?
Re: WinAPI GetFullPathName_() question
Posted: Sat May 11, 2024 12:51 pm
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.