-3

I m trying to open a file using shellexecute command my code is

ShellExecute(NULL,NULL,itemPath,NULL,NULL,SW_SHOW); 

or

ShellExecute(NULL,L"open",itemPath,NULL,NULL,SW_SHOW); 

I have used both ways but when I m giving path of a folder it opens a folder but when I m giving a full specified path of a file it doesn't work. one thing more if I m giving a hardcode path like for example

ShellExecute(NULL,L"open",L"E:\\abc.xlsx",NULL,NULL,SW_SHOW);

than it opens this file. can any one explain why it is happening.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Kashif Meo
  • 65
  • 1
  • 8
  • You need to escape backslashes in C++, e.g. `E:\\abc.xlsx`. – Jonathan Potter Jun 27 '16 at 06:29
  • I have used that. ShellExecute(NULL,L"open",L"E:\\abc.xlsx",NULL,NULL,SW_SHOW); this command runs perfectly . problem occurs when I m passing path using variable.. – Kashif Meo Jun 27 '16 at 06:49
  • What type is *itemPath*? – IInspectable Jun 27 '16 at 06:51
  • itempath is of CString type – Kashif Meo Jun 27 '16 at 07:52
  • 1
    If the call to `ShellExecute` appears to work when passing a string literal with the exact same content as a `CString` variable, then more likely than not your variable doesn't hold the value you think it does. At any rate, this isn't nearly enough information to help answer the question. You need to provide a [mcve]. You should also consider calling [ShellExecuteEx](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154.aspx) instead for better error reporting. – IInspectable Jun 27 '16 at 09:06

1 Answers1

2

ShellExecute works correctly, and the defect can be found in your code. The only explanation that makes sense is that itemPath is not what you think it is. If it were indeed a pointer to null-terminated character array containing L"E:\\abc.xlsx" then ShellExecute would behave as you expect.

You can debug the problem by inspecting the content of itemPath to find out what it really contains. Were you to have provided an MCVE then we could have been more specific in the diagnosis of the problem.

Finally, ShellExecute is deprecated, largely because it provides no good means of reporting failure conditions. You should use ShellExecuteEx instead.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I have debugged it. and itempath contains "E:\abc.xlsx" and itempath is variable of type CString – Kashif Meo Jun 27 '16 at 07:51
  • If `itemPath == L"E:\\abc.xlsx"` then what you report could not be true. Or do you really believe that `ShellExecute` does not work? – David Heffernan Jun 27 '16 at 08:12
  • actually there is bundle of line of code so I can't show the whole code I m getting this path from a file which is encrypted but finally after encryption the final value itempath contains is E:\abc.xlsx without double backslashes . same code is working perfectly in win7. but in win 10 it not behave like I m expecting.. – Kashif Meo Jun 27 '16 at 08:41
  • I stand by my answer. – David Heffernan Jun 27 '16 at 09:18
  • 1
    @KashifMeo: The culprit *has* to be bad content in the `itemPath` variable, it does not contain what you are expecting it to contain. There is no other explanation that fits the symptoms you have described. My guess is the decryption is probably causing extra garbage in the variable that you are not accounting for. – Remy Lebeau Jun 27 '16 at 18:56
  • Sorry one thing which I have forgot to tell you that I my file is locked using filter driver that file will not be shown in explorer.. that locked files are opening in win 7 but not working in win 10. that is what I m asking actually.. – Kashif Meo Jun 28 '16 at 04:59
  • That's a different question completely – David Heffernan Jun 28 '16 at 05:27