Obtain real path (I need translation to Purebasic)

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Nov 27, 2016 6:38 am

Obtain real path (I need translation to Purebasic)

Post 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);
}
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
novablue
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Nov 27, 2016 6:38 am

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

Post by novablue »

Thanks, your code only accepts files how can i make this work with paths?
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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.
Post Reply