积极答复者
怎么实现程序自我删除

问题
答案
-
你好,
根据你的问题,下面这个代码我已经调试过了,可以正常运行。供你参考。
程序自我删除一是可以利用windows的command program的删除文件操作;其次可以启动新的进程来执行这个删除操作。
更多解释内容,可以参考以下这篇文章:
http://hi.baidu.com/whmtorrent/item/083e3d4a8348beeaa5c066b7
#include "stdafx.h" #include <Windows.h> #include <strsafe.h> #define SELF_REMOVE_STRING TEXT("cmd.exe /C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"%s\"") void DelMe() { TCHAR szModuleName[MAX_PATH]; TCHAR szCmd[2 * MAX_PATH]; STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; GetModuleFileName(NULL, szModuleName, MAX_PATH); StringCbPrintf(szCmd, 2 * MAX_PATH, SELF_REMOVE_STRING, szModuleName); CreateProcess(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } void main() { /* Do what you need */ /* Call this function at the very end of your program to delete itself */ DelMe(); }
Best Regards!
May
May Wang
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已建议为答案 黃宇祥 2013年8月15日 6:08
- 已标记为答案 May Wang - MSFT 2013年8月20日 3:49
全部回复
-
Please refer : http://blog.sina.com.cn/s/blog_460ccbb7010007xe.html
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
-
你好,
根据你的问题,下面这个代码我已经调试过了,可以正常运行。供你参考。
程序自我删除一是可以利用windows的command program的删除文件操作;其次可以启动新的进程来执行这个删除操作。
更多解释内容,可以参考以下这篇文章:
http://hi.baidu.com/whmtorrent/item/083e3d4a8348beeaa5c066b7
#include "stdafx.h" #include <Windows.h> #include <strsafe.h> #define SELF_REMOVE_STRING TEXT("cmd.exe /C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"%s\"") void DelMe() { TCHAR szModuleName[MAX_PATH]; TCHAR szCmd[2 * MAX_PATH]; STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; GetModuleFileName(NULL, szModuleName, MAX_PATH); StringCbPrintf(szCmd, 2 * MAX_PATH, SELF_REMOVE_STRING, szModuleName); CreateProcess(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } void main() { /* Do what you need */ /* Call this function at the very end of your program to delete itself */ DelMe(); }
Best Regards!
May
May Wang
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已建议为答案 黃宇祥 2013年8月15日 6:08
- 已标记为答案 May Wang - MSFT 2013年8月20日 3:49