ShellExecute open
-
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 callingShellExecute
. Is that call in your code?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 ...
-
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 ...
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.
-
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.
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 ...
-
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 ...
-
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 ...
-
Sorry, I am out of ideas. But that message suggests that there is no default application registered for the file type.
-
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.
_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.
-
_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.
-
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.
-
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.
_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.