May 11, 2024, 08:50:56 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


GetPath

Started by ts-soft, August 01, 2007, 05:09:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ts-soft

GetPath is a translation of a PureBasic Snippet by GPI

I have translate it and enhanced, have fun:

STRING = GetPath(cPath:INT, Opt bShortLong = TRUE:INT)
Parameter:
cPath is a GetPath_xxx Constant or you can use any CSIDL_xxx from the WinAPI documentation
(the GetPath_xxx constants supports some extra Paths, like WinDir, TempDir, AuroraDir and so on!)
bShortLong with this you can set to ShortPathNames (FALSE) or LongPathNames (TRUE = Default)
The result is a string with trailing backslash

Your Inc-File with declarations: ("FileSystem.inc")
' --------------------------------------------------------------------------------
/* Return the path of some system-directorys */
Declare Extern GetPath(cPath:INT, Opt bShortLong = TRUE:INT),STRING
' Constants for GetPath, you can use all CSIDL_ Contants, found in the API
SetId "GetPath_Fonts",        0x00000014
SetId "GetPath_Programs",     0x00000002
SetId "GetPath_SendTo",       0x00000009
SetId "GetPath_StartMenu",    0x0000000B
SetId "GetPath_StartUp",      0x00000007
SetId "GetPath_Templates",    0x00000015
SetId "GetPath_MyDocuments",  0x00000005
SetId "GetPath_AppData",      0x0000001A
' Special Constants for GetPath
SetId "GetPath_Current",      0xFFFFFFFF
SetId "GetPath_System",       0xFFFFFFFE
SetId "GetPath_Windows",      0xFFFFFFFD
SetId "GetPath_Temp",         0xFFFFFFFC
SetId "GetPath_QuickLaunch",  0xFFFFFFFB
SetId "GetPath_ProgramFiles", 0xFFFFFFFA
SetId "GetPath_Aurora",       0xFFFFFFF9
SetId "GetPath_Emergency",    0xFFFFFFF8
SetId "GetPath_IBasicPro",    0xFFFFFFF7

GetPath.eba
AutoDefine "off"

Global GetPath

$INCLUDE "FileSystem.inc"

Declare Import, _GetCurrentDirectory Alias GetCurrentDirectoryA(nBufferLength:INT,lpBuffer:STRING),INT
Declare Import, _GetSystemDirectory Alias GetSystemDirectoryA(lpBuffer:STRING,nSize:INT),INT
Declare Import, _GetWindowsDirectory Alias GetWindowsDirectoryA(lpBuffer:STRING,nSize:INT),INT
Declare Import, _GetTempPath Alias GetTempPathA(nBufferLength:INT,lpBuffer:STRING),INT
Declare Import, _GetShortPathName Alias GetShortPathNameA(lpszLongPath:STRING,lpszShortPath:STRING,nSize:INT),INT
Declare Import, _GetLongPathName Alias GetLongPathNameA(lszShortPath:STRING,lpszLongPath:STRING,nSize:INT),INT
Declare Import, _SHGetSpecialFolderLocation Alias SHGetSpecialFolderLocation(hwnd:UINT,nFolder:INT,ppITEMIDLIST:POINTER),INT
Declare Import, _SHGetPathFromIDList Alias SHGetPathFromIDList(pITEMIDLIST:POINTER,PATH:STRING),INT
Declare Import, _CoTaskMemFree Alias CoTaskMemFree(pidl:POINTER)
Declare Import, _RegOpenKeyEx Alias RegOpenKeyExA(hkey:UINT,subkey:STRING,options:INT,security:INT,result:POINTER),INT
Declare Import, _RegQueryValueEx Alias RegQueryValueExA(hkey:UINT,valuename:STRING,reserved:POINTER,typ:POINTER,keyvalue:POINTER,size:POINTER),INT
Declare Import, _RegCloseKey Alias RegCloseKey(hkey:UINT),INT


Const MAX_PATH            = 260
Const HKEY_CURRENT_USER   = 0x80000001
Const HKEY_LOCAL_MACHINE  = 0x80000002
Const KEY_ALL_ACCESS      = 63
Const REG_DWORD           = 0x04

/* Return the path of some system-directorys */
Sub GetPath(cPath:INT,Opt bShortLong = TRUE:INT),STRING
  Def result[MAX_PATH + 1]:ISTRING
  Def Pidl:Pointer
  Def pPidl:Pointer
  pPidl = &Pidl

  Select cPath
    Case @GetPath_Current       : _GetCurrentDirectory(MAX_PATH, result)
    Case @GetPath_System        : _GetSystemDirectory(result, MAX_PATH)
    Case @GetPath_Windows       : _GetWindowsDirectory(result, MAX_PATH)
    Case @GetPath_Temp          : _GetTempPath(MAX_PATH, result)
    Case @GetPath_QuickLaunch   : result = GetPath(@GetPath_AppData) + "Microsoft\\Internet Explorer\\Quick Launch\\"
    Case @GetPath_ProgramFiles  : result = RegGetValue(HKEY_LOCAL_MACHINE ,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion","ProgramFilesDir")
    Case @GetPath_Aurora
      result = RegGetValue(HKEY_CURRENT_USER,"Software\\IonicWind\\Aurora\\PATHS","BIN")
      result = Left$(result, Len(result) - 3)
    Case @GetPath_Emergency
      result = RegGetValue(HKEY_CURRENT_USER,"Software\\IonicWind\\Emergence BASIC\\PATHS","BIN")
      result = Left$(result, Len(result) - 3)
    Case @GetPath_IBasicPro
      result = RegGetValue(HKEY_CURRENT_USER,"Software\\Pyxia\\IBasic Pro\\PATHS","BIN")
      result = Left$(result, Len(result) - 3)
    Default
      _SHGetSpecialFolderLocation(NULL, cPath, pPidl)
      _SHGetPathFromIDList(Pidl, result)
      _CoTaskMemFree(Pidl)
  EndSelect

  If Right$(result, 1) <> Chr$(92) Then result += Chr$(92)
  If bShortLong
    _GetLongPathName(result, result, MAX_PATH)
  Else
    _GetShortPathName(result, result, MAX_PATH)
  Endif
  Return result
EndSub

' helpfunctions
Sub RegGetValue(hKey:INT, lpSubKey:STRING, Opt ValueName:STRING),STRING
  Def retval:STRING
  Def ln, typ, getdw, ret:INT

  retval=""
  ln=255

  If _RegOpenKeyEx(hKey,lpSubKey, NULL, KEY_ALL_ACCESS, ret) = 0
    _RegQueryValueEx(ret, ValueName,NULL, typ, retval, ln)
    If typ = REG_DWORD
         ln = 4
         _RegQueryValueEx(ret, ValueName, NULL, typ, getdw,ln)
         retval = Str$(getdw)
      endif
   endif
   _RegCloseKey(ret)
   return retval
EndSub

Sorry for my bad english

srod

Now that is bloody handy! Thanks.

:)