// KillSelf2.cpp : Defines the entry point for the application. //#include "stdafx.h"/* Hi everyone: I've been working on the "racing" problem and I think I've found a solution: Remake the SelfDelete() function as a separate thread. Then increase the process and thread priorities so that most of the CPU cycles go to the process/thread. That will cause the ShellExecute process to wait in the background until the thread has exited.
The trick is to increase the process/thread priorities *after* you invoke the ShellExecute call. That way ShellExecute process will inherit the lower priority state. I tried out the code below by adding a 20 second delay from the time the ShellExecute call is made until the thread actually exits. Increasing the thread priority will effectively stop execution of the command shell process. Be careful because it will make your system "hang" -- but that's acceptable since the thread takes less than a second to exit without an artifical delay .
You will need to increase the priority of the current process. You can do this with the OpenProcess call and set permissions to PROCESS_ALL_ACCESS.
The code below should give you an idea of what going on. I hope everyone finds the main points correct (if not, be sure to post corrrections!) I've made a test program file using this code if anyone is interested. You can contact me a <tvarnas_at_excite_dot_com>
Thanks Matti, Alexander, and Luc for positing on this NG thread.
//#include "stdafx.h"/*
Hi everyone: I've been working on the "racing" problem and I think I've found a solution:
Remake the SelfDelete() function as a separate thread. Then increase the
process and thread priorities so that most of the CPU cycles go to the
process/thread. That will cause the ShellExecute process to wait in the
background until the thread has exited.
The trick is to increase the process/thread priorities *after* you invoke
the ShellExecute call. That way ShellExecute process will inherit the lower
priority state. I tried out the code below by adding a 20 second delay from
the time the ShellExecute call is made until the thread actually exits.
Increasing the thread priority will effectively stop execution of the
command shell process. Be careful because it will make your system "hang" --
but that's acceptable since the thread takes less than a second to exit
without an artifical delay .
You will need to increase the priority of the current process. You can do
this with the OpenProcess call and set permissions to PROCESS_ALL_ACCESS.
The code below should give you an idea of what going on. I hope everyone
finds the main points correct (if not, be sure to post corrrections!) I've
made a test program file using this code if anyone is interested. You can
contact me a <tvarnas_at_excite_dot_com>
Thanks Matti, Alexander, and Luc for positing on this NG thread.
--Tony
*///#include <windows.h>
#include <shlobj.h>
#include "shellapi.h"// globals
HANDLE hCurrentProcess, hSelfDeleteThread;
DWORD dwSelfDeleteThreadId, dwExitCode;
BOOL RemoveMainDirectory(const char* szComspec);BOOL RemoveMainDirectory(const char* szComspec)
{
TCHAR szParams [MAX_PATH];
TCHAR szModule [] = "E:\\Vc6\\VcTTT\\VcTT2003\\KillSelf2\\Debug\\test"; lstrcpy(szParams,"/c rd "); // run single command to...
lstrcat(szParams, szModule); // remove directory
lstrcat(szParams, " > nul"); // output results to nowhere
// execute command shell
if ((INT)ShellExecute(0,0,szComspec,szParams,0,SW_HIDE) > 32)
{
return TRUE;
}
else
{
return FALSE;
}
}////////////////////////////////////////////////////
// self-delete thread function
DWORD WINAPI tSelfDelete()
{
TCHAR szModule [MAX_PATH],
szComspec[MAX_PATH],
szParams [MAX_PATH];
// get file path names
if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
(GetEnvironmentVariable("Comspec",szComspec,MAX_PATH)!=0))
{
// create comspec parameters
lstrcpy(szParams,"/c del "); // run single command to...
lstrcat(szParams, szModule); // del(ete) self file and...
lstrcat(szParams, " > nul"); // output results to nowhere
// execute command shell
// and increase process+thread priorities
if ((INT)ShellExecute(0,0,szComspec,szParams,0,SW_HIDE) > 32)
{
RemoveMainDirectory(szComspec);
if ((SetPriorityClass(hCurrentProcess,HIGH_PRIORITY_CLASS)) &&
(SetThreadPriority(hSelfDeleteThread,THREAD_PRIORITY_HIGHEST)))
{
// notify explorer shell
SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0);
return 1;
}
}
}
return 0;
}////////////////////////////////////////////////////
// WinMain
INT APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//. . . code . . .
// set current process to "all_access"
hCurrentProcess=OpenProcess(
PROCESS_ALL_ACCESS,TRUE,
GetCurrentProcessId());
// create self-delete thread
hSelfDeleteThread=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)tSelfDelete,
0,0,&dwSelfDeleteThreadId);
// add thread creation error checking
// create thread state loop
GetExitCodeThread(hSelfDeleteThread,&dwExitCode);
while(dwExitCode==STILL_ACTIVE)
{
GetExitCodeThread(hSelfDeleteThread,&dwExitCode);
}
// exit program stuff
CloseHandle(hSelfDeleteThread);
CloseHandle(hCurrentProcess);
PostQuitMessage(0);
//. . .
return 0;
}
编译同不过!
我想用mfc的!!
shellexecute()
应该没有问题
{
HMODULE module = GetModuleHandle(0);
CHAR buf[MAX_PATH];
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(4);
__asm {
lea eax, buf
push 0
push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
return 0;
}
一楼是对的,代码我主页上也有,SDK写的,你试试能不能用.
http://nowcan.yeah.net