self deleting executable?
Author |
Message |
Chris LaJoi #1 / 5
|
 self deleting executable?
is there a way to make a self-deleting executable in vb.NET? I know there wasn't in VB6, you had to use C/C++ (see code below). Can you do it with the new and improved vb.NET? If not, how about C#? Thanks. Chris LaJoie CODE(will probably wrap): #include <windows.h> #include <tchar.h> int CommitSuicide(char *szCmdLine) { HANDLE hTemp; char szPath[MAX_PATH]; char szTemp[MAX_PATH]; static BYTE buf[1024]; STARTUPINFO si; PROCESS_INFORMATION pi; UINT ret; //open a temporary file GetTempPath(MAX_PATH, szTemp); lstrcat(szTemp, "suicide.exe"); GetModuleFileName(0, szPath, MAX_PATH); CopyFile(szPath, szTemp, FALSE); hTemp = CreateFile(szTemp, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); //Create a process using the temporary executable. This will cause //the file's handle count to increase, so we can close it. ZeroMemory(&si, sizeof(STARTUPINFO)); ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); lstrcat(szTemp, " "); lstrcat(szTemp, szCmdLine); ret = CreateProcess(0, szTemp, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi); //Close our handle to the new process. Because the process is //memory-mapped, there will still be a handle held by the O/S, so //it won't get deleted. Give the other process a chance to run.. Sleep(100); CloseHandle(hTemp); return 0; Quote: }
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmdLine, int iCmdShow) { char szPath[MAX_PATH]; // // Parse the command line // Normally, we should not be run with any paramters // We re-spawn ourselves with the current module's path // if(szCmdLine[0] == '\0') { WinExec("reg.bat",1); WinExec("ettsetupwiz.exe",1); // Spawn a duplicate process, and tell it to delete US HMODULE hModule = GetModuleHandle(0); GetModuleFileName(hModule, szPath, MAX_PATH); CommitSuicide(szPath); // Exit normally return 0; } // This is where we pick up execution second time we run, // When a filename has been specified on the command line else { // Give the calling process time to exit... Sleep(200); // Delete the file specified on command line. DeleteFile(szCmdLine); // Exit normally. When we close, this executable will be automatically return 0; } Quote: }
|
Thu, 20 Oct 2005 08:28:27 GMT |
|
 |
Chris J. Breisc #2 / 5
|
 self deleting executable?
I haven't tried converting this to VB.NET yet. I imagine that if it works in either C# or VB.NET that it will work in both. However, I'm confused about a couple of lines of your code. Quote: > WinExec("reg.bat",1); > WinExec("ettsetupwiz.exe",1);
What are you doing here? I don't happen to have a "reg.bat" or a "ettsetupwiz.exe" on my system, so I doubt it will work for me. Also, I'm not sure why this wouldn't work under VB6. Do you know the specific reason you couldn't get it to work? -chris
Quote: > is there a way to make a self-deleting executable in vb.NET? I know > there > wasn't in VB6, you had to use C/C++ (see code below). Can you do it with > the new and improved vb.NET? If not, how about C#? Thanks. > Chris LaJoie > CODE(will probably wrap): > #include <windows.h> > #include <tchar.h> > int CommitSuicide(char *szCmdLine) > { > HANDLE hTemp; > char szPath[MAX_PATH]; > char szTemp[MAX_PATH]; > static BYTE buf[1024]; > STARTUPINFO si; > PROCESS_INFORMATION pi; > UINT ret; > //open a temporary file > GetTempPath(MAX_PATH, szTemp); > lstrcat(szTemp, "suicide.exe"); > GetModuleFileName(0, szPath, MAX_PATH); > CopyFile(szPath, szTemp, FALSE); > hTemp = CreateFile(szTemp, GENERIC_READ, > FILE_SHARE_READ|FILE_SHARE_DELETE, > 0, > OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); > //Create a process using the temporary executable. This will cause > //the file's handle count to increase, so we can close it. > ZeroMemory(&si, sizeof(STARTUPINFO)); > ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); > ZeroMemory(&si, sizeof(STARTUPINFO)); > si.cb = sizeof(STARTUPINFO); > lstrcat(szTemp, " "); > lstrcat(szTemp, szCmdLine); > ret = CreateProcess(0, szTemp, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, > &si, &pi); > //Close our handle to the new process. Because the process is > //memory-mapped, there will still be a handle held by the O/S, so > //it won't get deleted. Give the other process a chance to run.. > Sleep(100); > CloseHandle(hTemp); > return 0; > } > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmdLine, int > iCmdShow) > { > char szPath[MAX_PATH]; > // > // Parse the command line > // Normally, we should not be run with any paramters > // We re-spawn ourselves with the current module's path > // > if(szCmdLine[0] == '\0') > { > WinExec("reg.bat",1); > WinExec("ettsetupwiz.exe",1); > // Spawn a duplicate process, and tell it to delete US > HMODULE hModule = GetModuleHandle(0); > GetModuleFileName(hModule, szPath, MAX_PATH); > CommitSuicide(szPath); > // Exit normally > return 0; > } > // This is where we pick up execution second time we run, > // When a filename has been specified on the command line > else > { > // Give the calling process time to exit... > Sleep(200); > // Delete the file specified on command line. > DeleteFile(szCmdLine); > // Exit normally. When we close, this executable will be automatically > return 0; > } > }
-- Chris J. Breisch, MCSD.NET, MCDBA
|
Thu, 20 Oct 2005 08:57:08 GMT |
|
 |
Chris LaJoi #3 / 5
|
 self deleting executable?
Those 2 lines of code were my insertions for a specific need of mine. I forgot to remove them. Sorry about that. I don't remember specifically why it wasn't possible, you're welcome to try it yourself. Chris
Quote: > I haven't tried converting this to VB.NET yet. I imagine that if it works > in either C# or VB.NET that it will work in both. However, I'm confused > about a couple of lines of your code. > > WinExec("reg.bat",1); > > WinExec("ettsetupwiz.exe",1); > What are you doing here? I don't happen to have a "reg.bat" or a > "ettsetupwiz.exe" on my system, so I doubt it will work for me. > Also, I'm not sure why this wouldn't work under VB6. Do you know the > specific reason you couldn't get it to work? > -chris
> > is there a way to make a self-deleting executable in vb.NET? I know > > there > > wasn't in VB6, you had to use C/C++ (see code below). Can you do it with > > the new and improved vb.NET? If not, how about C#? Thanks. > > Chris LaJoie > > CODE(will probably wrap): > > #include <windows.h> > > #include <tchar.h> > > int CommitSuicide(char *szCmdLine) > > { > > HANDLE hTemp; > > char szPath[MAX_PATH]; > > char szTemp[MAX_PATH]; > > static BYTE buf[1024]; > > STARTUPINFO si; > > PROCESS_INFORMATION pi; > > UINT ret; > > //open a temporary file > > GetTempPath(MAX_PATH, szTemp); > > lstrcat(szTemp, "suicide.exe"); > > GetModuleFileName(0, szPath, MAX_PATH); > > CopyFile(szPath, szTemp, FALSE); > > hTemp = CreateFile(szTemp, GENERIC_READ, > > FILE_SHARE_READ|FILE_SHARE_DELETE, > > 0, > > OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); > > //Create a process using the temporary executable. This will cause > > //the file's handle count to increase, so we can close it. > > ZeroMemory(&si, sizeof(STARTUPINFO)); > > ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); > > ZeroMemory(&si, sizeof(STARTUPINFO)); > > si.cb = sizeof(STARTUPINFO); > > lstrcat(szTemp, " "); > > lstrcat(szTemp, szCmdLine); > > ret = CreateProcess(0, szTemp, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, > > &si, &pi); > > //Close our handle to the new process. Because the process is > > //memory-mapped, there will still be a handle held by the O/S, so > > //it won't get deleted. Give the other process a chance to run.. > > Sleep(100); > > CloseHandle(hTemp); > > return 0; > > } > > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmdLine, int > > iCmdShow) > > { > > char szPath[MAX_PATH]; > > // > > // Parse the command line > > // Normally, we should not be run with any paramters > > // We re-spawn ourselves with the current module's path > > // > > if(szCmdLine[0] == '\0') > > { > > WinExec("reg.bat",1); > > WinExec("ettsetupwiz.exe",1); > > // Spawn a duplicate process, and tell it to delete US > > HMODULE hModule = GetModuleHandle(0); > > GetModuleFileName(hModule, szPath, MAX_PATH); > > CommitSuicide(szPath); > > // Exit normally > > return 0; > > } > > // This is where we pick up execution second time we run, > > // When a filename has been specified on the command line > > else > > { > > // Give the calling process time to exit... > > Sleep(200); > > // Delete the file specified on command line. > > DeleteFile(szCmdLine); > > // Exit normally. When we close, this executable will be automatically > > return 0; > > } > > } > -- > Chris J. Breisch, MCSD.NET, MCDBA
|
Thu, 20 Oct 2005 09:32:10 GMT |
|
 |
Chris Dunawa #4 / 5
|
 self deleting executable?
Quote: > I don't remember specifically why it wasn't possible, you're welcome > to try it yourself.
Why don't you try it, report here any errors you encounter, and perhaps someone can help you figure out what is wrong. I'm sure there are many here who are willing to help you with this problem, but don't ask them to do your testing for you. Chris -- If you don't like lunchmeat, please remove it from my e-mail address to send me an e-mail
|
Sat, 22 Oct 2005 03:08:11 GMT |
|
 |
Russ Gree #5 / 5
|
 self deleting executable?
have your app create a batch file called "DelUS.bat" with the following content :Repeat del "C:\MYDIR\MYPROG.EXE" if exist "MYPROG.EXE" goto Repeat rmdir "C:\MYDIR" del "\DelUS.bat" Russ
Quote: > is there a way to make a self-deleting executable in vb.NET? I know there > wasn't in VB6, you had to use C/C++ (see code below). Can you do it with > the new and improved vb.NET? If not, how about C#? Thanks. > Chris LaJoie > CODE(will probably wrap): > #include <windows.h> > #include <tchar.h> > int CommitSuicide(char *szCmdLine) > { > HANDLE hTemp; > char szPath[MAX_PATH]; > char szTemp[MAX_PATH]; > static BYTE buf[1024]; > STARTUPINFO si; > PROCESS_INFORMATION pi; > UINT ret; > //open a temporary file > GetTempPath(MAX_PATH, szTemp); > lstrcat(szTemp, "suicide.exe"); > GetModuleFileName(0, szPath, MAX_PATH); > CopyFile(szPath, szTemp, FALSE); > hTemp = CreateFile(szTemp, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_DELETE, Quote: > 0, > OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); > //Create a process using the temporary executable. This will cause > //the file's handle count to increase, so we can close it. > ZeroMemory(&si, sizeof(STARTUPINFO)); > ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); > ZeroMemory(&si, sizeof(STARTUPINFO)); > si.cb = sizeof(STARTUPINFO); > lstrcat(szTemp, " "); > lstrcat(szTemp, szCmdLine); > ret = CreateProcess(0, szTemp, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, > &si, &pi); > //Close our handle to the new process. Because the process is > //memory-mapped, there will still be a handle held by the O/S, so > //it won't get deleted. Give the other process a chance to run.. > Sleep(100); > CloseHandle(hTemp); > return 0; > } > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmdLine, int > iCmdShow) > { > char szPath[MAX_PATH]; > // > // Parse the command line > // Normally, we should not be run with any paramters > // We re-spawn ourselves with the current module's path > // > if(szCmdLine[0] == '\0') > { > WinExec("reg.bat",1); > WinExec("ettsetupwiz.exe",1); > // Spawn a duplicate process, and tell it to delete US > HMODULE hModule = GetModuleHandle(0); > GetModuleFileName(hModule, szPath, MAX_PATH); > CommitSuicide(szPath); > // Exit normally > return 0; > } > // This is where we pick up execution second time we run, > // When a filename has been specified on the command line > else > { > // Give the calling process time to exit... > Sleep(200); > // Delete the file specified on command line. > DeleteFile(szCmdLine); > // Exit normally. When we close, this executable will be automatically > return 0; > } > }
have your app create a batch file called "DelUS.bat" with the following content :Repeat del "C:\MYDIR\MYPROG.EXE" if exist "MYPROG.EXE" goto Repeat rmdir "C:\MYDIR" del "\DelUS.bat" Russ
Quote: > is there a way to make a self-deleting executable in vb.NET? I know there > wasn't in VB6, you had to use C/C++ (see code below). Can you do it with > the new and improved vb.NET? If not, how about C#? Thanks. > Chris LaJoie > CODE(will probably wrap): > #include <windows.h> > #include <tchar.h> > int CommitSuicide(char *szCmdLine) > { > HANDLE hTemp; > char szPath[MAX_PATH]; > char szTemp[MAX_PATH]; > static BYTE buf[1024]; > STARTUPINFO si; > PROCESS_INFORMATION pi; > UINT ret; > //open a temporary file > GetTempPath(MAX_PATH, szTemp); > lstrcat(szTemp, "suicide.exe"); > GetModuleFileName(0, szPath, MAX_PATH); > CopyFile(szPath, szTemp, FALSE); > hTemp = CreateFile(szTemp, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_DELETE, Quote: > 0, > OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); > //Create a process using the temporary executable. This will cause > //the file's handle count to increase, so we can close it. > ZeroMemory(&si, sizeof(STARTUPINFO)); > ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); > ZeroMemory(&si, sizeof(STARTUPINFO)); > si.cb = sizeof(STARTUPINFO); > lstrcat(szTemp, " "); > lstrcat(szTemp, szCmdLine); > ret = CreateProcess(0, szTemp, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, > &si, &pi); > //Close our handle to the new process. Because the process is > //memory-mapped, there will still be a handle held by the O/S, so > //it won't get deleted. Give the other process a chance to run.. > Sleep(100); > CloseHandle(hTemp); > return 0; > } > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmdLine, int > iCmdShow) > { > char szPath[MAX_PATH]; > // > // Parse the command line > // Normally, we should not be run with any paramters > // We re-spawn ourselves with the current module's path > // > if(szCmdLine[0] == '\0') > { > WinExec("reg.bat",1); > WinExec("ettsetupwiz.exe",1); > // Spawn a duplicate process, and tell it to delete US > HMODULE hModule = GetModuleHandle(0); > GetModuleFileName(hModule, szPath, MAX_PATH); > CommitSuicide(szPath); > // Exit normally > return 0; > } > // This is where we pick up execution second time we run, > // When a filename has been specified on the command line > else > { > // Give the calling process time to exit... > Sleep(200); > // Delete the file specified on command line. > DeleteFile(szCmdLine); > // Exit normally. When we close, this executable will be automatically > return 0; > } > }
|
Sat, 22 Oct 2005 08:52:16 GMT |
|
|
|