SHOpenWithDialog Function mit PB

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.
Marty2PB
Beiträge: 16
Registriert: 14.03.2014 00:00

SHOpenWithDialog Function mit PB

Beitrag von Marty2PB »

Moin,
Da ich nicht das Standard Context Menu von Windows nutze aber nur den 'Öffne mit' Dialog benötige und SHOpenWithDialog_() so nicht unter PB aufrufen lässt
habe ich das mal so gemacht :)
Hoffe das ist richtig 'Konvertiert' habe. Bin noch nicht so bewandert mi PB :| aber Funktioniert :mrgreen:

Source: https://msdn.microsoft.com/de-de/librar ... 85%29.aspx
Source: https://msdn.microsoft.com/de-de/librar ... 85%29.aspx

Code: Alles auswählen

    #OAIF_ALLOW_REGISTRATION = $00000001 ; Enable the "always use this program" checkbox. If Not passed, it will be disabled.
    #OAIF_REGISTER_EXT       = $00000002 ; Do the registration after the user hits the OK button.
    #OAIF_EXEC               = $00000004 ; Execute file after registering.
    #OAIF_FORCE_REGISTRATION = $00000008 ; Force the Always use this program checkbox To be checked. Typically, you won't use the OAIF_ALLOW_REGISTRATION flag when you pass this value. 
    #OAIF_HIDE_REGISTRATION  = $00000020 ; Introduced IN Windows Vista. Hide the Always use this program checkbox. If this flag is specified, the OAIF_ALLOW_REGISTRATION And OAIF_FORCE_REGISTRATION flags will be ignored. 
    #OAIF_URL_PROTOCOL       = $00000040 ; Introduced IN Windows Vista. The value For the extension that is passed is actually a protocol, so the Open With dialog box should show applications that are registered As capable of handling that protocol.
    #OAIF_FILE_IS_URI        = $00000080 ; Introduced IN Windows 8. The location pointed To by the pcszFile parameter is given As a URI.
    
Procedure SH_OpenWithDialog_(File$,OPEN_AS_INFO_FLAGS.l)
    
    Protected LibraryID
    
    Structure OPENASINFO
        pcszFile.i
        pcszClass.i
        oaifInFlags.l
    EndStructure  
  
    LibraryID = OpenLibrary(#PB_Any, "shell32.dll")
    If LibraryID
        
        Define OWD.OPENASINFO
        
        OWD\pcszFile = @File$
        OWD\pcszClass = #Null
        OWD\oaifInFlags = OPEN_AS_INFO_FLAGS
        
        CallFunction(LibraryID, "SHOpenWithDialog", hwnd,@OWD.OPENASINFO)
        CloseLibrary(LibraryID)
    EndIf
EndProcedure

File$ = #PB_Compiler_Home + "SDK\VisualC\Readme.txt"
SH_OpenWithDialog_(File$,#OAIF_ALLOW_REGISTRATION|#OAIF_EXEC)
__________________________________________________
MSDN-Links angepasst
29.05.2015
RSBasic
Zuletzt geändert von Marty2PB am 29.05.2015 03:29, insgesamt 2-mal geändert.
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8675
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 32 GB DDR4-3200
Ubuntu 22.04.3 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken
Kontaktdaten:

Re: SHOpenWithDialog Function mit PB

Beitrag von NicTheQuick »

Besser du öffnest die Library nur einmal am Anfang, anstatt sie jedes mal neu zu öffnen.
Bild
Marty2PB
Beiträge: 16
Registriert: 14.03.2014 00:00

Re: SHOpenWithDialog Function mit PB

Beitrag von Marty2PB »

NicTheQuick hat geschrieben:Besser du öffnest die Library nur einmal am Anfang, anstatt sie jedes mal neu zu öffnen.
So ?

Code: Alles auswählen

    #OAIF_ALLOW_REGISTRATION = $00000001 ; Enable the "always use this program" checkbox. If Not passed, it will be disabled.
    #OAIF_REGISTER_EXT       = $00000002 ; Do the registration after the user hits the OK button.
    #OAIF_EXEC               = $00000004 ; Execute file after registering.
    #OAIF_FORCE_REGISTRATION = $00000008 ; Force the Always use this program checkbox To be checked. Typically, you won't use the OAIF_ALLOW_REGISTRATION flag when you pass this value. 
    #OAIF_HIDE_REGISTRATION  = $00000020 ; Introduced IN Windows Vista. Hide the Always use this program checkbox. If this flag is specified, the OAIF_ALLOW_REGISTRATION And OAIF_FORCE_REGISTRATION flags will be ignored. 
    #OAIF_URL_PROTOCOL       = $00000040 ; Introduced IN Windows Vista. The value For the extension that is passed is actually a protocol, so the Open With dialog box should show applications that are registered As capable of handling that protocol.
    #OAIF_FILE_IS_URI        = $00000080 ; Introduced IN Windows 8. The location pointed To by the pcszFile parameter is given As a URI.
    
    Structure OPENASINFO
        pcszFile.i
        pcszClass.i
        oaifInFlags.l
    EndStructure 
           
    Prototype SH_OpenWithDialog_(HWND,*poainfo)
    If OpenLibrary(0, "SHELL32.DLL")
        Global SH_OpenWithDialog_.SH_OpenWithDialog_ = GetFunction(0, "SHOpenWithDialog")
        CloseLibrary(0)
    EndIf    
        
 Procedure SH_OpenWithDialog(File$,OPEN_AS_INFO_FLAGS.l)
    
    Define OWD.OPENASINFO
    
    OWD\pcszFile = @File$
    OWD\pcszClass = #Null
    OWD\oaifInFlags = OPEN_AS_INFO_FLAGS
      
    Result = SH_OpenWithDialog_(hwnd,@OWD.OPENASINFO)
    ProcedureReturn Result 
 EndProcedure    
 
File$ = #PB_Compiler_Home + "SDK\VisualC\Readme.txt"
Debug SH_OpenWithDialog(File$,#OAIF_ALLOW_REGISTRATION|#OAIF_EXEC)
Benutzeravatar
edel
Beiträge: 3667
Registriert: 28.07.2005 12:39
Computerausstattung: GameBoy
Kontaktdaten:

Re: SHOpenWithDialog Function mit PB

Beitrag von edel »

Du darfst sie nicht schliessen. Solange du auf die Funktionen der DLL zugreifen moechtest, muss die DLL auch geladen bleiben. Es spricht auch nichts dagegen, sie bis zum Ende geladen zu haben. PB gibt sie dann automatisch frei.
Benutzeravatar
_JON_
Beiträge: 389
Registriert: 30.03.2010 15:24

Re: SHOpenWithDialog Function mit PB

Beitrag von _JON_ »

Pass aber auch immer auf ob die Funktion überhaupt verfügbar ist.

Code: Alles auswählen

EnableExplicit

#OAIF_ALLOW_REGISTRATION = $00000001 ; Enable the "always use this program" checkbox. If Not passed, it will be disabled.
#OAIF_REGISTER_EXT       = $00000002 ; Do the registration after the user hits the OK button.
#OAIF_EXEC               = $00000004 ; Execute file after registering.
#OAIF_FORCE_REGISTRATION = $00000008 ; Force the Always use this program checkbox To be checked. Typically, you won't use the OAIF_ALLOW_REGISTRATION flag when you pass this value. 
#OAIF_HIDE_REGISTRATION  = $00000020 ; Introduced IN Windows Vista. Hide the Always use this program checkbox. If this flag is specified, the OAIF_ALLOW_REGISTRATION And OAIF_FORCE_REGISTRATION flags will be ignored. 
#OAIF_URL_PROTOCOL       = $00000040 ; Introduced IN Windows Vista. The value For the extension that is passed is actually a protocol, so the Open With dialog box should show applications that are registered As capable of handling that protocol.
#OAIF_FILE_IS_URI        = $00000080 ; Introduced IN Windows 8. The location pointed To by the pcszFile parameter is given As a URI.

Structure OPENASINFO
  pcszFile.i
  pcszClass.i
  oaifInFlags.l
EndStructure 

Prototype pSH_OpenWithDialog(HWND, *poainfo)

Procedure.l OpenWithDialog(File$, OPEN_AS_INFO_FLAGS.l = 0, hwnd.i = 0)
  
  Protected OWD.OPENASINFO, hShell32, SH_OpenWithDialog_.pSH_OpenWithDialog, Result
  
  hShell32 = OpenLibrary(#PB_Any, "SHELL32")
  If hShell32
    SH_OpenWithDialog_ = GetFunction(hShell32, "SHOpenWithDialog")
    If SH_OpenWithDialog_
      OWD\pcszFile = @File$
      OWD\pcszClass = #Null
      OWD\oaifInFlags = OPEN_AS_INFO_FLAGS
      Result = SH_OpenWithDialog_(hwnd, @OWD.OPENASINFO)
    Else
      Result = OpenAs_RunDLL_(hwnd, 0, File$, 0)
    EndIf
    CloseLibrary(hShell32)
  EndIf   
  
  ProcedureReturn Result 
EndProcedure    

Define File$ = #PB_Compiler_Home + "SDK\VisualC\Readme.txt"
Debug OpenWithDialog(File$,#OAIF_ALLOW_REGISTRATION|#OAIF_EXEC)
PureBasic 5.46 LTS (Windows x86/x64) | windows 10 x64 Oktober failure
Marty2PB
Beiträge: 16
Registriert: 14.03.2014 00:00

Re: SHOpenWithDialog Function mit PB

Beitrag von Marty2PB »

Ah Ok, ich hatte das so verstanden das 'OpenLibrary' nicht in eine Procdure sollte. Danke für das korrigieren @all :)
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8675
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 32 GB DDR4-3200
Ubuntu 22.04.3 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken
Kontaktdaten:

Re: SHOpenWithDialog Function mit PB

Beitrag von NicTheQuick »

Damit es kein Missverständnis gibt:
Natürlich darf 'OpenLibrary()' in eine Procedure, aber ein ständiges Öffnen und Schließen der Library kostet Zeit und Resourcen, weswegen es schlauer ist, sie nur einmalig zu öffnen.
Mit einem "Static" kann man auch erreichen, dass sie nur einmal geöffnet wird.

Code: Alles auswählen

EnableExplicit

#OAIF_ALLOW_REGISTRATION = $00000001 ; Enable the "always use this program" checkbox. If Not passed, it will be disabled.
#OAIF_REGISTER_EXT       = $00000002 ; Do the registration after the user hits the OK button.
#OAIF_EXEC               = $00000004 ; Execute file after registering.
#OAIF_FORCE_REGISTRATION = $00000008 ; Force the Always use this program checkbox To be checked. Typically, you won't use the OAIF_ALLOW_REGISTRATION flag when you pass this value.
#OAIF_HIDE_REGISTRATION  = $00000020 ; Introduced IN Windows Vista. Hide the Always use this program checkbox. If this flag is specified, the OAIF_ALLOW_REGISTRATION And OAIF_FORCE_REGISTRATION flags will be ignored.
#OAIF_URL_PROTOCOL       = $00000040 ; Introduced IN Windows Vista. The value For the extension that is passed is actually a protocol, so the Open With dialog box should show applications that are registered As capable of handling that protocol.
#OAIF_FILE_IS_URI        = $00000080 ; Introduced IN Windows 8. The location pointed To by the pcszFile parameter is given As a URI.

Structure OPENASINFO
	pcszFile.i
	pcszClass.i
	oaifInFlags.l
EndStructure

Prototype pSH_OpenWithDialog(HWND, *poainfo)

Procedure.l OpenWithDialog(File$, OPEN_AS_INFO_FLAGS.l = 0, hwnd.i = 0)
	Static hShell32 = #False
	Protected OWD.OPENASINFO, SH_OpenWithDialog_.pSH_OpenWithDialog, Result = #False
	
	If (Not hShell32)
		hShell32 = OpenLibrary(#PB_Any, "SHELL32")
	EndIf
	
	If hShell32
		SH_OpenWithDialog_ = GetFunction(hShell32, "SHOpenWithDialog")
		If SH_OpenWithDialog_
			OWD\pcszFile = @File$
			OWD\pcszClass = #Null
			OWD\oaifInFlags = OPEN_AS_INFO_FLAGS
			Result = SH_OpenWithDialog_(hwnd, @OWD.OPENASINFO)
		Else
			Result = OpenAs_RunDLL_(hwnd, 0, File$, 0)
		EndIf
	EndIf   
	
	ProcedureReturn Result
EndProcedure   

Define File$ = #PB_Compiler_Home + "SDK\VisualC\Readme.txt"
Debug OpenWithDialog(File$,#OAIF_ALLOW_REGISTRATION|#OAIF_EXEC) 
Bild
Antworten