Announcement

Collapse
No announcement yet.

I'm trying to open a .jpg, etc, with the associated program.using shell

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I'm trying to open a .jpg, etc, with the associated program.using shell

    I am trying to shell to an associated image file; meaning, I'm trying to open a .jpg, etc, with the associated program (i.e. Windows Photo Viewer).

    Appreciate the help

    Thanks,

    Manny

  • #2
    Howdy, Manny!

    I typically use ShellExecute ...

    Code:
     ShellExecute(hDlg, "Open", "system\shortcuts.jpg", $Nul, $Nul, %SW_ShowNormal)

    Comment


    • #3
      Thank you for responding..

      Manny
      Last edited by Manny Corrao; 8 Aug 2020, 03:05 PM.

      Comment


      • #4
        Just posted this in another thread

        I use it to find out what the default application is for a specific extension.

        '
        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "win32api.inc"
        
        FUNCTION GetOpener (myfile AS STRING) AS STRING
        ' Get Registration Key for default OPEN application for a filename [and call the application]
        LOCAL retval AS LONG
        LOCAL hkey AS LONG
        LOCAL OpenDialog AS STRINGZ * 256
        LOCAL Extension AS STRINGZ * 256
        LOCAL ReaderCmd AS STRINGZ * 256
        Extension = "." & PARSE$(myfile,".",-1)
        retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
        retval = regqueryvalueex(hkey,"",0,%reg_sz,OpenDialog,256)
        retval = regclosekey(hkey)
        Extension = OpenDialog & "\SHELL\OPEN\command"
        retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
        retval = regqueryvalueex(hkey,"",0,%reg_sz,ReaderCmd,256)
        retval = regclosekey(hkey)
        REPLACE "%1" WITH myfile IN ReaderCmd
        FUNCTION = ReaderCmd
        'uncomment next line to open file
        'retval = SHELL(ReaderCmd)
        END FUNCTION
        
        
        FUNCTION PBMAIN () AS LONG
        LOCAL finame AS STRING
        finame = "test.txt"
        MSGBOX Getopener(finame)
        END FUNCTION
        '

        Comment


        • #5
          Gary

          I think that Manny actually wants to open the .jpg

          Your code could include a file name could it not?

          I use this technique opening word and excel files 'automatically'

          Kerry
          [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
          Kerry Farmer

          Comment


          • #6
            Thanks guys, I appreciate the response.

            I am actually trying to open the .jpg, using the associated program. I want it to have the focus, and be the top=most visible window.

            Thanks all.

            Manny..

            Comment


            • #7
              Originally posted by Manny Corrao View Post
              Thanks guys, I appreciate the response.

              I am actually trying to open the .jpg, using the associated program. I want it to have the focus, and be the top=most visible window.
              I've just updated my earlier code to be WSTRING (Unicode) aware and to do what you want.

              '
              Code:
              #COMPILE EXE
              #DIM ALL
              %UNICODE=1
              #INCLUDE "win32api.inc"
              FUNCTION PBMAIN () AS LONG
              LOCAL finame AS WSTRING
              DISPLAY OPENFILE 0,,,"Select file",EXE.PATH$ , CHR$("All files", 0, "*.*", 0) _ &
                      ,"","",0 TO finame
              IF finame> "" THEN
                  SHELL(Getopener(finame),1)
              END IF
              END FUNCTION
              
              FUNCTION GetOpener (myfile AS WSTRING) AS WSTRING
              ' Get Registration Key for default OPEN application for a filename
              LOCAL retval AS LONG
              LOCAL hkey AS LONG
              LOCAL RegPath AS WSTRINGZ * 256
              LOCAL Extension AS WSTRINGZ * 256
              LOCAL OpenerCmd AS WSTRINGZ * 256
              Extension = PATHNAME$(EXTN,myfile)
              retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
              retval = regqueryvalueex(hkey,"",0,%reg_sz,RegPath,256)
              retval = regclosekey(hkey)
              Extension = RegPath & "\SHELL\OPEN\command"
              retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
              retval = regqueryvalueex(hkey,"",0,%reg_sz,OpenerCmd,256)
              retval = regclosekey(hkey)
              REPLACE "%1" WITH myfile IN OpenerCmd
              FUNCTION = OpenerCmd
              END FUNCTION

              Comment


              • #8
                Manny,
                The code I posted does what you ask. Did you try it and what result did you get? Where did it fall short of what you wanted?

                Comment


                • #9
                  Hello Gary;

                  I must confess: I am an somewhat of an amateur and I tried your code and most likely didn't know how to use it because I couldn't get it to work. I didn't know if I would get any other help so I did a work-around and wrote a one line batch script and shell to it. That works fine though I would prefer to use PB Console code to accomplish the task.

                  Thanks,

                  Manny

                  Comment


                  • #10
                    Gary, It does nothing.

                    Stewart, code in post 7 does nothing even compiled/run in PBWin 10. OP wants PBCC. GateOpener returns empty string when passed an existing jpg name.

                    Cheers,
                    Dale

                    Comment


                    • #11
                      Executable version .. put a file "manny.jpg" in the same folder as this code and run it. This pulls up the default client for "manny.jpg" - works just fine here.

                      Code:
                      'Compilable Example:
                      #Compile Exe
                      #Include "Win32API.inc"
                      
                      Function PBMain() As Long
                         ShellExecute(0, "Open", "manny.jpg", $Nul, $Nul, %SW_ShowNormal)
                      End Function
                      In this case I used "0" instead of hDlg because this example has no dialog.

                      Download it all at http://www.garybeene.com/files/manny.zip

                      added ... I tried it in PBCC and PBWin - works in both.

                      Comment


                      • #12
                        Gary, yes that works. (just substituted file name of jpg I have here). Thanks

                        Manny, with this example the console closes when default viewer opens. Additional code will allow continued use of console.

                        Cheers,
                        Dale

                        Comment


                        • #13
                          Originally posted by Dale Yarker View Post
                          Gary, It does nothing.

                          Stewart, code in post 7 does nothing even compiled/run in PBWin 10. OP wants PBCC. GateOpener returns empty string when passed an existing jpg name.

                          Cheers,
                          That's strange, it works for me.

                          Here's a PBCC4 version that also works for me (That's the latest CC I've got - since then I've stuck with PBWin since it does everything I need).

                          '
                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          #INCLUDE "win32api.inc"
                          DECLARE FUNCTION GetOpener (myfile AS STRING) AS STRING
                          FUNCTION PBMAIN () AS LONG
                          LOCAL finame AS STRING
                          finame = COMMAND$
                          SHELL(Getopener(finame),1)
                          END FUNCTION
                          
                          FUNCTION GetOpener (myfile AS STRING) AS STRING
                          ' Get Registration Key for default OPEN application for a filename
                          LOCAL retval AS LONG
                          LOCAL hkey AS LONG
                          LOCAL RegPath AS ASCIIZ * 256
                          LOCAL Extension AS ASCIIZ * 256
                          LOCAL OpenerCmd AS ASCIIZ * 256
                          Extension = RIGHT$(myfile,4)
                          retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
                          retval = regqueryvalueex(hkey,"",0,%reg_sz,RegPath,256)
                          retval = regclosekey(hkey)
                          Extension = RegPath & "\SHELL\OPEN\command"
                          retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
                          retval = regqueryvalueex(hkey,"",0,%reg_sz,OpenerCmd,256)
                          retval = regclosekey(hkey)
                          REPLACE "%1" WITH myfile IN OpenerCmd
                          FUNCTION = OpenerCmd
                          END FUNCTION
                          
                          '
                          Click image for larger version  Name:	openertest.jpg Views:	0 Size:	21.7 KB ID:	797836

                          Comment


                          • #14
                            '
                            Code:
                            #compile exe
                            #dim all
                            
                            'Compilable Example:
                            'Gary's code adjusted to show codes returned by ShellExecute
                            
                            #include "Win32API.inc"
                            
                            function pbmain() as long
                              local hCon, FuncRet as dword
                              hCon = con.handle
                              FuncRet = ShellExecute(hCon, "Open", "NonExist.jpg", $nul, $nul, %sw_shownormal)
                              ? str$(FuncRet) + " - no such file"
                              'set file name in next line to an existing JPG
                              FuncRet = ShellExecute(hCon, "Open", "TestJPEG.jpg", $nul, $nul, %sw_shownormal)
                              ? str$(FuncRet) + " - per MSDN is greater than 32 for success"
                              waitkey$ 'I get 2 and 42 on console
                            end function'
                            Stewart, You've got picture proof. I'll try it after supper.

                            Manny, You should be all set now, and with choices.

                            Cheers,
                            Dale

                            Comment


                            • #15
                              Hi Gary,

                              That worked! Thank you for code sample. My problem appears to have been the handling of 'hDlg'

                              Thank you all for your help and participation.

                              Manny

                              Comment


                              • #16
                                Hello Dale:

                                Thanks for the code and your participation: I do appreciate your help.

                                Manny:

                                Comment


                                • #17
                                  Hey Guys:

                                  Though I am now using Gary's code, I thought I would share the batch script I created:


                                  shell "StockCharts.bat"

                                  @echo OFF
                                  START Charts\1ViewStocks.jpg


                                  Thanks again,

                                  Manny

                                  Comment


                                  • #18
                                    If you want/need the name of the executable, the FindExecutable() Winapi function is a little more straightforward than reading the registry.
                                    Michael Mattias
                                    Tal Systems Inc. (retired)
                                    Racine WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Originally posted by Michael Mattias View Post
                                      If you want/need the name of the executable, the FindExecutable() Winapi function is a little more straightforward than reading the registry.
                                      FindExecutable requires a valid filename, so you can't just use it to find for example the default ".jpg" opener without having or creating a specific file.

                                      More importantly, it's badly flawed:https://devblogs.microsoft.com/oldne...08-00/?p=93274:

                                      The Find­Executable function comes from 16-bit Windows, and back in those days, there were no context menu shell extensions or custom drop targets. (There was DDE, but that’s okay, because programs still have to register an executable to be used in the fallback case when nobody responds to the DDE message.)

                                      In the port to 32-bit Windows, the Find­Executable function remains, but it works only in the case where files were registered in the 16-bit way; that is, with a command line executable. It so happens that most file types are still registered that way, so the Find­Executable function basically still works.

                                      Since the Find­Executable function is basically a throwback to 16-bit Windows, there is another attempt to accommodate the 16-bit world that is not as obvious: The Find­Executable function takes the thing you pass and converts it into a short file name before trying to look up the handler.

                                      The effect of the conversion to a short file name depends on a bunch of things.

                                      If the volume does not have short file name autogeneration enabled, then the conversion to a short file name has no effect. But if the volume does have short file name autogeneration enabled, then the net effect is that the extension gets truncated to three characters. foo.abcde becomes foo~1.abc. And then Find­Executable looks up and returns the handler for the .abc extension instead of the .abcde extension.

                                      Comment


                                      • #20
                                        Originally posted by Michael Mattias View Post
                                        If you want/need the name of the executable, the FindExecutable() Winapi function is a little more straightforward than reading the registry.
                                        Try FindExecutable with an .accdb file.

                                        Even if it does find an executable, it won't tell you about any default switches applicable to the Shell Open. (i.e. a default /NOSTARTUP switch before the file name for an Access .mdb, /dde for excel, /n for Word)

                                        Comment

                                        Working...
                                        X