Obtaining the PureBasic path

Share your advanced PureBasic knowledge/code with the community.
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Obtaining the PureBasic path

Post by Progi1984 »

A function for obtaining the purebasic path (Windows & Linux)

Thank for Freak & Gnozal :)

Code: Select all

Procedure.s PB_GetPBFolder()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    ;{
      Protected hKey1.l, Type.l, Res.l, Folder.s, lpbData.l, cbData.l, WindowsVersion.l
      cbData = (#MAX_PATH * 2) + 2
      lpbData = AllocateMemory(cbData)
      Folder=""
      hKey1=0
      Type=0
      Res=-1
      Select OSVersion()
        Case #PB_OS_Windows_95, #PB_OS_Windows_98, #PB_OS_Windows_ME;{
          Debug "Detected OS : Windows 95/98/ME"
          Res=RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Classes\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS, @hKey1)
        ;}
        Case #PB_OS_Windows_NT3_51, #PB_OS_Windows_NT_4, #PB_OS_Windows_2000, #PB_OS_Windows_XP, #PB_OS_Windows_Server_2003;{
          Debug "Detected OS : Windows NT/2000/XP"
          Res=RegOpenKeyEx_(#HKEY_CLASSES_ROOT, "Applications\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS, @hKey1)
        ;}
        Default;{ Win Vista / Server 2008
          Debug "Detected OS : Windows Vista/Server 2008"
          Res=RegOpenKeyEx_(#HKEY_CURRENT_USER, "Software\Classes\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS , @hKey1)
        ;}
      EndSelect
      If Res = #ERROR_SUCCESS And hKey1
        If RegQueryValueEx_(hKey1, "", 0, @Type, lpbData, @cbData)=#ERROR_SUCCESS
          Folder = PeekS(lpbData)
          Folder = GetPathPart(StringField(Folder,2,Chr(34)))
        EndIf
        RegCloseKey_(hKey1)
      EndIf
      If lpbData
        FreeMemory(lpbData)
        lpbData=0
      EndIf
      ProcedureReturn Folder
    ;}
    CompilerCase #PB_OS_Linux
    ;{
      Protected hCompiler.l, PBFolder.s
      hCompiler = RunProgram("which", "pbcompiler ", "", #PB_Program_Open|#PB_Program_Read)
      PBFolder = ""
      If hCompiler
        While ProgramRunning(hCompiler)
          PBFolder + ReadProgramString(hCompiler) + Chr(13)
        Wend
        CloseProgram(hCompiler)
      Else
        PBFolder = ""
      EndIf
      If PBFolder = ""
        if GetEnvironmentVariable ( "PUREBASIC_HOME" ) <> ""
          PBFolder = GetEnvironmentVariable ( "PUREBASIC_HOME" )
        endif
      endif
      Procedurereturn PBFolder
    ;}
  CompilerEndSelect
EndProcedure
Last edited by Progi1984 on Wed Nov 26, 2008 9:53 pm, edited 1 time in total.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Hi,

Nice example and thanks for sharing.

Also, the purebasic path should be given in environment variables if pb is correctly installed on linux.

Code: Select all

Procedure.s PB_GetPBFolder()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    ;{
      Protected hKey1.l, Type.l, Res.l, Folder.s, lpbData.l, cbData.l, WindowsVersion.l
      cbData = (#MAX_PATH * 2) + 2
      lpbData = AllocateMemory(cbData)
      Folder=""
      hKey1=0
      Type=0
      Res=-1
      Select OSVersion()
        Case #PB_OS_Windows_95, #PB_OS_Windows_98, #PB_OS_Windows_ME;{
          Debug "Detected OS : Windows 95/98/ME"
          Res=RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Classes\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS, @hKey1)
        ;}
        Case #PB_OS_Windows_NT3_51, #PB_OS_Windows_NT_4, #PB_OS_Windows_2000, #PB_OS_Windows_XP, #PB_OS_Windows_Server_2003;{
          Debug "Detected OS : Windows NT/2000/XP"
          Res=RegOpenKeyEx_(#HKEY_CLASSES_ROOT, "Applications\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS, @hKey1)
        ;}
        Default;{ Win Vista / Server 2008
          Debug "Detected OS : Windows Vista/Server 2008"
          Res=RegOpenKeyEx_(#HKEY_CURRENT_USER, "Software\Classes\PureBasic.exe\shell\open\command", 0, #KEY_ALL_ACCESS , @hKey1)
        ;}
      EndSelect
      If Res = #ERROR_SUCCESS And hKey1
        If RegQueryValueEx_(hKey1, "", 0, @Type, lpbData, @cbData)=#ERROR_SUCCESS
          Folder = PeekS(lpbData)
          Folder = GetPathPart(StringField(Folder,2,Chr(34)))
        EndIf
        RegCloseKey_(hKey1)
      EndIf
      If lpbData
        FreeMemory(lpbData)
        lpbData=0
      EndIf
      ProcedureReturn Folder
    ;}
    CompilerCase #PB_OS_Linux
    ;{
    ProcedureReturn GetEnvironmentVariable ( "PUREBASIC_HOME" )
    ;}
  CompilerEndSelect
EndProcedure

Debug  PB_GetPBFolder()
Best regards

Wolf
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

I improved the first code thanks to Hroudtwolf.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

I don't understand the difference with

Code: Select all

Debug #PB_Compiler_Home 
:roll:
ImageThe happiness is a road...
Not a destination
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

#PB_Compiler_Home will give the path on your system, but in an another computer, the constant will be the same but may be not the folder of Purebasic.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Ok thanks PROGI 8)
ImageThe happiness is a road...
Not a destination
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

This does not work with a fresh install of 4.20+. #HKEY_CLASSES_ROOT has been retired due to compatibility issues from insufficient privileges across user accounts. Only use _CLASSES on anything Windows 9x.
Post Reply