Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to self delete

Posted on 2017-08-31
23
Low Priority
?
85 Views
Last Modified: 2017-09-23
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.
0
Comment
Question by:Mlungisi Ndlela
  • 7
  • 6
  • 6
  • +2
23 Comments
 
LVL 36

Expert Comment

by:ste5an
ID: 42276824
There is no reliable way to do this..

what is your use-case here?
0
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42276837
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
 
LVL 36

Assisted Solution

by:ste5an
ste5an earned 166 total points (awarded by participants)
ID: 42276845
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 45

Accepted Solution

by:
AndyAinscow earned 502 total points (awarded by participants)
ID: 42276877
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
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42276918
@Andy. It just flush and exit but the file is not deleted. Any idea why is that?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 42276934
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
 
LVL 36

Expert Comment

by:ste5an
ID: 42276936
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
 
LVL 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 166 total points (awarded by participants)
ID: 42276940
a process can't delete itself since it is running. you need another task to clean things up.
1
 
LVL 36

Expert Comment

by:ste5an
ID: 42276958
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
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42277027
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 42277039
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
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42277043
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
 
LVL 36

Expert Comment

by:ste5an
ID: 42277085
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
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42277096
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
 
LVL 36

Expert Comment

by:ste5an
ID: 42277112
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 166 total points (awarded by participants)
ID: 42280021
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
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 42280994
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
 
LVL 36

Expert Comment

by:ste5an
ID: 42281023
Yup, UAC and the file system virtualization can be a problem...
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 42281039
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
 
LVL 35

Expert Comment

by:sarabande
ID: 42281103
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
 
LVL 35

Expert Comment

by:sarabande
ID: 42299344
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 42299359
@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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 42305970
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

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

810 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