How to self delete

Hi EE.

Does anyone know how can I make my app to self delete? In VB.NET I use this:
Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + 
          Application.ExecutablePath); 
Application.Exit();

Open in new window

How can I also archive that in C++, I'm using Visual C++ 2010 Windows forms.
LVL 2
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
There is no reliable way to do this..

what is your use-case here?
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
I'm trying to make an app that will run to check if the content are protected or not, if the system is not secured by our system, then this app should remove it self, this is to prevent the system to run on an un-protected PC. This app should only run on PC which are protected by our system.
0
ste5anSenior DeveloperCommented:
Protected content and secured systems are two different things. You can store protected content on unsecure systems. Otherwise for example cloud services would not work.

Just run that test in the installer and deny installation when you requirements are not met.
Otherwise only test the state and give the user an appropriate message.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

AndyAinscowFreelance programmer / ConsultantCommented:
To show you the C++.net code:
      using namespace System::Diagnostics;
...
      private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                         Process::Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + Application::ExecutablePath);
Application::Exit();

                   }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
@Andy. It just flush and exit but the file is not deleted. Any idea why is that?
0
AndyAinscowFreelance programmer / ConsultantCommented:
It does delete it on my PC.  Is the exe in a location where you do not have delete rights ?

ps. The code is identical to yours - just converted to C++.net syntax.
0
ste5anSenior DeveloperCommented:
Application.Exit does not quit an application. It only leaves the message pump and informs all other threads to do so. That process can last some time. Maybe longer than your ping waiting time.

Also check that you use only one ampersand.
0
David Johnson, CD, MVPOwnerCommented:
a process can't delete itself since it is running. you need another task to clean things up.
1
ste5anSenior DeveloperCommented:
FYI: http://www.catch22.net/tuts/self-deleting-executables

Also in .NET binaries are locked. Then there is the permissions problem.. I would simply change the strategy.
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Well this is a bit confusing then. In VB.NET as well as on Java this works and @Andy said its working on his side. @Andy which OS you are using? I'm using Win7 32bit (Intel Pentium)
0
AndyAinscowFreelance programmer / ConsultantCommented:
Win 10 but the app is just a form with a button on it.

Try changing the duration you use in the cmd you start and see if that helps.
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Ok will try that and I will also try it on another OS in a VM as well to see, but I don't think my OS has a problem because I had used this in Java as well as in VB.NET and there we no problems.
0
ste5anSenior DeveloperCommented:
It can work, but there is no guarantee that the Application.Exit() call and all threads are finished after your 3 seconds ping timeout.

Much worse: what do you do, if there is an answer?
And also: not every user can run ping on his machine, it could be blocked by GPO.

[..]  I had used this in Java as well as in VB.NET and there we no problems.
Just again: There is no GUARANTEE that this works. You just were lucky so far. And Java is a different language and eco-system. It's comparing apples and oranges..
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
So what could be the best way to do this? I think it will be a bad idea to use the approach of delete on Computer shutdown, because that will bug the user.
0
ste5anSenior DeveloperCommented:
Well, I would be really annoyed not being to able to start my program the next time at all...

As I already wrote: Just make this test part of your installation routine. When it fails, then don't install it at all. But when it's installed, the simply output a message that it's not safe to run and quit the application.
0
sarabandeCommented:
ping 1.1.1.1 -n 1 -w 3000 > Nul & Del ...
i tried this statement in a command window of a win7 system and replaced the del command by an echo command. whatever ip address or url i typed, the echo command always was executed even if the ping returned immediately with an error like 'can't find host xxxxxxxx'. that means that even when the errorlevel was not 0 after ping the cmd would try to execute the 2nd cmd, what means in your case that the deletion is only successful when the ping command needs some time to proceed and the file to delete is no longer in use, i. e. had enough time to exit. so it is as told by ste5an that your way is not reliable even if it had worked with vb.net or java. both are interpreters and don't have a need to keep the execution file open when the next staement is an exit statement. with c++.net you don't have a pure .net executable as a c++ executable always has a native c++ part which was handled by windows os and not by .net.

David already told that you have to use two commands to do what you want. the first command could be like

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
              {
                       Process::Start("cmd.exe", "/C echo @echo off >err.cmd & "
                                                 "ping 1.1.1.1 -n 1 -w 3000 > Nul & "
                                                 "if errorlevel 1 echo  del " +  Application::ExecutablePath + " >>err.cmd");
                       Application::Exit();
              }

Open in new window


that would empty a previous err.cmd file - if any - and then add the delete statement   if the ping returned with error.

hence you can start a second application which simply calls the err.cmd.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
              {
                       Process::Start("cmd.exe", "/C err.cmd);
                       Application::Exit();

              }

Open in new window


alternatively you could call err.cmd directly from cmd (window or batch) after you made sure that the first application was finished and no longer was blocked by the windows os. generally i would say that a native 32-bit c++ program more likely is to not being opened after exit as it is the case with a .net component (especially in debug mode). and of course if you would be able to move the check into a setup utility as suggested by ste5an, it would simplify things. same is if you make the check always at begin of your application and terminate if the ping returned with error but do not delete the executable. of course, the best way would be if you could provide a method to check for secured addresses without using ping at all.

Sara

Sara
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Thanks to everyone. I think the problem is with the path, it seems as if the path is not found or is incorrect. I've use the
SendKeys::Send();

Open in new window

and when I also send the enter the CMD says the specified path can not be found. Even if I do it manually still the same error occurs.
0
ste5anSenior DeveloperCommented:
Yup, UAC and the file system virtualization can be a problem...
0
AndyAinscowFreelance programmer / ConsultantCommented:
Odd that it works in VB.Net in that case.  (My snippet was your vb.net converted to C++.net with no other alterations.  VB.Net and C++.net will compile to the same intermediate code which is what is executed.)
0
sarabandeCommented:
vb.net is an interpreter. it no longer has the executable file locked after it read it once.

when I also send the enter the CMD says the specified path can not be found

can you post both the code and the command (including the file path). actually the error 'path not found' is a technical error which easily could be corrected by either changing from relative to absolute path or by choosing the right default directory. if there is a folder containing spaces the error is due to missing quotes around the path.

Sara
0
sarabandeCommented:
Andy, as far as I understand from this thread, the proposed solution cannot reliably solve the requirement to delete an application after execution. The Author explicitly told that your code did not work.

I don't want to object as I have little expertise on visual basic and c# (that is also the reason why I didn't close the question myself) but perhaps you could explain why you are still convinced that #a42276877 is the solution.

Sara
0
AndyAinscowFreelance programmer / ConsultantCommented:
@Sara.

Looking at the question a piece of VB.NET code was given - the asker stated he used that for self deleting an app - and asked for the equivalent in C++.net.
>>>In VB.NET I use this: ......How can I also archive that in C++, I'm using Visual C++ 2010 Windows forms.
Comment #a42276877 provides that translation asked for.  The code does work, at least in a test app.

OK, the asker then afterwards found that for some reason it wasn't working in the specific case he then wanted to reuse the code for.  The remainder of the thread was discussing why it might not work in that specific case and other, possible, alternatives for self deleting hence I split between all contributors to this thread.

If you aren't happy feel free to object and I'll abide by what the moderators decide.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Translation of the (functional according to the asker) vb.net code to C++.net as requested and other ideas/advice to 'self delete' an app.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.