Pfade zu Systemordnern abfragen

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
bluejoke
Beiträge: 1244
Registriert: 08.09.2004 16:33
Kontaktdaten:

Pfade zu Systemordnern abfragen

Beitrag von bluejoke »

Hallo!

Nachdem ich in einer erschöpfender, lang andauernder Suche (ich hab sogar im ApiGuide gesucht!!!) im englischen Forum endlich auf die passende Funktion zur Abfrage von Systemordnern (wie z.B. Favoriten, Dokumente, Verlauf u.s.w.) gestoßen bin, hab ich mir gedacht, dass das der perfekte Eintrag für dieses Forum wäre.

Noch einmal aber muss ich betonen: der Code ist nicht von mir!

Code: Alles auswählen

;This code was originally written by Dev Ashish.
;It is not to be altered or distributed,
;except as part of an application.
;You are free to use it in any application,
;provided the copyright notice is left unchanged.
;
;Code Courtesy of
;Dev Ashish
; 
;   The following table outlines the different DLL versions,
;   and how they were distributed.
;
;   Version     DLL             Distribution Platform
;   4.00          All               Microsoft® Windows® 95/Windows NT® 4.0.
;   4.70          All               Microsoft® Internet Explorer 3.x.
;   4.71          All               Microsoft® Internet Explorer 4.0
;   4.72          All               Microsoft® Internet Explorer 4.01 and Windows® 98
;   5.00          Shlwapi.dll  Microsoft® Internet Explorer 5
;   5.00          Shell32.dll   Microsoft® Windows® 2000.
;   5.80          Comctl32.dll Microsoft® Internet Explorer 5
;   5.81          Comctl32.dll Microsoft® Windows 2000
;
;

;   © Microsoft. Information copied from Microsoft;s
;   Platform SDK Documentation in MSDN
;   (http://msdn.microsoft.com)
;
;   If a special folder does not exist, you can force it to be
;   created by using the following special CSIDL:
;   (Version 5.0)

#CSIDL_FLAG_CREATE = $8000

;   Combine this CSIDL with any of the CSIDLs listed below
;   to force the creation of the associated folder.

;   The remaining CSIDLs correspond to either file system or virtual folders.
;   Where the CSIDL identifies a file system folder, a commonly used path
;   is given as an example. Other paths may be used. Some CSIDLs can be
;   mapped to an equivalent %VariableName% environment variable.
;   CSIDLs are much more reliable, however, and should be used if at all possible.

;   File system directory that is used to store administrative tools for an individual user.
;   The Microsoft Management Console will save customized consoles to
;   this directory and it will roam with the user.
;   (Version 5.0)
#CSIDL_ADMINTOOLS = $30

;   File system directory that corresponds to the user;s
;   nonlocalized Startup program group.
#CSIDL_ALTSTARTUP = $1D

;   File system directory that serves as a common repository for application-specific
;   data. A typical path is C:\Documents and Settings\username\Application Data.
;   This CSIDL is supported by the redistributable ShFolder.dll for systems that do
;   not have the Internet Explorer 4.0 integrated shell installed.
;   (Version 4.71)
#CSIDL_APPDATA = $1A

;   Virtual folder containing the objects in the user;s Recycle Bin.
#CSIDL_BITBUCKET = $A

;   File system directory containing containing administrative tools
;   for all users of the computer.
;   Version 5
#CSIDL_COMMON_ADMINTOOLS = $2F

;   File system directory that corresponds to the nonlocalized Startup program
;   group for all users. Valid only for Windows NT® systems.
#CSIDL_COMMON_ALTSTARTUP = $1E

;   Application data for all users. A typical path is
;   C:\Documents and Settings\All Users\Application Data.
;   Version 5
#CSIDL_COMMON_APPDATA = $23

;   File system directory that contains files and folders that appear on the
;   desktop for all users. A typical path is C:\Documents and Settings\All Users\Desktop.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_DESKTOPDIRECTORY = $19

;   File system directory that contains documents that are common to all users.
;   A typical path is C:\Documents and Settings\All Users\Documents.
;   Valid for Windows NT® systems and Windows 95 and Windows 98
;   systems with Shfolder.dll installed.
#CSIDL_COMMON_DOCUMENTS = $2E

;   File system directory that serves as a common repository for all users; favorite items.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_FAVORITES = $1F

;   File system directory that contains the directories for the common program
;   groups that appear on the Start menu for all users. A typical path is
;   C:\Documents and Settings\All Users\Start Menu\Programs.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_PROGRAMS = $17

;   File system directory that contains the programs and folders that appear on
;   the Start menu for all users. A typical path is
;   C:\Documents and Settings\All Users\Start Menu.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_STARTMENU = $16

;   File system directory that contains the programs that appear in the
;   Startup folder for all users. A typical path is
;   C:\Documents and Settings\All Users\Start Menu\Programs\Startup.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_STARTUP = $18

;   File system directory that contains the templates that are available to all users.
;   A typical path is C:\Documents and Settings\All Users\Templates.
;   Valid only for Windows NT® systems.
#CSIDL_COMMON_TEMPLATES = $2D

;   Virtual folder containing icons for the Control Panel applications.
#CSIDL_CONTROLS = $3

;   File system directory that serves as a common repository for Internet cookies.
;   A typical path is C:\Documents and Settings\username\Cookies.
#CSIDL_COOKIES = $21

;   Windows Desktop—virtual folder that is the root of the namespace..
#CSIDL_DESKTOP = $0

;   File system directory used to physically store file objects on the desktop
;   (not to be confused with the desktop folder itself).
;   A typical path is C:\Documents and Settings\username\Desktop
#CSIDL_DESKTOPDIRECTORY = $10

;   My Computer—virtual folder containing everything on the local computer:
;   storage devices, printers, and Control Panel. The folder may
;   also contain mapped network drives.
#CSIDL_DRIVES = $11

;   File system directory that serves as a common repository for the user;s
;   favorite items. A typical path is C:\Documents and Settings\username\Favorites.
#CSIDL_FAVORITES = $6

;   Virtual folder containing fonts. A typical path is C:\WINNT\Fonts.
#CSIDL_FONTS = $14

;   File system directory that serves as a common repository for
;   Internet history items.
#CSIDL_HISTORY = $22

;   Virtual folder representing the Internet.
#CSIDL_INTERNET = $1

;   File system directory that serves as a common repository for
;   temporary Internet files. A typical path is
;   C:\Documents and Settings\username\Temporary Internet Files.
#CSIDL_INTERNET_CACHE = $20

;   File system directory that serves as a data repository for local
;   (non-roaming) applications. A typical path is
;   C:\Documents and Settings\username\Local Settings\Application Data.
;   Version 5
#CSIDL_LOCAL_APPDATA = $1C

;   My Pictures folder. A typical path is
;   C:\Documents and Settings\username\My Documents\My Pictures.
;   Version 5
#CSIDL_MYPICTURES = $27

;   A file system folder containing the link objects that may exist in the
;   My Network Places virtual folder. It is not the same as CSIDL_NETWORK,
;   which represents the network namespace root. A typical path is
;   C:\Documents and Settings\username\NetHood.
#CSIDL_NETHOOD = $13

;   Network Neighborhood—virtual folder representing the
;   root of the network namespace hierarchy.
#CSIDL_NETWORK = $12

;   File system directory that serves as a common repository for documents.
;   A typical path is C:\Documents and Settings\username\My Documents.
#CSIDL_PERSONAL = $5

;   Virtual folder containing installed printers.
#CSIDL_PRINTERS = $4

;   File system directory that contains the link objects that may exist in the
;   Printers virtual folder. A typical path is
;   C:\Documents and Settings\username\PrintHood.
#CSIDL_PRINTHOOD = $1B

;   User;s profile folder.
;   Version 5
#CSIDL_PROFILE = $28

;   Program Files folder. A typical path is C:\Program Files.
;   Version 5
#CSIDL_PROGRAM_FILES = $2A

;   A folder for components that are shared across applications. A typical path
;   is C:\Program Files\Common.
;   Valid only for Windows NT® and Windows® 2000 systems.
;   Version 5
#CSIDL_PROGRAM_FILES_COMMON = $2B

;   Program Files folder that is common to all users for x86 applications
;   on RISC systems. A typical path is C:\Program Files (x86)\Common.
;   Version 5
#CSIDL_PROGRAM_FILES_COMMONX86 = $2C

;   Program Files folder for x86 applications on RISC systems. Corresponds
;   to the %PROGRAMFILES(X86)% environment variable.
;   A typical path is C:\Program Files (x86).
;   Version 5
#CSIDL_PROGRAM_FILESX86 = $2A

;   File system directory that contains the user;s program groups (which are
;   also file system directories). A typical path is
;   C:\Documents and Settings\username\Start Menu\Programs.
#CSIDL_PROGRAMS = $2

;   File system directory that contains the user;s most recently used documents.
;   A typical path is C:\Documents and Settings\username\Recent.
;   To create a shortcut in this folder, use SHAddToRecentDocs. In addition to
;   creating the shortcut, this function updates the shell;s list of recent documents
;   and adds the shortcut to the Documents submenu of the Start menu.
#CSIDL_RECENT = $8

;   File system directory that contains Send To menu items. A typical path is
;   C:\Documents and Settings\username\SendTo.
#CSIDL_SENDTO = $9

;   File system directory containing Start menu items.
;   A typical path is C:\Documents and Settings\username\Start Menu.
#CSIDL_STARTMENU = $B

;   File system directory that corresponds to the user;s Startup program group.
;   The system starts these programs whenever any user logs onto Windows NT® or
;   starts Windows® 95. A typical path is
;   C:\Documents and Settings\username\Start Menu\Programs\Startup.
#CSIDL_STARTUP = $7

;   System folder. A typical path is C:\WINNT\SYSTEM32.
;   Version 5
#CSIDL_SYSTEM = $25

;   System folder for x86 applications on RISC systems.
;   A typical path is C:\WINNT\SYS32X86.
;   Version 5
#CSIDL_SYSTEMX86 = $29

;   File system directory that serves as a common repository
;   for document templates.
#CSIDL_TEMPLATES = $15

;   Version 5.0. Windows directory or SYSROOT. This corresponds to the %windir%
;   or %SYSTEMROOT% environment variables. A typical path is C:\WINNT.
#CSIDL_WINDOWS = $24

#NOERROR = 0

#MAX_PATH = 260

Procedure.s GetSpecialFolderLocation(lngCSIDL.l)
;   Returns path To a special folder on the machine
;
;   Refer to the comments in declarations for OS and
;   IE dependent CSIDL values.

  Protected lngRet.l
  Protected strLocation.s
  Protected pidl.l
 
  strLocation = Space(#MAX_PATH)

; retrieve a PIDL for the specified location
  lngRet = SHGetSpecialFolderLocation_(0, lngCSIDL, @pidl)
 
  If lngRet = #NOERROR
;   convert the pidl to a physical path
            ;    SHGetPathFromIDList_(pidl, @strLocation) 
            ;    If lngRet = #NOERROR
   BoolReturn = SHGetPathFromIDList_(pidl, @strLocation)
    If BoolReturn
;     If successful, Return the location
      ProcedureReturn RTrim(strLocation)
    EndIf       
;   calling application is responsible for freeing the allocated memory
;   for pidl when calling SHGetSpecialFolderLocation. We have to
;   call IMalloc::Release, but to get to IMalloc, a tlb is required.
;
;   According to Kraig Brockschmidt in Inside OLE,   CoTaskMemAlloc,
;   CoTaskMemFree, and CoTaskMemRealloc take the same parameters
;   as the interface functions and internally call CoGetMalloc, the
;   appropriate IMalloc function, and then IMalloc::Release.
    CoTaskMemFree_(pidl)
  EndIf
EndProcedure

; TEST CALL
Debug GetSpecialFolderLocation(#CSIDL_FAVORITES)

Original-Poster war im englischen Forum: FloHimself
Link zum Original Post: http://purebasic.myforums.net/viewtopic ... 5535#25535


Ich hoff mal, das geht in Ordnung, dass der Code nicht von mir selber ist.


Simon


P.S.: Ich schreib mal noch ein paar Stichpunkte für die Suchfunktion com Forum:
Favoriten, Ordner, Systemordner, Verlauf, Ordner, System, Api
Zuletzt geändert von bluejoke am 05.11.2004 15:45, insgesamt 1-mal geändert.
Benutzeravatar
stbi
Beiträge: 685
Registriert: 31.08.2004 15:39
Wohnort: Cleverly Hills

Re: Pfade zu Systemordnern abfragen

Beitrag von stbi »

müsste das nicht

lngRet = SHGetPathFromIDList_(pidl, @strLocation)

heissen? Sonst macht die zweite If-Abfrage irgendwie keinen Sinn ...
PB 4.02 XP Pro SP2 "Der Code ist willig, aber der Prozessor ist schwach."

Es gibt keine Vista-Witze. Es ist alles wahr!
Benutzeravatar
bluejoke
Beiträge: 1244
Registriert: 08.09.2004 16:33
Kontaktdaten:

Beitrag von bluejoke »

Du hast Recht, stimmen tut die Zeile wohl nicht,
in der API-Hilfe steht auch noch, dass die Funktion nen Boolean-Wert zurück gibt.
Ich hoffe, es stimmt jetzt. Bei mir läufts auf jeden Fall.

Ich habs direkt oben im Code editiert, die Version stimmt jetzt also.
Der alte Code ist ziemlich weit eingerückt und auskommentiert, aber noch da, falls es euch interessiert :D

Danke stbi,

Simon
Benutzeravatar
Kiffi
Beiträge: 10714
Registriert: 08.09.2004 08:21
Wohnort: Amphibios 9

Beitrag von Kiffi »

> Du hast Recht, stimmen tut die Zeile wohl nicht,

nun brauchst Du auch lngRet und BoolReturn nicht mehr

Code: Alles auswählen

Procedure.s GetSpecialFolderLocation(lngCSIDL.l)

  Protected strLocation.s
  Protected pidl.l
 
  strLocation = Space(#MAX_PATH)

  If SHGetSpecialFolderLocation_(0, lngCSIDL, @pidl) = #NOERROR

    ; convert the pidl to a physical path
    If SHGetPathFromIDList_(pidl, @strLocation)
      ProcedureReturn RTrim(strLocation)
    EndIf       
    
    CoTaskMemFree_(pidl)

  EndIf

EndProcedure
Grüße ... Kiffi
Benutzeravatar
bluejoke
Beiträge: 1244
Registriert: 08.09.2004 16:33
Kontaktdaten:

Beitrag von bluejoke »

Hmm ja,
So isses nicht nur kürzer sondern sieht irgendwie auch sauberer aus, wie gesagt, der Code ist auch nicht von mir...
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

Code: Alles auswählen

Structure EMID
  cb.b
  abID.b[1]
EndStructure

Structure ITEMIDLIST
  mkid.EMID
EndStructure

Procedure.s GetSpecialeFolder(folder.l)
  *itemid.ITEMIDLIST = #NULL
  If SHGetSpecialFolderLocation_ (0, folder, @*itemid) = #NOERROR
    location.s = Space (#MAX_PATH)
    If SHGetPathFromIDList_ (*itemid, @location)
      If Right(location, 1) <> "\" : location + "\" : EndIf
      ProcedureReturn location
    EndIf
  EndIf
EndProcedure
Aber bitte nicht für ProgramFiles und ähnlich benutzen. Sondern hier aus der Registrierung lesen!

Immer Windows-Version überprüfen
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
bluejoke
Beiträge: 1244
Registriert: 08.09.2004 16:33
Kontaktdaten:

Beitrag von bluejoke »

Für was sind denn die Strukturen??

Das einzige was dein Code noch zusätzlich macht sind die Backslashes oder?
Hab ich was übersehen?
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

Die andere Procedur übergibt ein Long, was nicht ganz korrekt ist. Sollte aber auch Funktionieren.
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
sharkpeter
Beiträge: 474
Registriert: 29.08.2004 16:18
Wohnort: Dresden
Kontaktdaten:

Beitrag von sharkpeter »

Hallo All,

oder man nehme die UserLib von Andreas, war mal bei PureArea.net gelistet unter "setup"

Gruß Jens
PB3.94 bis 6.xx / (Win98/98SE/ME/XPProSP3/WINVista-X32)/WIN7HP-X64/WIN8PRO-X32/WIN10-64

Nichts wissen macht nichts, sich dumm stellen aber schon.
Benutzeravatar
Andre
PureBasic Team
Beiträge: 1765
Registriert: 11.09.2004 16:35
Computerausstattung: MacBook Core2Duo mit MacOS 10.6.8
Lenovo Y50 i7 mit Windows 10
Wohnort: Saxony / Deutscheinsiedel
Kontaktdaten:

Beitrag von Andre »

sharkpeter hat geschrieben: oder man nehme die UserLib von Andreas, war mal bei PureArea.net gelistet unter "setup"
Ist sie auch noch immer. :mrgreen:

(Kompatibilität mit neuen PB-Versionen ist mir jetzt allerdings nicht bekannt.)
Bye,
...André
(PureBasicTeam::Docs - PureArea.net | Bestellen:: PureBasic | PureVisionXP)
Antworten