Page 1 of 1

Obtain real path (I need translation to Purebasic)

Posted: Fri Jul 05, 2024 5:33 pm
by novablue
The following code should give me the real path in case a part of the path is going through a symbolic link.
If someone could translate it to Purebasic i would appreciate it.

Code: Select all

void realpath(const char *filename, wchar_t *pathbuf, int size)
{
    OFSTRUCT of;
    HANDLE file = (HANDLE)OpenFile(filename,&of,OF_READ);
    GetFinalPathNameByHandle(file,pathbuf,size,FILE_NAME_OPENED);
    CloseHandle(file);
}

Re: Obtain real path (I need translation to Purebasic)

Posted: Fri Jul 05, 2024 6:45 pm
by infratec

Code: Select all

#FILE_NAME_OPENED = $8


Import "shell32.lib"
  GetFinalPathNameByHandleW.l(hFile.i, *lpszFilePath, cchFilePath.l, dwFlags.l)
EndImport

Procedure.s realpath(filename$)
  
  Protected File.i, Path$, *PathBuf, PathLen.i
  
  File = ReadFile(#PB_Any, filename$)
  If File
    *PathBuf = AllocateMemory(2048)
    If *PathBuf
      PathLen = GetFinalPathNameByHandleW(FileID(File), *PathBuf, MemorySize(*PathBuf), #FILE_NAME_OPENED)
      If PathLen
        Path$ = PeekS(*PathBuf, PathLen)
      EndIf
      FreeMemory(*PathBuf)
    EndIf
    CloseFile(File)
  EndIf
  
  ProcedureReturn Path$
  
EndProcedure

Re: Obtain real path (I need translation to Purebasic)

Posted: Fri Jul 05, 2024 8:36 pm
by novablue
Thanks, your code only accepts files how can i make this work with paths?

Re: Obtain real path (I need translation to Purebasic)

Posted: Sat Jul 06, 2024 12:17 pm
by infratec
:?: :?: :?:

Can you open a path?

I don't think so.
So FILE_NAME_OPENED can never do this.

Is this is working with you provided code?

You only asked for a conversation.
Next time please ask directly for what you want.