Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ShellExecute open

ShellExecute open

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionlearning
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Go to Windows Explorer, right click on one of the files with that type, and check its properties.

    _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #3

    I check it, there is no any problem, it is open properly in windows explorer … not in my app, using ShellExecute …

    L 2 Replies Last reply
    0
    • _ _Flaviu

      I am trying to open any kind of file using

      ShellExecute(NULL, _T("open"), sPath + _T("\\") + sText, NULL, NULL, SW_SHOWNORMAL);

      and is working well, but there a little issue: when I am trying to open an image file, I get always the "How do you want to open this file" dialog, everytime … why ? Of course, when I am trying to open this kind of image file from y windows explorer, is open with my default program, Windows Photo Viewer. Is there any kind of parameters to put on ShellExecute ? Please note that I use this for ever kind of file, including exe files.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      _Flaviu wrote:

      Is there any kind of parameters to put on ShellExecute ?

      Try NULL instead of "open";

      MSDN[^] wrote:

      The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        I check it, there is no any problem, it is open properly in windows explorer … not in my app, using ShellExecute …

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Have you checked that the details in the properties for open refer to the absolute path to your application? What is the return value from ShellExecute when you run the code? What are the values of sPath and sText?

        _ 1 Reply Last reply
        0
        • L Lost User

          _Flaviu wrote:

          Is there any kind of parameters to put on ShellExecute ?

          Try NULL instead of "open";

          MSDN[^] wrote:

          The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #6

          I have tried in this way, the same behavior, but I noticed that this issue is present on some PC, but not on the other … strange ...

          1 Reply Last reply
          0
          • L Lost User

            Have you checked that the details in the properties for open refer to the absolute path to your application? What is the return value from ShellExecute when you run the code? What are the values of sPath and sText?

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #7

            The returned value is

            0x0000002a

            HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + _T("\\") + sText, NULL, NULL, SW_SHOWNORMAL);

            this values is below 32, which is normal … am I right ? sTest is file name, and it is correct, as I said, on some PC is working without any issue ...

            1 Reply Last reply
            0
            • _ _Flaviu

              I check it, there is no any problem, it is open properly in windows explorer … not in my app, using ShellExecute …

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              I noticed in the Remarks section of ShellExecute function (shellapi.h) | Microsoft Docs[^] that is is strongly recommended that you call CoInitializeEx to initialise the COM system, before calling ShellExecute. Is that call in your code?

              _ 1 Reply Last reply
              0
              • L Lost User

                I noticed in the Remarks section of ShellExecute function (shellapi.h) | Microsoft Docs[^] that is is strongly recommended that you call CoInitializeEx to initialise the COM system, before calling ShellExecute. Is that call in your code?

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #9

                I have tried:

                		hr = CoInitializeEx(NULL, COINIT\_APARTMENTTHREADED);
                			if (SUCCEEDED(hr))
                			{
                				HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + \_T("\\\\") + sText, NULL, NULL, SW\_SHOWNORMAL);
                				if (reinterpret\_cast(hInstance) <= 32)// Could not start application
                					::SendMessage(theApp.m\_pMainWnd->GetSafeHwnd(), WM\_SETMESSAGESTRING, 0, (LPARAM)(LPCTSTR)GetDocument()->FormatHinstance(hInstance));
                			}
                

                the same result ...

                L 1 Reply Last reply
                0
                • _ _Flaviu

                  I have tried:

                  		hr = CoInitializeEx(NULL, COINIT\_APARTMENTTHREADED);
                  			if (SUCCEEDED(hr))
                  			{
                  				HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + \_T("\\\\") + sText, NULL, NULL, SW\_SHOWNORMAL);
                  				if (reinterpret\_cast(hInstance) <= 32)// Could not start application
                  					::SendMessage(theApp.m\_pMainWnd->GetSafeHwnd(), WM\_SETMESSAGESTRING, 0, (LPARAM)(LPCTSTR)GetDocument()->FormatHinstance(hInstance));
                  			}
                  

                  the same result ...

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  The documentation states that you should use:

                  CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

                  But I do not now if that will make a difference. The only way forward is to use the debugger and check exactly what happens at each point. You could also try setting the operation to "open", rather than NULL.

                  _ 1 Reply Last reply
                  0
                  • L Lost User

                    The documentation states that you should use:

                    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

                    But I do not now if that will make a difference. The only way forward is to use the debugger and check exactly what happens at each point. You could also try setting the operation to "open", rather than NULL.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #11

                    I have tried this:

                    			HRESULT hr = E\_FAIL;
                    			hr = CoInitializeEx(NULL, COINIT\_APARTMENTTHREADED | COINIT\_DISABLE\_OLE1DDE);
                    			if (SUCCEEDED(hr))
                    			{
                    				HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + \_T("\\\\") + sText, NULL, NULL, SW\_SHOWNORMAL);
                    				if (reinterpret\_cast(hInstance) <= 32)// Could not start application
                    					::SendMessage(theApp.m\_pMainWnd->GetSafeHwnd(), WM\_SETMESSAGESTRING, 0, (LPARAM)(LPCTSTR)GetDocument()->FormatHinstance(hInstance));
                    			}
                    			CoUninitialize();
                    

                    same behavior … that "How do you want to open this file" is still present when I try to start an image file ...

                    L 2 Replies Last reply
                    0
                    • _ _Flaviu

                      I have tried this:

                      			HRESULT hr = E\_FAIL;
                      			hr = CoInitializeEx(NULL, COINIT\_APARTMENTTHREADED | COINIT\_DISABLE\_OLE1DDE);
                      			if (SUCCEEDED(hr))
                      			{
                      				HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + \_T("\\\\") + sText, NULL, NULL, SW\_SHOWNORMAL);
                      				if (reinterpret\_cast(hInstance) <= 32)// Could not start application
                      					::SendMessage(theApp.m\_pMainWnd->GetSafeHwnd(), WM\_SETMESSAGESTRING, 0, (LPARAM)(LPCTSTR)GetDocument()->FormatHinstance(hInstance));
                      			}
                      			CoUninitialize();
                      

                      same behavior … that "How do you want to open this file" is still present when I try to start an image file ...

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Sorry, I am out of ideas. But that message suggests that there is no default application registered for the file type.

                      _ 1 Reply Last reply
                      0
                      • _ _Flaviu

                        I have tried this:

                        			HRESULT hr = E\_FAIL;
                        			hr = CoInitializeEx(NULL, COINIT\_APARTMENTTHREADED | COINIT\_DISABLE\_OLE1DDE);
                        			if (SUCCEEDED(hr))
                        			{
                        				HINSTANCE hInstance = ShellExecute(AfxGetMainWnd()->GetSafeHwnd(), NULL, sPath + \_T("\\\\") + sText, NULL, NULL, SW\_SHOWNORMAL);
                        				if (reinterpret\_cast(hInstance) <= 32)// Could not start application
                        					::SendMessage(theApp.m\_pMainWnd->GetSafeHwnd(), WM\_SETMESSAGESTRING, 0, (LPARAM)(LPCTSTR)GetDocument()->FormatHinstance(hInstance));
                        			}
                        			CoUninitialize();
                        

                        same behavior … that "How do you want to open this file" is still present when I try to start an image file ...

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        How to Fix: Windows 10 Keeps Asking: 'How do you want to open this file?' | www.infopackets.com[^]

                        1 Reply Last reply
                        0
                        • L Lost User

                          Sorry, I am out of ideas. But that message suggests that there is no default application registered for the file type.

                          _ Offline
                          _ Offline
                          _Flaviu
                          wrote on last edited by
                          #14

                          I really appreciate your post ! Kindly thank you !

                          1 Reply Last reply
                          0
                          • _ _Flaviu

                            I am trying to open any kind of file using

                            ShellExecute(NULL, _T("open"), sPath + _T("\\") + sText, NULL, NULL, SW_SHOWNORMAL);

                            and is working well, but there a little issue: when I am trying to open an image file, I get always the "How do you want to open this file" dialog, everytime … why ? Of course, when I am trying to open this kind of image file from y windows explorer, is open with my default program, Windows Photo Viewer. Is there any kind of parameters to put on ShellExecute ? Please note that I use this for ever kind of file, including exe files.

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #15

                            _Flaviu wrote:

                            I am trying to open an image file

                            Just to clarify that statement.... The method you uses does the following. 1. Determine the type of file using the extension. 2. Look up what application the OS (windows) has recorded to open that type of file. 3. Open the file using the application. However if 1 fails, in that the file type is not known (already specified) then it will ask you for what to open it with. If you want to explicitly specify some application to open the file with then you need a different method. If you want to preclude the message in the first place then you will need to write code that first detects if the type is known. However that can still be problematic because if there is a known application for type, and then the application is uninstalled (or otherwise not available) there is no assurance that the type will have been unregistered. So it might still fail to open it.

                            _ 1 Reply Last reply
                            0
                            • J jschell

                              _Flaviu wrote:

                              I am trying to open an image file

                              Just to clarify that statement.... The method you uses does the following. 1. Determine the type of file using the extension. 2. Look up what application the OS (windows) has recorded to open that type of file. 3. Open the file using the application. However if 1 fails, in that the file type is not known (already specified) then it will ask you for what to open it with. If you want to explicitly specify some application to open the file with then you need a different method. If you want to preclude the message in the first place then you will need to write code that first detects if the type is known. However that can still be problematic because if there is a known application for type, and then the application is uninstalled (or otherwise not available) there is no assurance that the type will have been unregistered. So it might still fail to open it.

                              _ Offline
                              _ Offline
                              _Flaviu
                              wrote on last edited by
                              #16

                              The strange thing is that this behaviour is present on some PC, and on some PC are not present... and I noticed this behaviour even on Outlook application, on some image attachments ... so, guess is PC issue, not application issue.

                              L J 2 Replies Last reply
                              0
                              • _ _Flaviu

                                The strange thing is that this behaviour is present on some PC, and on some PC are not present... and I noticed this behaviour even on Outlook application, on some image attachments ... so, guess is PC issue, not application issue.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #17

                                _Flaviu wrote:

                                guess is PC issue, not application issue.

                                Exactly as I suggested last week.

                                1 Reply Last reply
                                0
                                • _ _Flaviu

                                  The strange thing is that this behaviour is present on some PC, and on some PC are not present... and I noticed this behaviour even on Outlook application, on some image attachments ... so, guess is PC issue, not application issue.

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #18

                                  _Flaviu wrote:

                                  The strange thing is that this behaviour is present on some PC, and on some PC are not present

                                  Because, as I explained, there is an association on the PC, each PC, which specifies what if anything to open the file with based on the extension.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups