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

Posted on 2007-10-03
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

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 43

    Accepted Solution

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now