Modul "FileInfo"

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
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Modul "FileInfo"

Beitrag von Sicro »

Ich habe gerade gemerkt, dass ich das Modul "FileInfo" nur im Thread "WinAPI Library (Snippets)" veröffentlicht habe.
Hier ist er aber besser aufzufinden.

Code: Alles auswählen

IncludeFile "FileInfo[Win].pbi"

Define File$ = "C:\WINDOWS\system32\notepad.exe"
  
Debug "FileComments:         " + FileInfo::GetFileComments(File$)
Debug "FileCompanyName:      " + FileInfo::GetFileCompanyName(File$)
Debug "FileDescription:      " + FileInfo::GetFileDescription(File$)
Debug "FileInternalName:     " + FileInfo::GetFileInternalName(File$)
Debug "FileLegalCopyright:   " + FileInfo::GetFileLegalCopyright(File$)
Debug "FileLegalTrademarks:  " + FileInfo::GetFileLegalTrademarks(File$)
Debug "FileOriginalFilename: " + FileInfo::GetFileOriginalFilename(File$)
Debug "FileVersion:          " + FileInfo::GetFileVersion(File$)
Debug "FixedFileType:        " + FileInfo::GetFixedFileType(File$)
Debug "FixedFileVersion:     " + FileInfo::GetFixedFileVersion(File$)
Debug "FixedProductVersion:  " + FileInfo::GetFixedProductVersion(File$)
Debug "ProductName:          " + FileInfo::GetProductName(File$)
Debug "ProductVersion:       " + FileInfo::GetProductVersion(File$)
Debug "FileBitSystem:        " + FileInfo::GetFileBitSystem(File$)
  
Define Image = FileInfo::GetIcon(File$, 32)
If Image
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 100, 100, "Icon Extraction", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ImageGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), ImageID(Image))
    While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
  EndIf
Else
  Debug "GetIcon: Error"
EndIf
https://github.com/SicroAtGIT/PureBasic ... iv-Rebirth
Pfad: File/FileInfo[Win].pbi
Zuletzt geändert von Sicro am 25.07.2018 19:12, insgesamt 9-mal geändert.
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
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

Re: Modul "FileInfo"

Beitrag von ts-soft »

:allright: Danke fürs Teilen!

Ich hab da aber noch einen kleinen Verbesserungsvorschlag :wink:

Am Anfang des Codes, das EnableExplicit weglassen und das Beispiel so einfügen:

Code: Alles auswählen

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  Define File.s = "notepad.exe"
  
  Debug "FileComments:         " + FileInfo::GetFileComments(File)
  Debug "FileCompanyName:      " + FileInfo::GetFileCompanyName(File)
  Debug "FileDescription:      " + FileInfo::GetFileDescription(File)
  Debug "FileInternalName:     " + FileInfo::GetFileInternalName(File)
  Debug "FileLegalCopyright:   " + FileInfo::GetFileLegalCopyright(File)
  Debug "FileLegalTrademarks:  " + FileInfo::GetFileLegalTrademarks(File)
  Debug "FileOriginalFilename: " + FileInfo::GetFileOriginalFilename(File)
  Debug "FileVersion:          " + FileInfo::GetFileVersion(File)
  Debug "FixedFileType:        " + FileInfo::GetFixedFileType(File)
  Debug "FixedFileVersion:     " + FileInfo::GetFixedFileVersion(File)
  Debug "FixedProductVersion:  " + FileInfo::GetFixedProductVersion(File)
  Debug "ProductName:          " + FileInfo::GetProductName(File)
  Debug "ProductVersion:       " + FileInfo::GetProductVersion(File) 
CompilerEndIf
So kann man das als Include-Datei nutzen ohne Änderungen durchzuführen!

Gruß
Thomas
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
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: Modul "FileInfo"

Beitrag von Sicro »

Code angepasst und ihn zum CodeArchiv hinzugefügt. Danke! :)
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Rinzwind
Beiträge: 2
Registriert: 07.02.2017 11:39

Re: Modul "FileInfo"

Beitrag von Rinzwind »

This one doesn't work correctly with Windows 10 apps (Windows Explorer shows file details correctly, but PB functions return empty strings).

Eg. C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1712.3351.0_x64__8wekyb3d8bbwe\Calculator.exe

Any solution?
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: Modul "FileInfo"

Beitrag von Sicro »

Hello Rinzwind, thank you for pointing out that the code doesn't work on Windows 10 apps. This weekend I'll see why it doesn't work.

Please note: The code will shortly be available under the MIT license. Therefore, please wait a short time if you want to use the code.
Zuletzt geändert von Sicro am 26.01.2018 15:33, insgesamt 1-mal geändert.
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Rinzwind
Beiträge: 2
Registriert: 07.02.2017 11:39

Re: Modul "FileInfo"

Beitrag von Rinzwind »

Found out that setting tempcli to 040904B0 works in this case (English).

Also tried GetFileVersionInfoSizeEx & GetFileVersionInfoEx with #FILE_VER_GET_NEUTRAL | #FILE_VER_GET_LOCALISED but that still caused no result from VerQueryValue.

But it seems like a problem specific to only a few apps like Calculator (which happened to be the one I choose to test :)). Another app like the Calendar C:\Program Files\WindowsApps\microsoft.windowscommunicationsapps_17.8827.21595.0_x64__8wekyb3d8bbwe\HxCalendarAppImm.exe does return info...

Also read somewhere (.net framework source github) that Explorer itself also tries some fallbacks when it doesn't get a result at first..
"040904B0" ; US English + CP_UNICODE
"040904E4" ; US English + CP_USASCII
"04090000" ; US English + unknown codepage
Benubi
Beiträge: 186
Registriert: 22.10.2004 17:51
Wohnort: Berlin, Wedding

Re: Modul "FileInfo"

Beitrag von Benubi »

OT:

Und wenn Du dazu noch eine GetIcon() oder GetIconImage(OptionalSizeParameter) hinzufügen kannst, wäre das ja mal ziemlich "Zucker-leckerli". Oder passt das nicht wirklich thematisch zum Modul dazu?

Da hätte man alles in einem Modul bzw. aus einer Hand für seine "Datei-Eigenschaften" Procedure / Dialog-Box oder sowas.

Nur so eine Idee...
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: Modul "FileInfo"

Beitrag von Sicro »

@Rinzwind:
Thank you very much for this informations. As soon as I have enough time, I will include the further workarounds in the code and will probably improve the code a little bit.

@Benubi:
Vielen Dank für deinen Vorschlag. :allright:
Das werde ich einbauen. :wink:
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: Modul "FileInfo"

Beitrag von Sicro »

  • Neuer Befehl:

    Code: Alles auswählen

    GetFileBitSystem(File$)
  • Weitere Fallbacks für die TranslateCodes hinzugefügt (Vielen Dank, Rinzwind!)
Die Icon-Befehle konnte ich leider noch nicht einbauen, weil ich mich noch nicht genug in diese Technik einarbeiten konnte.
Super wäre es, wenn Icons größer als 32x32 ausgelesen werden könnten (mein Code unten liest von der Datei "7zFM.exe" nur das 16x16-Icon).
Ich stelle hier einfach mal meinen (fehlerhaften) Code hier rein. Vielleicht kann mir einer der WinAPI-Experten bei der Umsetzung helfen.

Code: Alles auswählen

Procedure GetIcon(FilePath$, IconIndex = -1)
  
  Protected CountOfIcons, BestIcon, Image
  Protected Dim SmallIcons(0), Dim LargeIcons(0)
  Protected IconInfo.ICONINFO, Bitmap.BITMAP
  
  ; Ermittle Anzahl der verfügbaren Icons
  CountOfIcons = ExtractIconEx_(@FilePath$, -1, 0, 0, 0)
  
  If IconIndex = -1
    ProcedureReturn CountOfIcons
  EndIf
  
  ; Prüfe, ob Icons gefunden wurden
  ; - Ist der Rückgabewert kleiner als Null, wurden keine Icons gefunden
  If CountOfIcons < 0
    ProcedureReturn #False
  EndIf
  
  ; Prüfe, ob der geforderte Icon-Index gültig ist
  If IconIndex > CountOfIcons
    ProcedureReturn #False
  EndIf
  
  ; Extrahiere die Icons
  ReDim SmallIcons(CountOfIcons)
  ReDim LargeIcons(CountOfIcons)
  Result = ExtractIconEx_(@FilePath$, 0, @SmallIcons(0), @LargeIcons(0), CountOfIcons)
  
  ; Suche das bestqualitative Icon
  If LargeIcons(IconIndex) <> 0
    BestIcon = LargeIcons(IconIndex)
  Else
    If SmallIcons(IconIndex) <> 0
      BestIcon = SmallIcons(IconIndex)
    EndIf
  EndIf
  
  ; Erstelle aus dem Icon ein PB-Image
  If BestIcon <> 0
    GetIconInfo_(BestIcon, @IconInfo)
    GetObject_(IconInfo\hbmMask, SizeOf(BITMAP), @Bitmap)
    Image = CreateImage(#PB_Any, Bitmap\bmWidth, Bitmap\bmHeight)
    
    If StartDrawing(ImageOutput(Image))
      DrawImage(BestIcon, 0, 0)
      StopDrawing()
    EndIf
  EndIf
  
  ; Gebe Icons wieder frei
  For i = 0 To CountOfIcons
    DestroyIcon_(LargeIcons(i))
    DestroyIcon_(SmallIcons(i))
  Next i
  
  ProcedureReturn Image
  
EndProcedure

Image = GetIcon("C:\Programme\7-Zip\7zFM.exe", 1)
;GetIcons("C:\Dokumente und Einstellungen\Alexander\Desktop\test.pb")
;GetIcons("C:\Programme\PureBasic\PureBasic.exe")
;GetIcons("C:\Dokumente und Einstellungen\Alexander\Desktop\logo.ico")

If OpenWindow(0,0,0,500,250,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  ImageGadget(1,10,10,32,32,0)
  If IsImage(Image)
    SetGadgetState(1, ImageID(Image))
  EndIf
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
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

Re: Modul "FileInfo"

Beitrag von ts-soft »

Ich würde es mal mit der LoadImage_() API probieren: https://docs.microsoft.com/en-us/window ... loadimagea

Gruß
Thomas
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
Antworten