How to delete a file with ShellExecute in Visual C++ 2005

Posted on 2007-10-03
Medium Priority
Last Modified: 2013-11-26
Hi there,

I've got troubles using ShellExecute in Visual C++ 2005. I've written a simple exe foo.exe intended to delete the file "bar" which is in the same directory as the exe. When I run foo from the Dos prompt,
I get error 2 (file not found ? cf http://www.nsftools.com/tips/ShellExec.lss)

I've written

      HINSTANCE hRetour;

      LPWSTR sVerb, sCommand, sParameters, sDirectory;
      ConvertMultiByteToWideChar("open", &sVerb);
      ConvertMultiByteToWideChar("del", &sCommand);
      ConvertMultiByteToWideChar(""bar"", &sParameters);
      ConvertMultiByteToWideChar("", &sDirectory);

      hRetour = ShellExecute(0,
      int iRetour = GetLastError();
      printf("GetLastError = %i   ", iRetour);

where the function ConvertMultiByteToWideChar is :

void ConvertMultiByteToWideChar(char * sInput, LPWSTR * pwOutput)
      int Size = MultiByteToWideChar (CP_ACP, 0, sInput, -1, NULL, 0);
      *pwOutput = new WCHAR[Size];
      MultiByteToWideChar (CP_ACP, 0, sInput, -1, *pwOutput, Size);

Thanks in advance
Question by:B_B_Boris
1 Comment
LVL 45

Accepted Solution

AndyAinscow earned 2000 total points
ID: 20012112
ShellExectute will work with UNICODE strings - so you don't need to convert.

I'd also suggest you look very carefully at the parameters you have set for the ShellExecute.  Is that correct?

ShellExecute(0, "open", "del", "bar", "", SW_HIDE);  

(If you want to delete a file then why not use the SHFileOperation command ?)

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question