Page 1 of 1

Obtaining the PureBasic path

Posted: Wed Nov 26, 2008 7:49 pm
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

Posted: Wed Nov 26, 2008 8:05 pm
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

Posted: Wed Nov 26, 2008 9:54 pm
by Progi1984
I improved the first code thanks to Hroudtwolf.

Posted: Thu Nov 27, 2008 9:14 am
by Kwai chang caine
I don't understand the difference with

Code: Select all

Debug #PB_Compiler_Home 
:roll:

Posted: Thu Nov 27, 2008 11:36 am
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.

Posted: Tue Dec 02, 2008 10:57 am
by Kwai chang caine
Ok thanks PROGI 8)

Posted: Mon Dec 15, 2008 4:46 pm
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.